How do I open a new Window in WinUI3 with WinRT/C++?
Asked Answered
D

2

1

How do I open a new window in WinRT / WinUI3? I want to click a button and open up another floating window on top of the current / main window.

I have tried code from several samples with zero luck:

The majority of the C++ WinUI3 documentation still only has C# code samples in it and is largely useless to me for that reason. I just want to open a new window and nothing else.

Denaturalize answered 18/9, 2021 at 5:17 Comment(0)
H
1

You can use Windows App SDK Samples and for example modify this method: void DemoPage::TitleBtn_Click

like this:

void DemoPage::TitleBtn_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e)
{
    auto window = Window();
    auto tb = TextBlock();
    tb.Text(L"Hello");
    window.Content(tb);
    window.Activate();
}

Which looks exactly like the C# sample here: Create a new Window

var window = new Window();
window.Content = new TextBlock() { Text = "Hello" };
window.Activate();
Hindustan answered 18/9, 2021 at 14:8 Comment(7)
In project reunion 0.8.2 I'm getting an exception at CreateInstance(winrt::Windows::Foundation::IInspectable const& baseInterface, winrt::Windows::Foundation::IInspectable& innerInterface) with this code. I tried it with a 1.0.0 experimental project and it worked fine.Denaturalize
I'm using the "standard" Project Reunion 0.8.3.52036702 from here learn.microsoft.com/en-us/windows/apps/windows-app-sdk/…. Make sure you C++/WinRT is also up to date (I'm using 2.0.210913.7). What exception do you have? on which CreateInstance?Hindustan
auto window = Window(); gives me an Unhandled Exception 0xC000027B. I confirmed all my nuget packages are updated to latest.Denaturalize
C++/WinRT and Project Reunion are not nugets: i.imgur.com/kvfyaDC.pngHindustan
C++/WinRT comes in two parts: The VSIX (responsible for project creation), and the C++/WinRT NuGet package containing the code generator and base library. The latter is a required build dependency, while the former is (technically) optional.Proprietress
true, I just remember having weird problems some times ago when the vsix was not up to date. Now I always check all this blindly...Hindustan
The split was part of the transition from C++/WinRT v1 to v2. The C++/WinRT v1 code generator and base library shipped as part of the Windows SDK. Deploying the generator and library via the NuGet platform decoupled C++/WinRT from the Windows SDK, allowing it to progress at its own pace. With v2, having the VSIX up-to-date isn't quite as important as in the v1 timeframe.Proprietress
D
0

You must upgrade to Project Reunion 1.0.0 EXPERIMENTAL or higher to use <winrt/Microsoft.UI.Windowing.h>. You can then create an AppWindow as follows:

auto appwind = winrt::Microsoft::UI::Windowing::AppWindow::Create();
appwind.Title(L"New Window Title");
appwind.Show();

This does NOT work in project reunion 0.8.2 or 0.8.3 stable releases.

Denaturalize answered 18/9, 2021 at 14:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.