Home > Mobile >  WinUI 3 - Creating a new window with existing XAML file
WinUI 3 - Creating a new window with existing XAML file

Time:12-23

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?

CodePudding user response:

I was able to make it work with this code:

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

CodePudding user response:

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