One thing that went unnoticed in the last post is that the console app is multi-instance: if you click in the live tile or in the start menu, instead of bringing the same app to the front, you are opening a new window and running a new instance of it. This is something new for UWP apps: until now, you could only have one instance of the app running in your machine. While this works for many apps, when you are developing an LOB app, for example, you may need to open more than one instance and that can be limiting. The SDK released in the Aril 2018 update allow you to create multi instanced apps.
Single instance apps
The default UWP apps have a single instance. When you activate it for the first time, it opens the main window and runs the app. After the second call, the same app is activated and you can handle the activation in the OnLaunchedmethod, like this:
This way, you will see the activation number in the main page for the app. The app continues to have a single page, but you can control the number of times it's activated. One other way to handle this is to open multiple windows, one for each activation. It is done like this:
The process is similar to the previous ones, but there is a difference: for the first activation, it fills the main window, and if not, it creates a new window with the CreateNewView method, then fills its content in the same way it did for the main window. After the window and the view are created and activated, we call ApplicationViewSwitcher.TryShowAsStandaloneAsync to show it. This will show the new window for the app.
This arrangement works fine, but there is a problem, here: all windows are in the same process and, if you have a problem in one of them, all will crash. This may not be what you want, and it's changed in the April 2018 update - you can have multi-instance UWP apps.
Multi-Instance apps
The main difference is the SupportsMultipleInstances attribute in the package.appxmanifest file, in the Application node:
The desktop4 and iot2 namespaces are defined in the Package node:
Once you put these in your package.appxmanifest, your application will be multi-instanced:
If you don't want to add this data manually, you can install the Multi-Instance templates, created by Microsoft. In Visual Studio, go to Tools/Extensions and Updates and install the Multi-Instance template:
Once you do that, you will get two new UWP templates, the Multi-Instance UWP App and Multi-Instance UWP Redirection App:
The Multi-Instance UWP App template does what we did before: creates a blank app with the new attribute. The Multi-Instance Redirection UWP App is a bit more complex. Sometimes you want to handle how multi instancing works and call a specific instance. In this case, the template generates a Program.cs file with a Main method, where you can handle the redirection.
From the comments in the file, you can see that you can get the default behavior just by using this code in the Main method:
The key will be linked to the instance you want. For example, if you generate only different keys, new instances will be opened, and if you generate only one key, the first instance will be called. You can control how many instances you want and which ones you want to activate by using the keys you generate. For example, this code will allow you to have three instances open and will call them in a round-robin scheme:
In this case, we are keeping it simple, but we can make something more complex here. For example, we can activate some instance depending on the parameters passed to the application:
The key for the instance depends on the first parameter passed to the app. If we change our package.appxmanifest to add an ExecutionAlias, we can open a console window and call the app with a parameter:
Now, we can handle different instances of our app. If we want to process the arguments (for example, to open a different file depending on the passed parameter), we can do it in the App.xaml.cs. Please note that the OnLaunched method is not called when the app is activated from the command line. In this case, you must override the OnActivated method and process the arguments from there:
The processing of the arguments in the main page are done in the OnNavigatedTo method in MainPage.xaml.cs:
Now, if we open a command line window and type something like MultiInstance "this is a test", the program will open a new instance with the arguments:
Conclusions
As you can see, this new feature brings a lot of flexibility to your app. You already had the possibility to create many windows and now you can even create new instances and redirect the app to the right instance, depending on how the app is launched.
The full source code is available in http://github.com/bsonnino/MultiInstanceUWP