【问题标题】:Can't navigation between xaml in frame?无法在框架中的 xaml 之间导航?
【发布时间】:2016-09-07 13:07:46
【问题描述】:

我有问题。为什么它没有导航到其他 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);
     }
}

【问题讨论】:

  • 你的调试器告诉你什么?即您是否正在点击事件处理程序?是否正确检索项目?
  • 没有错误。当我单击 navmenuitem 上的每个项目但没有任何内容时。
  • 我想单击 navmenuitem 上必须导航到该框架中的其他 xaml 的每个项目
  • 例如:Frame 中的默认 RootPages。我单击添加提要,然后导航到 AddFeedView 被 RootPages 替换。
  • 它是否命中了您方法中设置的断点?

标签: c# xaml win-universal-app


【解决方案1】:

你可能是按照官方Navigation menu (XAML) sample来设计你的布局的。

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

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

你可以像这样修改这里的代码:

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 混淆了。

你可以像这样修改你的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));
}

【讨论】:

    猜你喜欢
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    相关资源
    最近更新 更多