MoonKnight
MoonKnight

Reputation: 23831

Navigating to a TabbedPage using a ToolBarItem with FreshMvvm

I would like to know how I can call a specific tabbed page of my TabbedNavigationContainer using a ToolBarItem click. I have a BaseContentPage base class

public class BaseContentPage : ContentPage, IPage
{
    public BaseContentPage()
    {
        ToolbarItems.Add(new ToolbarItem("Main Page", null, () => 
        {
            //Application.Current.MainPage = ??;
        }));
    }
}

from which all pages derive from.

public class App : Application
{
    public App()
    {
        Registrations();
        InitializeGui();
    }

    private void Registrations()
    {
        //FreshIOC.Container.Register<IFreshNavigationService
    }

    private void InitializeGui()
    {
        var tabbedNavigationContainer = new FreshTabbedNavigationContainer();
        tabbedNavigationContainer.AddTab<MapPageModel>("Map", "icon.png");
        tabbedNavigationContainer.AddTab<HistoryPageModel>("History", "icon.png");
        MainPage = tabbedNavigationContainer;
    }
}

This opens my view and I can see my tabbed application. My question is how can I select the Map page when the ToolbarItem "Main Page" is clicked?

I am aware I could write my own basic navigation service in which App gets injected, but this seem like I am not using the full potential of FreshMvvm?

Thanks for your time.

Upvotes: 1

Views: 901

Answers (1)

Gerald Versluis
Gerald Versluis

Reputation: 34128

I'm not entirely sure about the structure of your project but I think you're trying to add the navigation to the code-behind of your actual Page right? Although you can do this, it is somewhat against the MVVM principle. If you'd still want to do that you will probably have to do something like this:

FreshIOC.Container.Resolve<IFreshNavigationService>().PushPage (FreshPageModelResolver.ResolvePageModel<MainPageModel>(null), null);

Although it should work, it isn't the best way.

You should assign the Command property of your ToolBarItem, which is bindable and create a PageModel behind it which implements that command.

I am going to assume you're using XAML, so your XAML will look like this:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" Title="MyPage">
    <ContentPage.ToolbarItems>
        <ToolbarItem Text="Main Page" Command="{Binding GoToMainPageCommand}" />
    </ContentPage.ToolbarItems>

    <!-- ... Rest of page ... -->
</ContentPage>

Now create a PageModel for this page which implements the GoToMainPageCommand.

public class MyPagePageModel : FreshBasePageModel
{
   public ICommand GoToMainPageCommand { get; private set; }

   public MyPagePageModel()
   {
      GoToMainPageCommand = new Command(GoToPage);
   }

   private async void GoToPage()
   {
      await CoreMethods.PushPageModel<MainPageModel>();
   }
}

Now you are navigating to it in a true MVVM way.

Upvotes: 1

Related Questions