Skip to content

Instantly share code, notes, and snippets.

@ChaseFlorell
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChaseFlorell/9942910 to your computer and use it in GitHub Desktop.
Save ChaseFlorell/9942910 to your computer and use it in GitHub Desktop.
Simple way to Show a Fragment from within an MvxFragmentActivity
using System;
using Cirrious.CrossCore;
using Cirrious.MvvmCross.Droid.Fragging;
using Cirrious.MvvmCross.Droid.Fragging.Fragments;
using Cirrious.MvvmCross.ViewModels;
namespace $NAMESPACE$
{
public class FragmentActivityBase : MvxFragmentActivity
{
/// <summary>
/// Show a Fragment with an unpopulated ViewModel.
/// (Leverages <see cref="Mvx.IocConstruct{T}()"/> to construct the ViewModel on the fly.)
/// </summary>
/// <typeparam name="TFragment">The MvxFragment you wish to launch</typeparam>
/// <typeparam name="TViewModel">The IMvxViewModel you wish to use</typeparam>
/// <param name="frameLayout">The FrameLayout that the Fragment is being injected into</param>
/// <remarks>TViewModel is constructed on the fly.
/// If you wish to pass in a pre-populated ViewModel, use <see cref="ShowFragment{TFragment}(IMvxViewModel, int)"/></remarks>
/// <example>
/// ShowFragment{FooFragment, FooFragmentViewModel}(Resource.Id.ContentFrame);
/// </example>
protected void ShowFragment<TFragment, TViewModel>(int frameLayout)
where TFragment : MvxFragment
where TViewModel : IMvxViewModel
{
var fragment = (TFragment)Activator.CreateInstance(typeof(TFragment));
fragment.ViewModel = Mvx.IocConstruct<TViewModel>();
SupportFragmentManager.BeginTransaction()
.Replace(frameLayout, fragment)
.Commit();
}
/// <summary>
/// Show a Fragment with a pre-populated ViewModel
/// </summary>
/// <typeparam name="TFragment">The MvxFragment you wish to launch</typeparam>
/// <param name="viewModel">The pre-populated ViewModel you wish to use</param>
/// <param name="frameLayout">The FrameLayout that the Fragment is being injected into</param>
/// <remarks> Use when you need to pass data into the ViewModel
/// If you wish to have an unpopulated ViewModel constructed, use <see cref="ShowFragment{TFragment, TViewModel}(int)"/>
/// </remarks>
/// <example>
/// var viewModel = Mvx.IocConstruct{FooViewModel}();
/// viewModel.Bar = "This is the bar string."
/// ShowFragment{FooFragment}(viewModel, Resource.Id.ContentFrame);
/// </example>
protected void ShowFragment<TFragment>(IMvxViewModel viewModel, int frameLayout)
where TFragment : MvxFragment
{
var fragment = (TFragment)Activator.CreateInstance(typeof(TFragment));
fragment.ViewModel = viewModel;
SupportFragmentManager.BeginTransaction()
.Replace(frameLayout, fragment)
.Commit();
}
}
}
// create your own viewModel and pass it to the Fragment
var viewModel = new FooViewModel {Content = "some content"};
ShowFragment<FooFragment>(viewModel, Resource.Id.ContentFrame);
// construct a ViewModel and then populate
// - probably what you'll use when you have both
// IMvxCommands in the Constructor of your ViewModel
// AND you need to fill with data
var viewModel = Mvx.IocConstruct<FooViewModel>();
viewModel.Bar = "This is the bar string."
ShowFragment<FooFragment>(viewModel, Resource.Id.ContentFrame);
// have the viewModel constructed by Mvx.
ShowFragment<FooFragment, FooFragmentViewModel>(Resource.Id.ContentFrame);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment