Viewmodel始终为null

时间:2015-08-06 11:24:40

标签: android xamarin mvvmcross

         **This is My ViewModel**
        namespace TipCalc.Core
         { 
           public class ProductModel : MvxViewModel
        {
            public string productname{ get; set;}
            public string productprice{ get; set;}
            public ProductModel()
        {
            productname="qwe";
            productprice="123";
      }
        //This is my view model
   }
 }



   **This is my View**

       public class ViewOfProduct : MvxFragment
{

    public new ProductModel ViewModel
    {
        get { return (ProductModel) base.ViewModel; }
        set { base.ViewModel = value; }
    }



    public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {

        View view = inflater.Inflate (Resource.Layout.ViewOfProduct, container, false);
        return view;
    }
}

我正在使用xamrin android应用程序,我正在使用MvvmCross.I让ProductModel(ViewModel)在视图中始终为null。因此我无法将数据绑定到控件。请帮我解决此问题。

1 个答案:

答案 0 :(得分:0)

您需要使用BindingInflate在视图中使用数据绑定:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        var ignore = base.OnCreateView(inflater, container, savedInstanceState);

        var view = this.BindingInflate(FragmentId, null);
        return view;
    }

另外我建议使用gerenics来设置viewmodel:

public class SomeFragment : MvxFragment<SomeViewModel>

此处提供完整的示例应用: https://github.com/MvvmCross/MvvmCross-AndroidSupport/blob/master/Samples/Example.Droid/Activities/MainActivity.cs#L33