首页 > 解决方案 > 将 UWP NavigationView 内容设置为页面显示页面的类型名称

问题描述

我正在使用 XAML IslandsNavigationView从 .NET Core WPF 应用程序窗口中显示 UWP 控件(我不需要整个导航基础结构——我只需要导航视图控件)。我还有一个从 call 派生的类PageHome我想用它在控件的框架中显示内容。当我将框架的内容设置为Home类的对象时,我在框架的内容中看到Home对象的类型名称,就好像ToString()在对象上被调用而不是渲染其内容。我错过了什么?

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?
            }
        }
    }
}

标签: wpfuwpframenavigationviewxaml-islands

解决方案


UWPWindows.UI.Xaml.Controls.Frame无法呈现 WPFSystem.Windows.Controls.Page元素。

您应该将该Content属性设置为 UWPWindows.UI.Xaml.Controls.Page或使用 WPF System.Windows.Controls.Frame


推荐阅读