【问题标题】:Viewmodel is always nullViewmodel 始终为空
【发布时间】:2015-10-29 12:40:14
【问题描述】:
         **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。我在视图中得到 ProductModel(ViewModel) 始终为空。所以我无法将数据绑定到控件。帮我解决这个问题。

【问题讨论】:

    标签: android xamarin mvvmcross


    【解决方案1】:

    您需要使用 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 来设置视图模型:

    public class SomeFragment : MvxFragment<SomeViewModel>
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 2017-12-08
      • 1970-01-01
      • 2017-07-31
      • 2019-07-18
      相关资源
      最近更新 更多