Reputation: 583
I am working on an application using Xamarin.Android and MVVMCross and I am running into some issues with trying to set the ViewModel (MvxViewModel) for a Fragment (MvxFragment).
In my research so far, I have found some examples but they seem to be pretty dated and, therefore, no longer useful.
https://forums.xamarin.com/discussion/3652/mvvmcross-activity-vs-fragment http://slodge.blogspot.com/2013/06/n26-fragments-n1-days-of-mvvmcross.html
My specific situation is that I am trying to create an application with a drawerlayout which I have working just fine, the problem is that when I load an MvxFragment subclass via the FragmentManager the ViewModel is not being associated with the View (the ViewModel was correctly associated before I changed the View from a Activity to a Fragment.)
When I was looking up some documentation on how to do this, I found the following code
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
return this.BindingInflate(Resource.Layout.Fragment_Detail, null);
}
the issue with this is that there is no method BindingInflate(...)
in MvxFragment.
Here is what my OnCreateView looks like
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
var view = inflater.Inflate(Resource.Layout.HomeView, container, false);
return view;
}
So my question boils down to: How do I associate my ViewModel with my View that is an extention of MVxFragment?
Not sure if this helps, but I am creating the Fragment like so
var fragment = new HomeView();
FragmentManager.BeginTransaction().Replace(Resource.Id.content_frame, fragment).Commit();
Upvotes: 4
Views: 2853
Reputation: 583
I can't believe that I missed this, but the solution was really simple. All I had to do was add this.
using Cirrious.MvvmCross.Binding.Droid.BindingContext;
Upvotes: 2