You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在UWP应用中设置NavigationView对应标签页的实际展示内容

解决NavigationView标签对应主体内容设置的方案

首先明确你混淆的两个属性:NavigationViewItem.Content是菜单选项的显示内容,而NavigationView本身的Content属性才是主体内容的承载区域,按以下步骤实现即可:

  • 第一步:为每个标签对应的独立页面创建Page类型文件,比如首页对应HomePage.xaml、设置页对应SettingsPage.xaml,每个页面内编写你需要展示的主体内容。
  • 第二步:修改主页面的XAML布局,在NavigationView.Content中嵌入Frame控件作为页面切换的容器,示例代码如下:
<NavigationView x:Name="MainNav" SelectionChanged="MainNav_SelectionChanged">
    <!-- 自定义菜单项 -->
    <NavigationView.MenuItems>
        <NavigationViewItem Content="首页" Tag="Home" />
        <NavigationViewItem Content="数据统计" Tag="Statistics" />
        <NavigationViewItem Content="设置" Tag="Settings" />
    </NavigationView.MenuItems>
    <!-- 主体内容区域,用Frame承载各标签对应的页面 -->
    <NavigationView.Content>
        <Frame x:Name="ContentFrame" />
    </NavigationView.Content>
</NavigationView>
  • 第三步:在主页面的后台代码中处理SelectionChanged事件,根据选中的菜单项切换Frame加载的页面,示例代码如下:
private void MainNav_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
{
    if (args.SelectedItem is not NavigationViewItem selectedItem) return;
    
    var targetPage = selectedItem.Tag.ToString() switch
    {
        "Home" => typeof(HomePage),
        "Statistics" => typeof(StatisticsPage),
        "Settings" => typeof(SettingsPage),
        _ => null
    };

    if (targetPage != null && ContentFrame.CurrentSourcePageType != targetPage)
    {
        ContentFrame.Navigate(targetPage);
    }
}

可选优化点

  • 如需跳转时传参,使用Navigate方法的重载:ContentFrame.Navigate(targetPage, 自定义参数对象)
  • 如需支持页面回退,可开启NavigationView.IsBackButtonVisible属性,关联ContentFrame.CanGoBack状态实现回退逻辑
  • 该方案对原生UWP NavigationView和WinUI 2/3版本的NavigationView均适用

内容的提问来源于stack exchange,提问作者Seaender07

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 16:54:05