Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自定义控件绑定不工作_C#_Ios_Mvvmcross - Fatal编程技术网

C# 自定义控件绑定不工作

C# 自定义控件绑定不工作,c#,ios,mvvmcross,C#,Ios,Mvvmcross,我已经为ios创建了一个自定义的numbpad控件,现在尝试将它与MVVMCross绑定到我的ViewModel。但它不起作用。ViewModel中的值始终为null 号码牌的代码为: [Register("NumberPad")] public partial class NumberPad : UIView { public string Text { get; set; } public NumberPad(IntPtr

我已经为ios创建了一个自定义的numbpad控件,现在尝试将它与MVVMCross绑定到我的ViewModel。但它不起作用。ViewModel中的值始终为null

号码牌的代码为:

    [Register("NumberPad")]
public partial class NumberPad : UIView
{

    public string Text {
        get;
        set;
    }


    public NumberPad(IntPtr h): base(h)
    {
        //SetUp ();         
    }

    public NumberPad (RectangleF frame) : base(frame)
    {
        SetUp ();

    }

    public NumberPad ()
    {           
        //SetUp ();         
    }

    void SetUp ()
    {
        var arr = NSBundle.MainBundle.LoadNib ("NumberPad", this, null);
        var v = Runtime.GetNSObject (arr.ValueAt (0)) as UIView;
        v.Frame = new RectangleF (0, 0, Frame.Width, Frame.Height);
        AddSubview (v);

        Number0.TouchUpInside += HandleTouchUpInside;
        Number1.TouchUpInside += HandleTouchUpInside;
        Number2.TouchUpInside += HandleTouchUpInside;
        Number3.TouchUpInside += HandleTouchUpInside;
        Number4.TouchUpInside += HandleTouchUpInside;
        Number5.TouchUpInside += HandleTouchUpInside;
        Number6.TouchUpInside += HandleTouchUpInside;
        Number7.TouchUpInside += HandleTouchUpInside;
        Number8.TouchUpInside += HandleTouchUpInside;
        Number9.TouchUpInside += HandleTouchUpInside;

    }

    void HandleTouchUpInside (object sender, EventArgs e)
    {
        int tag = ((UIButton)sender).Tag;

        if (tag >= 0 && tag <= 9) {
            Text = String.Format("{0}{1}",Text,tag);
        } else if (tag == 10 && Text.Length > 0) {
            Text = Text + Text.Substring (0, Text.Length - 1);
        }
    }
}

有什么想法吗?

如果希望MvvmCross自动获取属性中的更改,如:

public string Text {
    get;
    set;
}
然后您必须给MvvmCross一些方法来知道属性的值已经改变。最简单的方法是简单地提供一个具有常规名称的事件,并在文本更改时引发此更改,例如:

public event EventHandler TextChanged;

private string _text;
public string Text {
    get { return _text; }
    set {  _text = value; TextChanged.Raise(this);  }
}
有关自定义控件和自定义绑定的详细信息,请参见中的N=18、19、20和28

public event EventHandler TextChanged;

private string _text;
public string Text {
    get { return _text; }
    set {  _text = value; TextChanged.Raise(this);  }
}