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.

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
rileyd
  • 56
  • 1
  • 5

2 Answers2

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();
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • 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. – rileyd Sep 18 '21 at 14:24
  • I'm using the "standard" Project Reunion 0.8.3.52036702 from here https://learn.microsoft.com/en-us/windows/apps/windows-app-sdk/stable-channel#version-08. 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? – Simon Mourier Sep 18 '21 at 14:32
  • `auto window = Window();` gives me an Unhandled Exception 0xC000027B. I confirmed all my nuget packages are updated to latest. – rileyd Sep 18 '21 at 14:41
  • C++/WinRT and Project Reunion are not nugets: https://i.imgur.com/kvfyaDC.png – Simon Mourier Sep 18 '21 at 15:34
  • 1
    C++/WinRT comes in two parts: The VSIX (responsible for project creation), and the C++/WinRT [NuGet package](https://www.nuget.org/packages/Microsoft.Windows.CppWinRT/) containing the code generator and base library. The latter is a required build dependency, while the former is (technically) optional. – IInspectable Sep 18 '21 at 15:44
  • 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... – Simon Mourier Sep 18 '21 at 15:52
  • 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. – IInspectable Sep 18 '21 at 16:16
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.

rileyd
  • 56
  • 1
  • 5