【问题标题】:Using NavigationView in WPF Project在 WPF 项目中使用 NavigationView
【发布时间】:2021-10-03 00:19:16
【问题描述】:

我想在 WPF 项目中使用带有 xaml 岛的 navigationView。我将 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.'

【问题讨论】:

    标签: c# wpf xaml-islands


    【解决方案1】:

    这在您的代码中可能根本不是问题。根据这篇文章,它可能是混合配置 DDL、项目设置、.NET 安装,甚至是外部 .dll。

    Attempted to read or write protected memory. This is often an indication that other memory is corrupt

    如果这确实是由于 WPF 窗口而发生的,请确保您已经仔细检查了这些线索,因为此异常是由计算机上从任何地方分配的受保护内存引起的。

    【讨论】:

    • frame.Navigate(typeof(PageName)); 它给出了这部分的错误。如果设置frame.Content = new Page();如果我这样做,没有问题。
    • 设置中启用非托管代码调试时,错误变化如下。 Exception thrown at 0x7B3F9693 (Windows.UI.Xaml.dll) in NavigationViewSample.exe: 0xC0000005: Access violation reading location 0x00000000.
    • 尝试将 Dispatcher.RunAsync 放在这一行附近。就像这样stackoverflow.com/a/46922640/9192934
    • 感谢@bondarenko,错误消失了,但不幸的是它没有显示相关页面。什么可能导致问题?
    • _=Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() =&gt;{frame.Navigate(typeof(Configure)); }); 抱歉,问题没有解决。我是这样做的。它给出了一个错误。抛出异常:NavigationViewSample.dll 中的“System.InvalidOperationException”
    猜你喜欢
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多