xamarin - 当用户将输入输入单元格时,自定义单元格不会更新 View 模型

标签 xamarin xamarin.ios mvvmcross 2-way-object-databinding

我是 MVVMCross(Xamarin.iOS) 的新手。所以,我可能走错了方向。如果有人能指出我正确的方向或指出我做错了什么。

我已经查看了 MVVMCross 的“CustomerManagement”和“Collection”示例。

基本上我正在尝试创 build 置屏幕。

我有一个带有一个文本字段的自定义 UITableViewCell。请参阅下面的代码。 “EntryTF”是 IBOutleted。 “EntryTF”与模型的“FirstName”属性绑定(bind)。将值设置为“FirstName”反射(reflect)在 UI 上。但是,如果用户在文本字段中输入内容,则不会保存到模型中。简而言之,单元格不会更新 View 模型/模型。

请注意,我想保留单元格类之外的绑定(bind)。因此,我可以将此单元格重复用于其他模型或字段。

public partial class PlainEntryCell : UITableViewCell
{
    public static readonly UINib Nib = UINib.FromName ("PlainEntryCell", NSBundle.MainBundle);
    public static readonly NSString Key = new NSString ("PlainEntryCell");

    public PlainEntryCell (IntPtr handle) : base (handle)
    {
  //            this.DelayBind (() => {
 //             this.AddBindings(new Dictionary<object,string> ())
 //         });
    }

    public static PlainEntryCell Create ()
    {
        return (PlainEntryCell)Nib.Instantiate (null, null) [0];
    }

    public string CaptionText {
        get {
            return EntryTF.Text;
        }
        set {
            EntryTF.Text = value;
        }
    }
}

我的 View 模型:

public class RegisterViewModel: MvxViewModel
{
    private RegisterModel _registerModel;

    public RegisterViewModel ()
    {
        _registerModel = new RegisterModel ();
        _registerModel.FirstName = "Test";
    }

    public RegisterModel Customer {
        get { return _registerModel; }
        set {
            _registerModel = value;
            RaisePropertyChanged ("Customer");
        }
    }
}

型号:

public class RegisterModel:MvxNotifyPropertyChanged
{
    private string _firstName;

    public string FirstName {
        get { return _firstName; }
        set {
            _firstName = value;
            RaisePropertyChanged ("FirstName");
        }
    }

    public string LastName { get; set; }

    public string PhoneNum { get; set; }

    public string Email { get; set; }

    public string Pin { get; set; }
}

表格 View 来源:

public class RegisterTableViewSource: MvxTableViewSource
{
    RegisterView _registerView;

    public RegisterTableViewSource (UITableView tableView, RegisterView registerView)
        : base (tableView)
    {
        _registerView = registerView;

        tableView.RegisterNibForCellReuse (PlainEntryCell.Nib,
            PlainEntryCell.Key);
        //tableView.RegisterNibForCellReuse (UINib.FromName ("DogCell", NSBundle.MainBundle), DogCellIdentifier);
    }

    protected override UITableViewCell GetOrCreateCellFor (UITableView tableView, Foundation.NSIndexPath indexPath, object item)
    {
        var cell = TableView.DequeueReusableCell (PlainEntryCell.Key, indexPath);
        cell.Bind (_registerView, "CaptionText Customer.FirstName");
        return cell;
        //return (UITableViewCell)TableView.DequeueReusableCell (PlainEntryCell.Key, indexPath);
    }

    public override nint RowsInSection (UITableView tableview, nint section)
    {
        return 2;
    }
}

更新: 仍然无法得到答案。 在上面的代码中,我想将“EntryTF”与模型的属性绑定(bind)。但我想在课外保持约束力。因此,如果有人可以指出绑定(bind)到“EntryTF”的直接方式,则不需要 CaptionText 属性 有没有像 Xamarin Forms 一样创建 BindableProperty 的方法?感觉MVVMCross这么成熟的框架,为什么没有解决这种简单的事情。

如果有任何简单/其他方法可以实现相同的目标,我也很乐意来到这里。

我也看过 MTD,但没有发现对自定义单元有多大用处,它也需要我大量的学习。

最佳答案

Basically I am trying to create Settings screen.

看看使用单点触控对话框,mt.d,与 MvvmCross,Use Mvvmcross Binding with MonoTouch.Dialog (Lists and Commands)

该 StackOverflow 问题/答案将帮助您开始将 mt.d 与 mvvmcross 结合使用。我将它用于我的应用程序中的基本设置屏幕。

Cell Isn't Updating

我写了一篇关于在 MvvmCross 中使用自定义单元格的文章,请参阅 http://benjaminhysell.com/archive/2014/04/mvvmcross-custom-mvxtableviewcell-without-a-nib-file/

我发现不使用 nib 文件并在代码中完整描述我的 UI 会更容易。

在您的 public partial class PlainEntryCell : UITableViewCell 中,您似乎从未将单元格绑定(bind)到 ViewModel。你已经注释掉了。我会尝试类似的方法,同时添加 http://slodge.blogspot.com/2013/07/playing-with-constraints.html到您的布局应用程序:

 public PlainEntryCell()
        {
            CreateLayout();
            InitializeBindings();
        }

 UILabel captionText;

 private void CreateLayout()
        {            
            SelectionStyle = UITableViewCellSelectionStyle.None;


            Accessory = UITableViewCellAccessory.DisclosureIndicator;          
            captionText = new UILabel();
             ContentView.AddSubviews(captionText);
            ContentView.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
            const int vPadding = 10;
            const int hPadding = 20;
            ContentView.AddConstraints(

                captionText.AtTopOf(ContentView).Plus(vPadding),
                captionText.AtLeftOf(ContentView).Plus(hPadding),
                captionText.Width().EqualTo(UIScreen.MainScreen.Bounds.Width / 2)
);

 private void InitializeBindings()
        {
            this.DelayBind(() =>
            {
                var set = this.CreateBindingSet<PlainEntryCell, RegisterViewModel();
                set.Bind(captionText).To(vm => vm.CaptionText);               
                set.Apply();
            });
        }

}

关于xamarin - 当用户将输入输入单元格时,自定义单元格不会更新 View 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31043759/

相关文章:

c# - 如何播放即使您在 Xamarin Forms 中浏览页面也能继续播放的音频文件?

c# - 无法在移动设备上滚动使用 Xamarin.Forms 创建的 xaml 页面

c# - 调用 MonoTouch.UIKit.UIDevice.CurrentDevice.SystemVersion 时发生崩溃

xamarin.ios - 支持两个 Storyboard

mvvmcross - MVVMCross for Touch 支持双向绑定(bind)吗? (除了 UITextFields)

xamarin - 从 ViewCell 访问 ViewModel 属性

android - Xamarin 表单 Java.Lang.NoSuchMethodError

c# - MVVMCross Release 构建不工作(LinkerPleaseInclude Listview)

xamarin - MVVMCross iOS 静态单元格命令绑定(bind)

c# - Microsoft Graph - 获取成员(member)资格