简体   繁体   English

WinUI 3 - 使用现有 XAML 文件创建新窗口

[英]WinUI 3 - Creating a new window with existing XAML file

I have two "windows" in my program, 1st being the main page, which the user should see on start and 2nd is the small window, that I need to open when a person presses certain Button.我的程序中有两个“窗口”,第一个是主页,用户应该在开始时看到,第二个是小窗口,当有人按下某个按钮时我需要打开它。

Main window is "MainWindow" Second window is "Window2"主窗口是“MainWindow” 第二个窗口是“Window2”

I have created a xaml file for Window2 where I have the layout (Window2.xaml), but I don't know how to specify this xaml file when creating the window我为 Window2 创建了一个 xaml 文件,其中有布局 (Window2.xaml),但我不知道如何在创建窗口时指定此 xaml 文件

In oder to open new window I used code from this question为了打开新窗口,我使用了这个问题的代码

private void sessionWieserherstellenBtn_Click(object sender, RoutedEventArgs e)
        {
            var window2 = new Window();
            window2.Content = new TextBlock() { Text = "Hello" };
            window2.Activate();
        }

The problem is that it creates a window with just "Hello" TextBlock, since the content is set to that TextBox.问题在于它创建了一个仅包含“Hello”TextBlock 的窗口,因为内容已设置为该 TextBox。 Is there any way to Basically do something like: window2.Content = Window2.xaml ?有没有办法基本上做类似的事情: window2.Content = Window2.xaml And also can I "link" Window2.cs file there?我还可以在那里“链接” Window2.cs 文件吗?

I was able to make it work with this code:我能够使用此代码使其工作:

private void sessionWieserherstellenBtn_Click(object sender, RoutedEventArgs e)
    {
        var newWindow = new Window2();
        newWindow.Activate();
    }

You can pass a page like this:你可以像这样传递一个页面:

Window window = new()
{
    Title = "Sub window",
    Content = new SubPage(),
};

// This will close the "Sub window" 
// when the MainWindow is closed.
this.Closed += (s, a) =>
{
    window.Close();
};

window.Activate();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM