我想在 WPF 项目中将 navigationView 与 xaml 岛一起使用。我将 NavigationView 添加到项目中。外观方面没有问题。如果我在代码隐藏中创建一个新页面,我可以在页面之间切换。
但是当我想在我的项目中打开一个附加页面时,我遇到了以下错误。
C# 代码
private UIControls.Frame frame;
private Media.FontFamily segoeFontFamily;
public MainWindow()
{
InitializeComponent();
segoeFontFamily = new Media.FontFamily("Segoe MDL2 Assets");
}
private void Host_ChildChanged(object sender, EventArgs e)
{
try
{
WindowsXamlHost host = (WindowsXamlHost)sender;
if (host.Child is UIControls.NavigationView navigationView)
{
var configureItem = new UIControls.NavigationViewItem()
{
Content = "Configure",
Icon = new UIControls.FontIcon()
{
FontFamily = segoeFontFamily,
Glyph = "\uE719",
}
};
var filterItem = new UIControls.NavigationViewItem()
{
Content = "Filter",
Icon = new UIControls.FontIcon()
{
FontFamily = segoeFontFamily,
Glyph = "\uE8C7",
}
};
navigationView.MenuItems.Add(configureItem);
navigationView.MenuItems.Add(filterItem);
frame = new UIControls.Frame();
navigationView.Content = frame;
navigationView.SelectionChanged += NavigationView_SelectionChanged;
}
}
catch (Exception)
{
}
}
private void NavigationView_SelectionChanged(UIControls.NavigationView sender, UIControls.NavigationViewSelectionChangedEventArgs args)
{
try
{
if (args.SelectedItem is UIControls.NavigationViewItem item)
{
switch (item.Content)
{
case "Configure":
frame.Navigate(typeof(Configure));
break;
case "Filter":
frame.Navigate(typeof(CanBusFilterPage));
break;
default:
break;
}
}
}
catch (Exception)
{
}
}
Xaml 代码
<xamlHost:WindowsXamlHost x:Name="Host"
InitialTypeName="Windows.UI.Xaml.Controls.NavigationView"
ChildChanged="Host_ChildChanged"/>
错误
System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'