Click here to Skip to main content
16,020,669 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

In a project in C++ WinUI 3, I have a problem to create two windows at the same time. I reduce the problem to narrow down and I realize that I can't create two objects Window in the same function. For example, if I have an event when a button is clicked like this:

void MainWindow::ClickButton(IInspectable const&, RoutedEventArgs const&)
{
w1 = Window();
w2 = Window();
}

Obviously I'm simplified the code for clarity. w1 and w2 are variables of type IWindow members of class MainWindow.

The avove code fails with an unmanaged exception. The function run perfectly and return, but the exception is triggered.

If I create only one Window it work right. How to create two windows inside an event handler?

Thanks in advance!

What I have tried:

I tried activate both windows but no luk
Posted

1 solution

You need a "multi-instance" application if you want more than one window in UWP.

Create a multi-instance Universal Windows App - UWP applications | Microsoft Learn[^]
 
Share this answer
 
Comments
Vicente Flich 26-Jan-24 2:40am    
But, I don't understand this is the problem, simply I want two windows in the same instance. If I code the same but in two events separately:

void MainWindow::ClickButton1(IInspectable const&, RoutedEventArgs const&)
{
w1 = Window();
}
void MainWindow::ClickButton2(IInspectable const&, RoutedEventArgs const&)
{
w2 = Window();
}

They work and create two windows without error. Why cannot do it in the same event handler?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900