Using NavigationView in WPF Project

812 Views Asked by At

I want to use navigationView with xaml islands in a WPF project. I added the NavigationView to the project. There is no problem in terms of appearance. If I create a new page in code-behind I can switch between pages.

But when I want to open an attached page in my project, I encounter the following error.

C# Code

        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 Code

<xamlHost:WindowsXamlHost x:Name="Host"
                              InitialTypeName="Windows.UI.Xaml.Controls.NavigationView"
                              ChildChanged="Host_ChildChanged"/>

Error

System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'
1

There are 1 best solutions below

13
Valentyn Bondarenko On

This might not be a problem in you code at all. According to this article, it may be mixed configuration DDLs, you project settings, you .NET install or even an external .dll.

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

If this is definitely happening because of the WPF Window, make sure you have double-checked these leads, because this exception is caused by protected memory allocated from everywhere on you computer.