c# - object does not contain a definition for Navigated
问题描述
im trying to get a xaml island navigation working in my core wpf app and get the error for:
ContentFrame.Content.Navigated += On_Navigated;
thats my mainwindow.cs:
public partial class MainWindow
{
private NavigationView NavView;
public MainWindow()
{
InitializeComponent();
}
// List of ValueTuple holding the Navigation Tag and the relative Navigation Page
private readonly List<(string Tag, Type Page)> _pages = new List<(string Tag, Type Page)>
{
("home", typeof(startPage)),
("other", typeof(otherPage)),
};
private void NavView_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
NavView = MyNavView.Child as NavigationView;
// Add handler for ContentFrame navigation.
ContentFrame.Navigated += On_Navigated;
// NavView doesn't load any page by default, so load home page.
NavView.SelectedItem = NavView.MenuItems[0];
NavView_Navigate("home", new EntranceNavigationTransitionInfo());
}
private void NavView_ItemInvoked(NavigationView sender,
NavigationViewItemInvokedEventArgs args)
{
if (args.IsSettingsInvoked == true)
{
NavView_Navigate("settings", args.RecommendedNavigationTransitionInfo);
}
else if (args.InvokedItemContainer != null)
{
var navItemTag = args.InvokedItemContainer.Tag.ToString();
NavView_Navigate(navItemTag, args.RecommendedNavigationTransitionInfo);
}
}
private void NavView_SelectionChanged(NavigationView sender,
NavigationViewSelectionChangedEventArgs args)
{
if (args.IsSettingsSelected == true)
{
NavView_Navigate("settings", args.RecommendedNavigationTransitionInfo);
}
else if (args.SelectedItemContainer != null)
{
var navItemTag = args.SelectedItemContainer.Tag.ToString();
NavView_Navigate(navItemTag, args.RecommendedNavigationTransitionInfo);
}
}
private void NavView_Navigate(string navItemTag, NavigationTransitionInfo transitionInfo)
{
Type _page = null;
if (navItemTag == "settings")
{
_page = typeof(SettingsPage);
}
else
{
var item = _pages.FirstOrDefault(p => p.Tag.Equals(navItemTag));
_page = item.Page;
}
var preNavPageType = ContentFrame.Content?.GetType();
// Only navigate if the selected page isn't currently loaded.
if (!(_page is null) && !Type.Equals(preNavPageType, _page))
{
//ContentFrame.Navigate(_page, null, transitionInfo);
}
}
private void On_Navigated(object sender, NavigationEventArgs e)
{
if (ContentFrame.Content?.GetType() == typeof(SettingsPage))
{
// SettingsItem is not part of NavView.MenuItems, and doesn't have a Tag.
NavView.SelectedItem = (NavigationViewItem)NavView.SettingsItem;
NavView.Header = "Settings";
}
else if (ContentFrame.Content != null)
{
var item = _pages.FirstOrDefault(p => p.Page == e.SourcePageType);
NavView.SelectedItem = NavView.MenuItems
.OfType<NavigationViewItem>()
.First(n => n.Tag.Equals(item.Tag));
NavView.Header =
((NavigationViewItem)NavView.SelectedItem)?.Content?.ToString();
}
}
}
thats the xaml:
<Window x:Class="GUI_Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:GUI_Test"
mc:Ignorable="d"
xmlns:xamlHost="clr-namespace:Microsoft.Toolkit.Wpf.UI.XamlHost;assembly=Microsoft.Toolkit.Wpf.UI.XamlHost"
Title="MainWindow" Height="450" Width="800">
<Grid>
<xamlHost:WindowsXamlHost x:Name="MyNavView" InitialTypeName="Windows.UI.Xaml.Controls.NavigationView" />
<ScrollViewer>
<Frame x:Name="ContentFrame" Padding="12,0,12,24" IsTabStop="True"/>
</ScrollViewer>
</Grid>
Most of the code come from: https://docs.microsoft.com/en-us/windows/uwp/design/controls-and-patterns/navigationview, all I did was throwing things out like keyboard navigation and using in the MainWindow.xaml
<xamlHost:WindowsXamlHost x:Name="MyNavView" InitialTypeName="Windows.UI.Xaml.Controls.NavigationView" />
Hope someone has stuff like this already done and could help me to get the nav running in core WPF.
thank you
Updated the code and getting now no errors anymore.
private void On_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
NavView.IsBackEnabled = ContentFrame.CanGoBack;
if (ContentFrame.Content?.GetType() == typeof(SettingsPage))
{
// SettingsItem is not part of NavView.MenuItems, and doesn't have a Tag.
NavView.SelectedItem = (NavigationViewItem)NavView.SettingsItem;
NavView.Header = "Settings";
}
else if (ContentFrame.Content != null)
{
var item = _pages.FirstOrDefault(p => p.Page == e.Content?.GetType());
NavView.SelectedItem = NavView.MenuItems
.OfType<NavigationViewItem>()
.First(n => n.Tag.Equals(item.Tag));
NavView.Header =
((NavigationViewItem)NavView.SelectedItem)?.Content?.ToString();
}
}
解决方案
该Navigated
事件在其上Frame
而不是在其上Content
:
ContentFrame.Navigated += On_Navigated;
推荐阅读
- python - 不支持在 python 中使用 pem 文件加密 json,提供 RSA 密钥格式
- c++ - grpc 找不到 protobuf 库
- react-native - React Native、IBECONS、RSSI 值到距离的转换
- python - 无法将自定义值传递给 AWS Lambda 函数
- anylogic - selectOutput5,链接到单选按钮
- android - 我的 Json 包含图像我想在图像视图中显示它
- css - 使用 Vue 过渡组来动画 CSS 网格中的列变化
- reactjs - 反应路由器,当搜索参数更改时防止滚动到顶部
- javascript - 如何在不使用 MathJax 刷新页面的情况下动态呈现 Tex 和 LaTex 公式?
- java - 在 GUI 更新之间创建延迟