【问题标题】:The contains of the window appears on the contains of the page at the same time: WPF窗口的包含同时出现在页面的包含:WPF
【发布时间】:2020-05-04 18:33:03
【问题描述】:

我真的是编程初学者,因此我面临很多错误。

在以下解决方案中,我有一个标识Window和一个更新用户信息Page,当我点击标识Window上的按钮时,窗口的包含和页面的包含同时出现,这是我既不想也无法解决的问题。我想要的是:当用户单击标识Window 上的按钮时,窗口的包含应该只显示页面的包含。非常感谢您的帮助!

窗口 XML 代码:

<Grid HorizontalAlignment="Center"
                VerticalAlignment="Center" 
                Margin="0,8" Width="385" >
        <Frame x:Name="Main"/>

        <StackPanel Margin="10 10 10 20" 
                    Orientation="Vertical"
                    VerticalAlignment="Center"
                    HorizontalAlignment="Center"
                    Width="357">
            <Label Content="User Name:"/>
            <TextBox Margin="0,0,3,0"/>
            <Label Content="Your Password"/>
            <PasswordBox Margin="0,0,3,0"/>
            <StackPanel Orientation="Horizontal" 
                        HorizontalAlignment="Center" 
                        Margin="10">
                <Button x:Name="ForUpdate"
                        Content="Match"
                        Click="Update"/>
            </StackPanel>
        </StackPanel>
    </Grid>

按钮事件处理程序C#代码:


    private void Update(object sender, RoutedEventArgs e)
     {
        Main.Content = new UpdateCio();
     }

页面 XML 代码:

<Grid>
    <StackPanel HorizontalAlignment="Center"
                VerticalAlignment="Center">
            <Label Content="this is test!"/>
    </StackPanel>
</Grid>

【问题讨论】:

  • 嗨,通常我们不会将一个窗口的内容切换到另一个窗口,而是打开一个新窗口并关闭(或不关闭)前一个窗口。对我来说,当我单击按钮时,您显示的代码失败了,所以我不确定 UpdateCio 是如何为您定义的。 **** UpdateCio updateCio = new UpdateCio();更新Cio.Show(); this.Close(); **** 当然,这取决于您要达到的目标。您想交换内容有什么特别的原因吗?
  • 谢谢您的回复!原因是:这个窗口是用于更新应用程序的数据(信息)的所有者。当他/她打开这个窗口时,需要输入密码,如果是真的,那么他可以更新他的信息。我只是使用了页面,以避免在应用程序中使用太多窗口。你明白我的意思吗?
  • 一段时间后我想我明白了,但我认为有一些重要的代码/xaml 没有显示,这使得它难以理解。 UpdateCio.xaml 是一个 Page 对吗?
  • 只有窗口包含类似的命名空间,我还没有发布它!我可以发布整个页面和窗口的包含,我应该吗?
  • 不,没关系,我想我明白了,我会发布一个答案,如果我没有得到它,你可以纠正我

标签: c# xml wpf


【解决方案1】:

据我了解,您正在尝试使用放置页面的框架,以便不打开新窗口。

为了使页面出现在框架中,您应该使用“导航”方法而不设置内容属性(https://docs.microsoft.com/fr-fr/dotnet/api/system.windows.controls.frame?view=netcore-3.1)。 (否则如果你只想设置 Content,你应该使用 ContentPresenter)。

所以更新会变成:

private void Update(object sender, RoutedEventArgs e)
    {
        Main.Navigate(new UpdateCio());
    }

这是说页面“UpdateCio.xaml”应该显示在框架内:

<Frame x:Name="Main"/>

它不会替换您定义的其余 xaml 代码,因此它显示在另一个 stackPanel 后面。

您可以更改第 Frame 的位置和 UpdateCio 的 xaml,使其显示在顶部并隐藏窗口的其余部分。 (有点老套)。

或者您可以真正使用 Frames 和 Pages 以及导航系统并定义一个 MainWindow 来显示不同的页面)。

MainWindow.xaml

<Grid HorizontalAlignment="Center"
            VerticalAlignment="Center" >
    <Frame x:Name="Main"/>
</Grid>

MainWindow.xaml.cs:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        //Set the initial page (you could also do it in xaml by setting the Source property of the Frame
        Main.Navigate(new IdentificationPage());
    }
}

IdentificationPage.xaml(您在 Window 中的 xaml 减去 Frame + 它变成了 Page 而不是 Window)。

<Grid>
    <StackPanel Margin="10 10 10 20" 
                Orientation="Vertical"
                VerticalAlignment="Center"
                HorizontalAlignment="Center"
                Width="357">
        <Label Content="User Name:"/>
        <TextBox Margin="0,0,3,0"/>
        <Label Content="Your Password"/>
        <PasswordBox Margin="0,0,3,0"/>
        <StackPanel Orientation="Horizontal" 
                    HorizontalAlignment="Center" 
                    Margin="10">
            <Button x:Name="ForUpdate"
                    Content="Match"
                    Click="Update"/>
        </StackPanel>
    </StackPanel>
</Grid>

IdentificationPage.xaml.cs:

public partial class IdentificationPage : Page
{
    public IdentificationPage()
    {
        InitializeComponent();
    }

    private void Update(object sender, RoutedEventArgs e)
    {
        //From a page, you have to use "NavigationService to Navigate"
        //(the .Navigate method appear only if the class inherits from Page)
        NavigationService.Navigate(new UpdateCio());
    }
}

UpdateCio.xaml 仍然是一个页面而不是一个窗口(这让我一开始感到困惑):

<Grid>
    <StackPanel HorizontalAlignment="Center"
            VerticalAlignment="Center">
        <Label Content="this is test!"/>
    </StackPanel>
</Grid>

UpdateCio.xaml.cs:

public partial class UpdateCio : Page
{
    public UpdateCio()
    {
        InitializeComponent();
    }
}

让我知道您最终选择了哪种解决方案。

【讨论】:

  • 错误信息是什么?您是否更改了 UpdateCio.xaml 和 IdentificationPage.xaml 的 xaml 以使其成为 Page 而不是 Window ? (“
猜你喜欢
  • 2013-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-31
  • 1970-01-01
相关资源
最近更新 更多