页面内容未显示在框架中,而是显示页面对象名称

时间:2019-09-29 15:16:44

标签: c# uwp

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

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

代码编译并运行,在框架中导航,但是它代替页面的内容(即XAML内容),而是显示页面对象的名称(即ConceptTestApp.Page1或ConceptTestApp.Page2)而不是页面内容(即文本块“ This is Page 1”或“ This is Page 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));
        }
    }
}

Page1 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>

Page2 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>

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

2 个答案:

答案 0 :(得分:2)

看起来您在执行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中不可用,因此在上面的示例中已将其删除。

答案 1 :(得分:0)

似乎您创建了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());
}