使用FreshMvvm的工具栏项导航到选项卡页面

本文关键字:选项 导航 FreshMvvm 工具栏 使用 | 更新日期: 2023-09-27 18:15:38

我想知道如何使用ToolBarItem单击调用TabbedNavigationContainer的特定选项卡页面。我有一个BaseContentPage基类

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

,所有页面都是从它派生的。

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;
    }
}

打开我的视图,我可以看到我的选项卡应用程序。我的问题是,当点击ToolbarItem"主页"时,我如何选择Map页面?

我知道我可以编写自己的基本导航服务,其中App被注入,但这似乎我没有使用FreshMvvm的全部潜力?

感谢您的宝贵时间。

使用FreshMvvm的工具栏项导航到选项卡页面

我不完全确定你的项目结构,但我认为你正试图将导航添加到实际页面的代码后面,对吗?虽然您可以这样做,但这在某种程度上违反了MVVM原则。如果你仍然想这样做,你可能需要这样做:

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

虽然它应该工作,但它不是最好的方法。

您应该分配ToolBarItemCommand属性,这是可绑定的,并在它后面创建一个PageModel来实现该命令。

我将假设您使用的是XAML,所以您的XAML将看起来像这样:

<?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>

现在为这个页面创建一个PageModel,它实现了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>();
   }
}

现在你是在一个真正的MVVM方式导航到它。