3

私は MVVMCross(Xamarin.iOS) が初めてです。だから、私は間違った方向にいる可能性があります。誰かが私を正しい方向に向けたり、私が間違っていることを指摘したりできれば.

MVVMCross の "CustomerManagement" と "Collection" のサンプルは既に見てきました。

基本的には設定画面を作成しようとしています。

1 つのテキスト フィールドを持つ 1 つのカスタム UITableViewCell があります。以下の私のコードを参照してください。「EntryTF」は IBOutleted です。「EntryTF」はモデルの「FirstName」プロパティにバインドされています。値を「FirstName」に設定するとUIに反映されます。しかし、ユーザーがテキストフィールドに何かを入力すると、モデルに保存されません。要するに、セルはビューモデル/モデルを更新していません。

セルクラスの外にバインドしたいことに注意してください。したがって、このセルを他のモデルやフィールドに再利用できます。

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

私のビューモデル:

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

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

更新: まだ答えを得ることができません。上記のコードでは、「EntryTF」をモデルのプロパティにバインドしたいと考えています。しかし、私はクラス外でバインディングを続けたいと思っています。そこで、誰かが「EntryTF」に直接バインドする方法を指摘できれば、CaptionText プロパティは必要ありません。 Xamarin Forms のように BindableProperty を作成する方法はありませんか? MVVMCross は成熟したフレームワークだと思うのに、なぜこのような単純なことの解決策がないのでしょうか。

同じことを達成する簡単な/他の方法があれば、私もここにいたいと思います.

MTD も調べましたが、カスタム セルにはあま​​り役に立ちませんでした。また、かなりの量の学習が必要です。

4

2 に答える 2