**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。因此我无法将数据绑定到控件。请帮我解决此问题。
答案 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>