1

I'm using XAML Islands to display a UWP NavigationView control from within a .NET Core WPF application window (I don't need the whole navigation infrastructure -- I just want the navigation view control). I also have a class derived from Page called Home that I want to use to display content in the control's frame. When I set the frame's content to an object of the Home class, I see the Home object's type name in the frame's content, as if ToString() was being called on the object rather than getting its content rendered. What am I missing?

using System.Windows;
using MyApp.Wpf.Pages;
using Windows.UI.Xaml.Controls;

namespace MyApp.Wpf
{
    public partial class MainWindow : Window
    {
        private Frame navigationFrame;

        public MainWindow()
        {
            InitializeComponent();
            uwpNavigationView.ChildChanged += uwpNavigationView_ChildChanged;
        }

        private void uwpNavigationView_ChildChanged(object sender, System.EventArgs e)
        {
            if (uwpNavigationView.Child is NavigationView navigationView)
            {
                var homeItem = new NavigationViewItem()
                {
                    Content = "Home",
                    Icon = new FontIcon()
                    {
                        Glyph = "\uE80F",
                    }
                };
                navigationView.MenuItems.Add(homeItem);
                navigationFrame = new Frame();
                navigationView.Content = navigationFrame;
                navigationView.SelectionChanged += NavigationView_SelectionChanged;
            }
        }

        private void NavigationView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
        {
            if(args.SelectedItem is NavigationViewItem item)
            {
                var homePage = new Home();
                navigationFrame.Content = homePage; // homepage.ToString() rendered here?
            }
        }
    }
}

JeffFerguson
  • 2,952
  • 19
  • 28

1 Answers1

1

A UWP Windows.UI.Xaml.Controls.Frame cannot render WPF System.Windows.Controls.Page elements.

You should set the Content property to a UWP Windows.UI.Xaml.Controls.Page or use a WPF System.Windows.Controls.Frame.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • Unfortunately, using a WPF `System.Windows.Controls.Frame` puts the text `"System.Windows.Controls.Frame"` in the frame before and after I load the `Home()` page in as the frame's content. @mm8 I can see that `InitializeComponent()` is being called in the `Home` constructor, which derives from `System.Windows.Controls.Page`, so the `Home()` object seems to be loading properly. I just don't think it's getting in the visual tree. – JeffFerguson Jul 10 '20 at 12:53
  • @JeffFerguson: Hmm...did you read my answer? You cannot display a WPF page in a UWP frame. It can't be rendered. – mm8 Jul 10 '20 at 13:04
  • I did, and I apologize for the confusion. As a remedy, I changed the `Frame`, per your suggestion, to a `System.Windows.Controls.Frame` so that I was using a WPF frame and not a UWP frame. Unfortunately, that gives me the result I mentioned above. At this point, I have a .NET Core WPF app, a UWP navigationView with XAML Islands, a WPF Frame, and a WPF Page. – JeffFerguson Jul 10 '20 at 13:33
  • @JeffFerguson: You still cannot use WPF elements, such as frames or pages, inside the XAML island. – mm8 Jul 10 '20 at 13:43
  • I see. In other words, once I start in on a XAML island within the visual tree, all of the children need to be UWP from there on down throughout all of its descendants for that part of the tree. – JeffFerguson Jul 10 '20 at 13:54