0

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

mm8
  • 163,881
  • 10
  • 57
  • 88

1 Answers1

0

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)
    {
        ...
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Thanks that did the trick, but now I got errors for: WindowsXamlHost does not contain a definition for Settingsitem, SelectedItem and MenuItems, Header. Ive updated the on_Navigate function. –  Feb 05 '20 at 09:17
  • @CodCase: Of couse the WindowsXamlHost itself hasn't got any of these properties. Its `Child` may have though depending on the value of `InitialTypeName`. – mm8 Feb 05 '20 at 12:18
  • ok now I think I have that. The Navigation is showing and is collabpseable but I still cant navigate to the pages. Ive updated my code to current version. –  Feb 05 '20 at 13:13
  • 1
    @CodCase: Please ask a new question if you have another issue. Your original question was about the `SourcePageType` property not being found. – mm8 Feb 05 '20 at 13:15