I am looking for an example or documentation to work with fragments in the current mvvmcross version. I found a lot of information about the old versions but I am confused how I should implement it now.
The best information so far I have found is in this article: http://gregshackles.com/presenters-in-mvvmcross-navigating-android-with-fragments/
But it is over one year old and I think there is already a newer/better way to do it.
What I want is having a single Activity(like in an SPA or having a MainView) this activity should contain one or two fragment and on a button click I want to navigate/change to a different fragment and the current one on the backstack for navigation. Or is it recommend to use only activities for navigation and not implementing a SPA?
Thanks for your help! Timo
The new way to use fragments in MvvmCross is by using attributes on your fragment.
[MvxFragment(typeof(ActivityHostViewModel), Resource.Id.content_frame, true)]
public class HomeFragment : BaseFragment<HomeViewModel>
{
}
The MvvmCross presenter will recognize that this is a fragment, and show it inside the host activity which is attached to the host viewmodel.
To support fragments in the presenter you need to add the following to your setup.cs
protected override IMvxAndroidViewPresenter CreateViewPresenter()
{
var mvxFragmentsPresenter = new MvxFragmentsPresenter(AndroidViewAssemblies);
Mvx.RegisterSingleton<IMvxAndroidViewPresenter>(mvxFragmentsPresenter);
return mvxFragmentsPresenter;
}
A full sample is available here: https://github.com/MvvmCross/MvvmCross/tree/develop/TestProjects/Android-Support/Fragments
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With