Tina Nigro

Multiple Windows in a WinRT app (8.1 and 10)

Multiple Windows in a WinRT app (8.1 and 10)

With Windows 8.1 appeared multitasking APIs allowing developers to use the Snap capabilities of Windows 8.0, expanded with Windows 8.1.
Since then, apps like Mail can open an embedded link by opening a second window. This multi windowing capability even made Apple jealous, and it is rumored to be available in iOS 9.

Multiple windows

Developers can also open a second window of the same app! This is really interesting as it allows, for example, to open a mail in a second window while having a primary window with all your unread mails.

Multiple instances of the same app

Apart from Microsoft apps, only a few apps are making use of those useful APIs, which is sad but understandable as the multi-windowing feature was designed primarily for tablets, which are definitely not selling like hotcakes.

With Windows 10, Microsoft introduced Tablet Mode (which I like to call Immersive Mode) and apps are in Fullscreen mode only when this mode is enabled. Otherwise, apps will launch in windows, just like it’s been the case before I was even born. Windows is about windows again, rejoice!

Getting started: how to use multiple windows

Interestingly, the APIs are the same whether you’re targetting Windows 8.1 or 10. The new version of Windows brings some new features, such as resizing your window.

Here’s the code.

public static async Task CreateNewWindow()
{
    var newCoreAppView = CoreApplication.CreateNewView();
    var appView = ApplicationView.GetForCurrentView();
    await newCoreAppView.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, async () =>
    {
        var window = Window.Current;
        var newAppView = ApplicationView.GetForCurrentView();
#if WINDOWS_UAP
        newAppView.SetPreferredMinSize(new Windows.Foundation.Size(400, 300));
#endif
        var frame = new Frame();
        window.Content = frame;
        frame.Navigate(typeof(MainPage));
        window.Activate();
        await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newAppView.Id, ViewSizePreference.UseMore, appView.Id, ViewSizePreference.Default);
#if WINDOWS_UAP
        var success = newAppView.TryResizeView(new Windows.Foundation.Size(400, 400));
#endif
    });
}

First, you need to create a new CoreApplication view, so you have a new Dispatcher (new UI Thread) for that new view. Then, get the instance of the current window in that new view, set its Content (a Frame which navigates here to MainPage), and finally activate and show it by sending its Id to

ApplicationViewSwitcher.TryShowAsStandaloneAsync()

Bonus

As you can see in the code, I take the benefits of some new Windows 10 APIs:

  • SetPreferredMinSize : Allows you to set the default minimum size of a window. If your secondary window is a widget, or a mini-player, this method is for you.
  • TryResizeView : Your UX scenario implies resizing the window for any reasons? Go for it.

Of course, using reflection you can use those in a W8.1 app running on a W10 machine.

Result

Windows 10 Apps Windows 10 apps can resize and have a minimum size

Windows 8.1 window size can't be set by the developer, while the user has a very limited control over the window's size Windows 8.1 window size can’t be set by the developer, while the user has a very limited control over the window’s size

Updated

It also works on Windows 10 Mobile!

GitHub

As usual, the samples are available here: https://github.com/TinaNigro/Sample3-MultiWindowing

Please note: I’m thinking of having just one repo with all the code samples inside, rather than having one repo per blogpost. Any opinion?

Multiple Windows in a WinRT app (8.1 and 10)
Prev post

Centered alignment of GridView items

Next post

How to compute the TitleBar height on Windows 10

Multiple Windows in a WinRT app (8.1 and 10)

Get in touch

Avenco comes with a built-in contact form.