gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:52:23 27 4
gpt4 key购买 nike

我正在尝试在 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));
}
}
}

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>

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

最佳答案

看起来您在做 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com