簡體   English   中英

在 WPF 項目中使用 NavigationView

[英]Using NavigationView in WPF Project

我想在 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.'

這在您的代碼中可能根本不是問題。 根據本文,它可能是混合配置 DDL、您的項目設置、您的 .NET 安裝甚至是外部 .dll。

嘗試讀取或寫入受保護的內存。 這通常表明其他內存已損壞

如果這確實是由於 WPF 窗口而發生的,請確保您已仔細檢查這些線索,因為此異常是由從您計算機上的任何地方分配的受保護內存引起的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM