Custom cell does not update viewmodel when user enters input into cell

I am new to MVVMCross (Xamarin.iOS). So, I could be in the wrong direction. If anyone can point me in the right direction or point out what I am doing wrong.

I've already looked at the "CustomerManagement" and "Collection" MVVMCross sample.

Basically I am trying to create a settings screen.

I have one custom UITableViewCell with one textbox. See my code below. "EntryTF" - IBOutleted. The "EntryTF" is bound to the "FirstName" property of the model. The value of the FirstName parameter is reflected in the user interface. But if the user enters something in the text box, it is not saved in the model. The viewmodel / model is not updated in the short cell.

Please note that I want to keep the binding to the cell class. This way I can reuse this cell for other models or fields.

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;
        }
    }
}

      

My view model:

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");
        }
    }
}

      

Model:

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; }
}

      

Source of TableView:

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;
    }
}

      

Update: Still not able to get a response. In the above code, I want to bind "EntryTF" to a property of the model. But I want to keep the binding of the outer class. So the CaptionText property is not required unless someone can specify a direct way to bind to "EntryTF", Isn't there a way to create a BindableProperty just like Xamarin Forms? I feel like MVVMCross is a mature framework, so there is no solution to this kind of simple stuff.

I would also love it here if there is some simple / different way to achieve the same things.

I also looked at MTD but didn't find much useful for a custom cell and I need a good amount of training as well.

+3


source to share


3 answers


Basically I am trying to create a settings screen.

Take a look at using monotouch dialog, mt.d, with MvvmCross, Use Mvvmcross binding with MonoTouch.Dialog (lists and commands)

In this StackOverflow question / answer, you will start using mt.d using mvvmcross. I use it for the main settings screens in my applications.

Cell is not updating



I wrote an article on using custom cells with MvvmCross, see http://benjaminhysell.com/archive/2014/04/mvvmcross-custom-mvxtableviewcell-without-a-nib-file/

It was easier for me not to use nib files and completely describe my interface in code.

It looks like in public partial class PlainEntryCell : UITableViewCell

you never bind a cell to the ViewModel. You have commented on this. I would try something like this by adding http://slodge.blogspot.com/2013/07/playing-with-constraints.html to your layout app:

 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();
            });
        }

}

      

+2


source


The custom cell must implement INotifyPropertyChanged in order to allow the ViewModel to be notified of a value change in your cell's properties.



public partial class PlainEntryCell : UITableViewCell, INotifyPropertyChanged
{
    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;
            RaisePropertyChanged("CaptionText");
        }
    }

    #region INotifyPropertyChanged implementation

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion
}

      

+1


source


Have you found a possible solution?

I've been trying for about a week to use the two way tableviewcell method. The first problem is a static Tableviewsource (Observable Collection) that triggers a reload of the source change collection and hides the Keyboard. set;} to CellItem -> The cell item is very static and I cannot get the binding from the setting item to the ViewModel.

My idea was that SettingViewModel -> ObservableCollection for TableViewSource SettingViewModel contains username and email -> when the email address changes, the username will be updated to the email message (the first part is separated by '@').

TableViewCell contains SettingItem (from ObservableCollection) with TextField SettingItem. Email is bound two-way to SettingViewModel <-> Email SettingItem Username is bound two-way to SettingViewModel <-> Username

do you have any ideas or solutions - the top answer won't help me.

0


source







All Articles