0

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.'
voltac
  • 33
  • 7

1 Answers1

0

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.

  • `frame.Navigate(typeof(PageName));` It gives the error in this part. If setting `frame.Content = new Page();` If I do it like this, there is no problem. – voltac Jul 27 '21 at 08:46
  • When Enable unmanaged code debugging was activated in the settings, the error changed as follows. `Exception thrown at 0x7B3F9693 (Windows.UI.Xaml.dll) in NavigationViewSample.exe: 0xC0000005: Access violation reading location 0x00000000.` – voltac Jul 27 '21 at 08:54
  • Try to put Dispatcher.RunAsync around this line. Just like this https://stackoverflow.com/a/46922640/9192934. – Valentyn Bondarenko Jul 27 '21 at 08:58
  • Thanks @bondarenko, the error is gone but unfortunately it doesn't bring up the relevant page. What could cause the problem? – voltac Jul 27 '21 at 10:13
  • `_=Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() =>{frame.Navigate(typeof(Configure)); });` Sorry, problem not solved. I did it this way. It gives an error. Exception thrown: 'System.InvalidOperationException' in NavigationViewSample.dll – voltac Jul 27 '21 at 10:33
  • I am not a Xamarin developer, and I can't be certain. In this way, try to post a different question, describe what you expected and what has been rendered instead. It could be some bug(s) in logic. – Valentyn Bondarenko Jul 27 '21 at 10:34
  • Do you have any information about the solution? – voltac Jul 27 '21 at 10:38
  • I do. See remarks section, https://learn.microsoft.com/en-us/dotnet/api/system.invalidoperationexception?view=net-5.0. 'A method that attempts to manipulate the UI from a thread that is not the main or UI thread'. It's recommended to read the exception message. – Valentyn Bondarenko Jul 27 '21 at 10:40
  • Unfortunately, there is no RunAsync method as in the first link you mentioned. I guess that's why there is a problem with threads. – voltac Jul 27 '21 at 10:48
  • RunAsync works in a different thread pool. To ensure thread safety, you can use InvokeAsync. – Valentyn Bondarenko Jul 27 '21 at 11:04
  • I used the InvokeAsync method. I am getting the same error as the original post I received. The problem remains the same. Additionally, thank you for your time. – voltac Jul 27 '21 at 11:08
  • Then apparently this is related to the thread safety problem. You are welcome. – Valentyn Bondarenko Jul 27 '21 at 11:35
  • Thank you. I will investigate a little more. If I find a solution I will share it here. – voltac Jul 27 '21 at 11:58