BenF114
BenF114

Reputation: 21

MvxFragment ViewModels

I'm trying to get Fragments to work when using MvvmCross 3.5.

I have the following fragment:

public class MainView
  : MvxFragment<MenuView>
{

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    var ignored = base.OnCreateView(inflater, container, savedInstanceState);
    this.EnsureBindingContextIsSet(savedInstanceState);
    var view = this.BindingInflate(Resource.Layout.MainView, null);

    return view;
 }
}

I have been using the following code for manually initializing the ViewModel:

var loaderService = Mvx.Resolve<IMvxViewModelLoader>();
ViewModel = (T)loaderService.LoadViewModel(
           new MvxViewModelRequest(typeof(T), null, null, null), null);

This works fine except when the Fragment is resumed at which point the app blows up with a NullReferenceException.

I am now trying to use the MvxCachingFragmentActivity to handle the lifecycle of the fragments but cannot find any documentation on it.

I have registered the fragments:

RegisterFragment<MainView, MainViewModel>(typeof(MainView).Name, bundle);

and then shown one:

ShowFragment(typeof(MainView).Name, Resource.Id.content_frame, bundle);

The fragment loads but ViewModel is null. Am I missing something obvious?

Upvotes: 1

Views: 781

Answers (1)

BenF114
BenF114

Reputation: 21

[MvxOwnedViewModelFragment] 

on the fragment is needed as of 3.5.1

Upvotes: 1

Related Questions