c# - 页面内容不显示在框架中,而是显示页面对象名称

标签 c# uwp

我正在尝试在 UWP 中实现基本导航,全部根据 https://learn.microsoft.com/en-us/windows/uwp/design/basics/navigate-between-two-pages .

我已使示例尽可能简单。制作了一个主应用程序窗口,其中有一个框架和 2 个按钮。单击按钮将导航框架以分别显示页面 1 或 2 的内容。该页面包含简单的文本 block

代码编译并运行,导航框架工作,但是它显示的不是页面内容(即 XAML 内容),而是页面对象的名称(即 ConceptTestApp.Page1 和 ConceptTestApp.Page2),而不是页面的内容(即文本 block “这是第 1 页”或“这是第 2 页”)。

我看不出我在这里做错了什么。非常感谢任何帮助。

应用程序 XAML

<Window x:Class="ConceptTestApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ConceptTestApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <StackPanel Orientation="Vertical">
            <StackPanel Orientation="Horizontal">
                <Button x:Name="btnPage1" FontSize="14" Height="20" Width="120" Content="Ga naar Pagina 1" Click="BtnPage1_Click" Margin="10,0"  />
                <Button x:Name="btnPage2" FontSize="14" Height="20" Width="120" Content="Ga naar Pagina 2" Click="BtnPage2_Click" Margin="10,0"  />
            </StackPanel>
            <Frame x:Name="rootFrame" HorizontalAlignment="Left" Margin="0" VerticalAlignment="Top" NavigationUIVisibility="Hidden" Width="600" Height="450"/>
        </StackPanel>

    </Grid>
</Window>

应用程序代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ConceptTestApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.rootFrame.Navigate(typeof(Page1));

        }


        private void BtnPage1_Click(object sender, RoutedEventArgs e)
        {
            // go to Pagina 1

            this.rootFrame.Navigate(typeof(Page1));
        }


        private void BtnPage2_Click(object sender, RoutedEventArgs e)
        {
            // go to Pagina 2

            this.rootFrame.Navigate(typeof(Page2));
        }
    }
}

第 1 页 XAML

<Page x:Class="ConceptTestApp.Page1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:ConceptTestApp"
      mc:Ignorable="d" 
      d:DesignHeight="400" d:DesignWidth="600"
      Title="Page1">

    <Grid>
        <TextBlock x:Name="pageTitle" FontSize="36" >This is Page 1</TextBlock>
    </Grid>

</Page>

第 2 页 XAML

<Page x:Class="ConceptTestApp.Page2"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:ConceptTestApp"
      mc:Ignorable="d" 
      d:DesignHeight="400" d:DesignWidth="600"
      Title="Page2">

    <Grid>
        <TextBlock x:Name="pageTitle" Text="This is Page 2" FontSize="24" />
    </Grid>
</Page>

(两个页面都没有任何隐藏代码,只有默认的初始化代码)

最佳答案

看起来您正在执行 WPF 应用程序而不是 UWP 应用程序。如果您使用 MainPage,这在 UWP 应用程序中确实有效

例如,

<Page
    x:Class="PageIssueSOF.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:PageIssueSOF"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    >

    <Grid>
        <StackPanel Orientation="Vertical">
            <StackPanel Orientation="Horizontal">
                <Button x:Name="btnPage1" FontSize="14" Height="20" Width="120" Content="Ga naar Pagina 1" Click="BtnPage1_Click" Margin="10,0"  />
                <Button x:Name="btnPage2" FontSize="14" Height="20" Width="120" Content="Ga naar Pagina 2" Click="BtnPage2_Click" Margin="10,0"  />
            </StackPanel>
            <Frame x:Name="rootFrame" HorizontalAlignment="Left" Margin="0" VerticalAlignment="Top" Width="600" Height="450"/>
        </StackPanel>

    </Grid>
</Page>

附带说明 NavigationUIVisibility="Hidden" 在 UWP 中不可用,因此已在上面的示例中删除。

关于c# - 页面内容不显示在框架中,而是显示页面对象名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58156548/

相关文章:

c# - 从 UWP 项目引用 .NETStandard 2.0 项目

c# - UWP 应用程序无法使用 StartupTask API 登录时启动

c# - MediaCapture StartPreviewAsync 失败

c# - 如何用 C# 中的函数语句替换 for 循环?

c# - UWP 中的 itemscontrol 不绑定(bind)到 observablecollection 项目的坐标

c# - 指示命令行激活已成功完成UWP

c# - Entity Framework 代码优先 : cycles or multiple cascade paths

c# - 如何使用属性更改 TimePicker 格式

c# - 为什么 Windsor 只能拦截虚拟或接口(interface)方法?

c# - 如何将项目添加到动态创建的select(html)控件