Search code examples
c#wpfuwpxaml-islands

frame does not contain a definition of SourcePageType


Im trying to setup a navigation for my wpf app with xaml islands but cant navigate between the views. I get an error:

frame does not contain a definition of SourcePageType

MainWindow.cs:

    private void On_Navigated(object sender, NavigationEventArgs e)
    {
        if (NavView.Child is NavigationView navigationView) {
            // NavView.IsBackEnabled = ContentFrame.CanGoBack;
            navView = navigationView;
            if (ContentFrame.Content?.GetType() == typeof(SettingsPage))
            {
                // SettingsItem is not part of NavView.MenuItems, and doesn't have a Tag.
                navView.SelectedItem = (NavigationViewItem)navView.SettingsItem;
                navView.Header = "Settings";
            }
            else if (ContentFrame.Content != null)
            {
                var item = _pages.FirstOrDefault(p => p.Page == e.Content);

                navView.SelectedItem = navView.MenuItems
                    .OfType<NavigationViewItem>()
                    .First(n => n.Tag.Equals(item.Tag));

                navView.Header =
                    ((NavigationViewItem)navView.Content)?.Content?.ToString();
            }
    } 
}

I think there is something wrong with the NavView_Loaded method to catch the selected item? Arnt there any examples for navigations in xaml islands?

thank you


Solution

  • The System.Windows.Controls.Frame class in WPF has no SourcePageType property and this has nothing to do with Xaml Islands.

    If you want to know the type of the Page that is currently loaded into the Frame, you could call the GetType() method on its Content property:

    private void On_Navigated(object sender, NavigationEventArgs e)
    {
    
        if (ContentFrame.Content?.GetType() == typeof(SettingsPage))
        {
            navView.SelectedItem = (NavigationViewItem)navView.SettingsItem;
            navView.Header = "Settings";
        }
        else if (ContentFrame.Content != null)
        {
            ...
        }
    }