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

C# 页面内容不会显示在框架中,而是显示页面对象名称,c#,uwp,C#,Uwp,我正试图在UWP中实现基本的导航,这一切都是根据我的想法 我已经尽可能地简化了这个例子。制作了一个主应用程序窗口,其中有一个框架和两个按钮。单击按钮将导航框架,分别显示第1页或第2页的内容。页面包含简单的文本块 代码编译并运行,在框架中导航,但是它显示的不是页面内容(即XAML内容),而是页面对象的名称(即ConceptTestApp.Page1 resp.ConceptTestApp.Page2),而不是页面内容(即文本块“This is page 1 resp.”This is page 2

我正试图在UWP中实现基本的导航,这一切都是根据我的想法

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

代码编译并运行,在框架中导航,但是它显示的不是页面内容(即XAML内容),而是页面对象的名称(即ConceptTestApp.Page1 resp.ConceptTestApp.Page2),而不是页面内容(即文本块“This is page 1 resp.”This is page 2')

我看不出我做错了什么。非常感谢您的帮助

应用程序XAML


应用程序代码

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间概念TestApp
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
this.rootFrame.Navigate(typeof(Page1));
}
私有无效BtnPage1_单击(对象发送者,路由目标e)
{
//去帕吉纳1号
this.rootFrame.Navigate(typeof(Page1));
}
私有无效BtnPage2_单击(对象发送者,路由目标e)
{
//去帕吉纳2号
this.rootFrame.Navigate(typeof(Page2));
}
}
}
第1页XAML


这是第一页
第2页XAML



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

看起来您正在使用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>



另一方面,UWP中没有
NavigationUIVisibility=“Hidden”
,因此在上面的示例中已被删除。

您似乎创建了一个WPF应用程序。在WPF中,当您使用
Frame.Navigate
时,它与UWP不同,您需要传递页面对象,而不是Type

private void BtnPage1_Click(object sender, RoutedEventArgs e)
{
    // go to Pagina 1
    this.rootFrame.Navigate(new Page1());
}


private void BtnPage2_Click(object sender, RoutedEventArgs e)
{
    // go to Pagina 2
    this.rootFrame.Navigate(new Page2());
}

你确定网页是否真的被导航到了吗?可能尝试更改页面的背景色,看看页面是否真的被导航。欢迎使用堆栈溢出。我想这是我第一次看到有人为他们的第一个问题发布MCVE。干得好!:)
<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>
<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>

<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>
private void BtnPage1_Click(object sender, RoutedEventArgs e)
{
    // go to Pagina 1
    this.rootFrame.Navigate(new Page1());
}


private void BtnPage2_Click(object sender, RoutedEventArgs e)
{
    // go to Pagina 2
    this.rootFrame.Navigate(new Page2());
}