简体   繁体   中英

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"

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

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. Is there any way to Basically do something like: window2.Content = Window2.xaml ? And also can I "link" Window2.cs file there?

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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