无法在帧中的xaml之间导航?

时间:2016-09-07 13:07:47

标签: c# xaml win-universal-app

我遇到了问题。为什么不导航到其他xaml?哪里错了? 所以,我试图让它可以在帧中的两个或更多xaml之间导航。

这是链接:https://github.com/Englbach/MutiViewInRootPage

 <SplitView.Content>
    <!-- OnNavigatingToPage we synchronize the selected item in the nav menu with the current page.
         OnNavigatedToPage we move keyboard focus to the first item on the page after it's loaded. -->
    <Frame x:Name="AppShellFrame">
       <Frame.ContentTransitions>
           <TransitionCollection>
               <NavigationThemeTransition>
                   <NavigationThemeTransition.DefaultNavigationTransitionInfo>
                      <EntranceNavigationTransitionInfo />
                   </NavigationThemeTransition.DefaultNavigationTransitionInfo>
               </NavigationThemeTransition>
           </TransitionCollection>
       </Frame.ContentTransitions>
   </Frame>
</SplitView.Content>
public static AppShell Current = null;

public List<NavMenuItem> NavList { get; } = new List<NavMenuItem>(new[]
{
     new NavMenuItem()
     {
          Symbol = Symbol.Add,
          Label = "Add feed",
          DestPage = typeof(RootPages),
          Arguments = typeof(AddFeedView)
     },
     new NavMenuItem()
     {
          Symbol = Symbol.Edit,
          Label = "Edit feeds",
          DestPage = typeof(RootPages),
          Arguments = typeof(EditFeedView)
     }
});
public AppShell()
{
     this.InitializeComponent();
     Current = this;
}

private void NavMenuList_ItemInvoked(object sender, ListViewItem e)
{
     NavMenuList.SelectedIndex = -1;
     var item = (NavMenuItem)((NavMenuListView)sender).ItemFromContainer(e);
     if(item!=null)
     {
          AppFrame.Navigate(typeof(RootPages), item.Arguments);
     }
}

1 个答案:

答案 0 :(得分:0)

您可能会跟随官方Navigation menu (XAML) sample设计您的布局。

您的演示中有两个小问题。

  1. 每次单击NavMenuList中的项目时,都应触发AppShell.xaml.cs中的NavMenuList_ItemInvoked事件。在这种情况下,您一次又一次地导航到RootPages,并一起将导航参数(item.Arguments)传递给RootPages,就像这个AppFrame.Navigate(typeof(RootPages), item.Arguments);一样,参数实际上就是你的目的地。
  2. 您可以像这样修改代码:

    private void NavMenuList_ItemInvoked(object sender, ListViewItem e)
    {
        //NavMenuList.SelectedIndex = -1;
        var item = (NavMenuItem)((NavMenuListView)sender).ItemFromContainer(e);
        if (item != null)
        {
            //AppFrame.Navigate(typeof(RootPages), item.Arguments);
            if (item.DestPage != null &&
                item.DestPage != this.AppFrame.CurrentSourcePageType)
            {
                this.AppFrame.Navigate(item.DestPage, item.Arguments);
            }
        }
    }
    
    1. 然后是第二个问题,正如我所说,Arguments应该是导航参数,例如,我导航到AddFeedView页面,我想发送消息“balalala”,然后我们可以像这样编码:AppFrame.Navigate(typeof(AddFeedView), "balalala");。这意味着,您与DestPageArguments混淆了。
    2. 您可以像这样修改NavList

      public List<NavMenuItem> NavList { get; } = new List<NavMenuItem>(new[]
      {
          new NavMenuItem()
          {
              Symbol = Symbol.Add,
              Label = "Add feed",
              DestPage = typeof(AddFeedView),
              Arguments = typeof(RootPages)
          },
          new NavMenuItem()
          {
              Symbol = Symbol.Edit,
              Label = "Edit feeds",
              DestPage = typeof(EditFeedView),
              Arguments = typeof(RootPages)
          }
      });
      

      此外,如果您希望AppFrame默认情况下首先导航到RootPages,则可以这样编码:

      public AppShell()
      {
          this.InitializeComponent();
          Current = this;
          AppFrame.Navigate(typeof(RootPages));
      }