Xamarin.Forms Prism 应用未显示主从导航栏

Xamarin.Forms Prism app not showing Master-Detail navigation bar

我正在尝试在我的 Xamarin.Forms 应用程序中使用我的 UWP 目标 运行ning 在 Windows 10.

配置主从导航

当我 运行 Xamarin 提供的示例 Master-Detail 应用程序(遵循 https://developer.xamarin.com/guides/xamarin-forms/user-interface/navigation/master-detail-page/),并将 MasterBehavior 更改为 Popover 时,我看到以下行为:

启动:

Select 汉堡图标:

进行选择:

在我的棱镜应用程序中,我导航到 MainPage/View1:

protected override void OnInitialized()
{
    InitializeComponent();
    var task = NavigationService.NavigateAsync("MainPage/View1");
    ...
}

MainPage 是我的 MasterDetailPage,MasterBehavior 设置为 Popover,View1 是一个 ContentPage。

主页:

<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
                  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                  xmlns:views="clr-namespace:My.Mobile.Application.Views;assembly=My.Mobile.Application"
                  x:Class="My.Mobile.Application.Frame.MainPage"
                  MasterBehavior="Popover">
  <MasterDetailPage.Master>
    <views:MasterPage x:Name="masterPage" />
  </MasterDetailPage.Master>
  <MasterDetailPage.Detail>
    <NavigationPage>
      <x:Arguments>
        <views:View1 />
      </x:Arguments>
    </NavigationPage>
  </MasterDetailPage.Detail>
</MasterDetailPage>

视图 1:

public View1()
{
  NavigationPage.SetHasNavigationBar(this,false);
  InitializeComponent();
}

启动时,我没有看到任何导航栏,只有 View1 的内容(目前只是红屏):

如果我将 MainPage.xaml 的 MasterBehavior 改为 Default 而不是 Popover,并删除 View1 中的 SetHasNavigationBar,我会在应用启动时看到侧边菜单:

主页:

<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
                  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                  xmlns:views="clr-namespace:My.Mobile.Application.Views;assembly=My.Mobile.Application"
                  x:Class="My.Mobile.Application.Frame.MainPage"
                  MasterBehavior="Default">
  <MasterDetailPage.Master>
    <views:MasterPage x:Name="masterPage" />
  </MasterDetailPage.Master>
  <MasterDetailPage.Detail>
    <NavigationPage>
      <x:Arguments>
        <views:View1 />
      </x:Arguments>
    </NavigationPage>
  </MasterDetailPage.Detail>
</MasterDetailPage>

视图 1:

public View1()
{
  //NavigationPage.SetHasNavigationBar(this,false);
  InitializeComponent();
}

当我在 MasterBehavior 设置为默认值的情况下启动后进行选择时,我现在看到汉堡菜单。

我可以在我的解决方案中添加或验证什么来模拟 MasterBehavior 设置为 Popup 的 Xamarin 示例行为吗?

尝试做这样的事情:

public partial class ShellView : MasterDetailPage, IMasterDetailPageOptions
{
    public ShellView()
    {
        InitializeComponent();
    }

    public bool IsPresentedAfterNavigation
    {
        get { return Device.Idiom != TargetIdiom.Phone; }
    }
}

问题在这里:var task = NavigationService.NavigateAsync("MainPage/View1");

由于要显示汉堡包菜单,因此需要将 Detail 包装在 NavigationPage 中。所以先注册一个NavigationPage用于导航

Container.RegisterTypeForNavigation<NavigationPage>();

然后将其添加到您的导航路径中:

var task = NavigationService.NavigateAsync("MainPage/NavigationPage/View1");

此外,您可以删除母版页中的详细信息标记 XAML,因为导航服务会为您构建它。