【问题标题】:Xamarin.Forms Prism app not showing Master-Detail navigation barXamarin.Forms Prism 应用程序未显示主从导航栏
【发布时间】:2017-01-15 05:38:13
【问题描述】:

我正在尝试在我的 Xamarin.Forms 应用程序中配置主从导航,而我的 UWP 目标在 Windows 10 上运行。

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

启动:

选择汉堡图标:

进行选择:

在我的 prism 应用中,我导航到 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 设置为 Default 进行选择时,我现在看到了汉堡菜单。

我是否可以在我的解决方案中添加或验证任何内容,以模拟将 MasterBehavior 设置为 Popup 的 Xamarin 示例行为?

【问题讨论】:

    标签: xamarin navigation xamarin.forms prism master-detail


    【解决方案1】:

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

    由于您想显示汉堡菜单,您需要将详细信息包装在 NavigationPage 中。所以先注册一个 NavigationPage 进行导航

    Container.RegisterTypeForNavigation&lt;NavigationPage&gt;();

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

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

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

    【讨论】:

      【解决方案2】:

      尝试做这样的事情:

      public partial class ShellView : MasterDetailPage, IMasterDetailPageOptions
      {
          public ShellView()
          {
              InitializeComponent();
          }
      
          public bool IsPresentedAfterNavigation
          {
              get { return Device.Idiom != TargetIdiom.Phone; }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-10-06
        • 1970-01-01
        • 2013-04-12
        • 2016-09-02
        • 1970-01-01
        • 2015-09-19
        • 1970-01-01
        • 1970-01-01
        • 2014-09-19
        相关资源
        最近更新 更多