xamarin.formsfreshmvvm

FreshMVVM - best way to open a page from a child ContentView that doesn't inherit from FreshBasePageModel


The following code shows 2 examples of an OpenPage Command. The one in MainPageModel works since it derives directly from FreshBasePageModel. However, the second OpenPage call in the ChildPageModel won't work (or compile). I don't want to pass the parent model all around. So how, using FreshMVVM, do I open a new page from the ChildPageModel (and have the back button work, etc)?

public class MainPageModel : FreshBasePageModel
{
    public Command OpenPage
    {
        get
        {
            return new Command(() =>
            {
                CoreMethods.PushPageModel<NewPageModel>();
            });
        }
    }

    public ChildPageModel ChildPageModel { get; set; }
}

public class ChildPageModel 
{
    public Command OpenPage
    {
        get
        {
            return new Command(() =>
            {
                // ??????
                CoreMethods.PushPageModel<NewPageModel>();
            });
        }
    }
}

Solution

  • The following code will accomplish this...

    var page = FreshPageModelResolver.ResolvePageModel<MainPageModel>();
    var model = page.GetModel() as MainPageModel;
    var navService = FreshMvvm.FreshIOC.Container.Resolve<IFreshNavigationService>();
    await navService.PushPage(page, null);