首页 > 解决方案 > 窗口的包含同时出现在页面的包含:WPF

问题描述

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

在下面的解决方案中,我有一个识别窗口和一个更新用户信息页面,当我点击识别窗口上的按钮时,窗口的包含和页面的包含同时出现,这就是我既不想也不能修复它。我想要的是:当用户单击识别窗口上的按钮时,窗口的包含应该只显示页面的包含。非常感谢你的帮助!

窗口 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>

标签: c#xmlwpf

解决方案


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

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

所以更新会变成:

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

这意味着页面“UpdateCio.xaml”应该显示在 Frame 内:

<Frame x:Name="Main"/>

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

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

或者您可以真正使用 Frames 和 Pages 以及导航系统并定义一个 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>

标识页.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>

更新Cio.xaml.cs:

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

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


推荐阅读