【问题标题】:Navigating to a TabbedPage using a ToolBarItem with FreshMvvm使用带有 FreshMvvm 的 ToolBarItem 导航到 TabbedPage
【发布时间】:2017-03-13 23:56:53
【问题描述】:

我想知道如何使用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 的潜力?

感谢您的宝贵时间。

【问题讨论】:

    标签: c# xamarin xamarin.forms freshmvvm


    【解决方案1】:

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

    FreshIOC.Container.Resolve&lt;IFreshNavigationService&gt;().PushPage (FreshPageModelResolver.ResolvePageModel&lt;MainPageModel&gt;(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 方式导航到它。

    【讨论】:

      猜你喜欢
      • 2017-04-10
      • 2021-03-06
      • 2017-11-13
      • 1970-01-01
      • 2016-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多