c# - 使用嵌套框架导航
问题描述
这是我的问题情况的简化示例。
主页.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>
其他页面.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
事件处理程序中,我需要能够_frame
根据NavigationViewItem
被调用进行设置;要么MainFrame
要么OtherFrame
。
注意:我可能会远离我的应用程序中的嵌套框架,但我首先想弄清楚这一点,因为我不想浪费学习机会。
解决方案
触发 ItemInvoked 事件时,可以从中获取 NavigationView 实例。NavigationView 有一个Content属性,它实际上代表了当前触发的 navigationView 中的 Frame。因此可以通过 Frame.name 获取 MainFrame 或OtherFrame。
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(.......);
}
}
}
推荐阅读
- django - Django - 查询集中两个特定字段的划分
- java - 如何使用 Java API 根据多个字段对弹性搜索记录进行排序?
- mongodb - Meteor 助手不能按预期在模板上工作
- node.js - 使用 Stripe 收集电子邮件地址
- python - 行 = i+1 在 tkinter
- python - 用于散热的 Python numpy 矢量化
- thymeleaf - 将照片从资源/静态/图像移动到本地驱动器
- javascript - 烧瓶错误“请求的 URL 不允许该方法。” 使用 Ajax 后调用时
- python-3.x - Python - 无法使用 Selenuim 通过文本定位元素
- html - 来自简单 HTML 代码的图像未显示在浏览器中