Howdy! 👋 It is almost 2022 and .NET MAUI team non-stop delivers new features for the upcoming MAUI year!
.NET MAUI Preview 11 was silently released (release notes) with Multi-Window support! Let's see how to add this cool feature to your application.
- Run
dotnet workload update
to update to the latest .NET MAUI Preview version. We need at least Preview 11. - To open the window we need just 2 lines of code: the first line initializes a new window with a Page inside, and the second line just opens the window:
var newWindow = new Window(new SecondPage());
Application.Current?.OpenWindow(newWindow);
- Closing the window is even simpler - we need just 1 line of code.
To close the current window call the next command:
Application.Current?.CloseWindow(GetParentWindow());
To close a specific window pass the instance of that window to the method:
var myLastWindow = Application.Current?.Windows.Last();
Application.Current?.CloseWindow(myLastWindow);
Platform configurations
iOS/MacCatalyst
- Create and register
SceneDelegate
[Register(nameof(SceneDelegate))]
public class SceneDelegate : MauiUISceneDelegate
{
}
- Open
Info.plist
and add the next content to the end of the file:
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
<true/>
<key>UISceneConfigurations</key>
<dict>
<key>UIWindowSceneSessionRoleApplication</key>
<array>
<dict>
<key>UISceneConfigurationName</key>
<string>__MAUI_DEFAULT_SCENE_CONFIGURATION__</string>
<key>UISceneDelegateClassName</key>
<string>SceneDelegate</string>
</dict>
</array>
</dict>
</dict>
The full code can be found on GitHub.
Happy holidays! 🎄🎁