1

我想知道如何TabbedNavigationContainer通过ToolBarItem点击来调用我的特定标签页。我有一个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;
    }
}

这将打开我的视图,我可以看到我的选项卡式应用程序。我的问题是单击“主页”Map时如何选择页面?ToolbarItem

我知道我可以编写自己的基本导航服务来App注入,但这似乎我没有充分利用 FreshMvvm 的潜力?

谢谢你的时间。

4

1 回答 1

1

我不完全确定您的项目结构,但我认为您正在尝试将导航添加到实际页面的代码隐藏中,对吗?虽然你可以这样做,但这有点违背 MVVM 原则。如果您仍然想这样做,您可能必须执行以下操作:

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

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

你应该分配你的Command属性ToolBarItem,它是可绑定的,并在它后面创建一个 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 方式导航到它。

于 2016-11-01T07:51:20.803 回答