在Win UI 3中,如何更改主窗口页面?

发布于 2025-02-10 08:58:19 字数 265 浏览 0 评论 0原文

在Win UI 3中,当用户单击按钮时,我想更改MainWindow的内容。现在,我拥有所有内容的MainWindow,然后用户单击一个按钮,然后打开一个新窗口,我们将其称为SecondWindow。我这样做是:

Window secondWindow= new SecondWindow();
secondWindow.Activate();

但是我想要的是要在主窗口中打开Secondwindow的内容,代替MainWindow的内容。我该怎么做?

In Win UI 3 I want to change the content of the MainWindow when the user clicks a button. Right now I have the MainWindow with all its content, then the user clicks a button and a new window opens, let's call it SecondWindow. I do this with:

Window secondWindow= new SecondWindow();
secondWindow.Activate();

But what I want is for SecondWindow's content to open in the main window, in place of MainWindow's content. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

王权女流氓 2025-02-17 08:58:19

如果您希望在另一个窗口内显示secondwindow的内容,则应是usercontrol而不是窗口。

然后,您可以简单地作为主窗口的根面板的孩子添加一个实例:

mainwindow.xaml:

<Window ...>
    <Grid x:Name="root">
...

mainwindow.xaml.cs:

this.grid.Children.Clear();
this.grid.Children.Add(new YourUserControl());

如果您定义了内容直接在窗口中,您应该将其content添加到主窗口中:

Window secondWindow = new SecondWindow();
var root = secondWindow.Content;

this.grid.Children.Clear();
this.grid.Children.Add(root);

secondWindow.Close();

If you want the display the contents of the secondWindow inside another window, it should be a UserControl instead of a window.

You could then simply add an instance of it as a child of the root panel of the main window:

MainWindow.xaml:

<Window ...>
    <Grid x:Name="root">
...

MainWindow.xaml.cs:

this.grid.Children.Clear();
this.grid.Children.Add(new YourUserControl());

If you define the contents directly in a window, you should add its Content to the main window like this:

Window secondWindow = new SecondWindow();
var root = secondWindow.Content;

this.grid.Children.Clear();
this.grid.Children.Add(root);

secondWindow.Close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文