【问题标题】:Navigation with nested frames使用嵌套框架导航
【发布时间】:2019-08-16 03:06:53
【问题描述】:

这是我的问题情况的简化示例。

MainPage.xaml

<page
    ...
    xmlns:helpers="using:MyNamespace.Helpers"
    xmlns:views="using:MyNamespace.Views"
    ...>

    <NavigationView Name="MainNav"
                    PaneDisplayMode="LeftCompact"
                    ...>
        <NavigationView.MenuItems>
            <NavigationViewItem Content="OtherPage"
                                helpers:NavHelper.NavigateTo="views:OtherPage">
                </NavigationViewItem>
                ... other NavigationViewItem's...
        </NavigationView.MenuItems>

        <Frame Name="MainFrame".../>   
</page>

MainPage.xaml.cs

namespace MyNamespace
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            MainNav.ItemInvoked += Navigate.NavView_ItemInvoked;
        }
        ...
    }
}

OtherPage.xaml - 在视图文件夹中

<page
    ...
    xmlns:helpers="using:MyNamespace.Helpers"
    xmlns:views="using:MyNamespace.Views"
    ...>

    <NavigationView Name="OtherNav"
                    PaneDisplayMode="Top"
                    ...>
        <NavigationView.MenuItems>
            <NavigationViewItem Content="Other Page"
                                helpers:NavHelper.NavigateTo="views:OtherPage_1">
                </NavigationViewItem>
                ... other NavigationViewItem's...
        </NavigationView.MenuItems>

        <Frame Name="OtherFrame".../>   
</page>

OtherPage.xaml.cs

namespace MyNamespace.Views
{
    public sealed partial class OtherPage : Page
    {
        public OtherPage()
        {
            this.InitializeComponent();
            OtherNav.ItemInvoked += Navigate.NavView_ItemInvoked;
        }
        ...
    }
}

Navigate.cs - 在服务文件夹中

namespace MyNamespace.Services
{
    static class Navigate
    {
        public static void NavView_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
        {
            \\ do some navigation logic
            ...
            private _frame = ???;
            ...

            _frame.Navigate(_page, null, transitionInfo);
        }

好吧,在所有示例代码之后,这是我的问题。在NavView_ItemInvoked 事件处理程序中,我需要能够根据被调用的NavigationViewItem 设置_frameMainFrameOtherFrame

注意:我可能会在我的应用程序中摆脱嵌套框架,但我首先想弄清楚这一点,因为我不想浪费学习机会。

【问题讨论】:

    标签: c# xaml uwp navigation


    【解决方案1】:

    当触发ItemInvoked事件时,可以从中获取NavigationView实例。NavigationView有一个Content属性,它实际上代表了当前触发的navigationView中的Frame。所以可以通过以下方式获取MainFrame或OtherFrame Frame.name

    static class MyNavigate
        {​
            public static void NavView_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)​
            {​
                Frame currentFrame = sender.Content as Frame;​
                String name = currentFrame.Name;​
                if (name == "OtherFrame")​
                {​
                    currentFrame.Navigate(.......);​
                }​
                else {​
                    currentFrame.Navigate(.......);​
                }​
            }​
        }
    

    【讨论】:

    • 谢谢@Faywang。我的脑海里有这个框架会被 Content 包含,比如sender.Content.myFrame。显然,我错了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-01
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 2019-09-17
    相关资源
    最近更新 更多