The path from a desktop app to a Universal Windows Platform (UWP) app

Windows

Windows
Last month, we provided an overview of the Desktop Bridge in this
Please, Log in or Register to view URLs content!
. We are excited to announce that since then we’ve released a new version of the desktop app converter, adding support for
Please, Log in or Register to view URLs content!
and fixing several issues to provide a more stable environment. To learn more about the improvements in the Desktop App Converter, please visit
Please, Log in or Register to view URLs content!
.

Today, we will follow up with a walkthrough of a concrete coding example that takes an existing desktop app and gradually migrates and enhances it with the Desktop Bridge into a full Universal Windows Platform (UWP) app that can run on all Windows 10 devices—such as Xbox One, HoloLens, phones, et cetera—and take full advantage of the platform.

Our starting point is a simple Windows Forms app that keeps track of the user’s status by storing it in the registry. Essentially, we start with this:

Please, Log in or Register to view URLs content!



private void cmbBox_SelectedIndexChanged(object sender, EventArgs e)
{
MyRegKey.SetValue("Status", cmbBox.SelectedItem);
}



The code for this sample app, as well as the code for each of the steps along the journey across the bridge, can be found in our
Please, Log in or Register to view URLs content!
. As we gradually migrate our app, it remains fully functional every step of the way and picks up additional features that will help keep users engaged.

Please, Log in or Register to view URLs content!


Step 1: Convert


As a first step to get our app on the bridge, we take it through the conversion process. There are a couple of options to do this initial step. For a simple app like this, which gets installed just by copying binaries, we can quickly perform a manual conversion as explained in this
Please, Log in or Register to view URLs content!
and demonstrated in this
Please, Log in or Register to view URLs content!
. Apps that are using a .MSI (or similar) installer should use the
Please, Log in or Register to view URLs content!
for the conversion process. Finally, there are several third party install tool providers who are in the process of adding support for the Desktop Bridge to UWP in their products, as well, such as
Please, Log in or Register to view URLs content!
,
Please, Log in or Register to view URLs content!
and
Please, Log in or Register to view URLs content!
.

Once the conversion is complete, we want to test, adjust, and enhance our application in Visual Studio. For that, we can take advantage of the project type for
Please, Log in or Register to view URLs content!
, which the Visual Studio team recently introduced for Visual Studio 15 Preview 2 (see
Please, Log in or Register to view URLs content!
for details and instructions).

Please, Log in or Register to view URLs content!


Now we can hit F5 and debug our application running within the UWP app model. With the conversion, a number of immediate platform benefits become available automatically, such as Start menu integration, clean install/update/uninstall, and distribution of the .appx package via your choice of distribution.

Please, Log in or Register to view URLs content!


Please, Log in or Register to view URLs content!


Step 2: Enhance


Now that we have brought our app into the UWP app model, an app identity (as specified in the appxmanifest.xml file) is added, which enables our app to call UWP APIs that were not available to our desktop application before. Let’s look at an example of how to add a Live Tile to our app. The goal is to update the tile on the Start menu whenever the user’s status changes:

Please, Log in or Register to view URLs content!




private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MyRegKey.SetValue("Status", comboBox1.SelectedItem);

// Update the apps live tile
XmlDocument tileXml =
TileUpdateManager.GetTemplateContent(TileTemplateType.TileSquare150x150Text03);

XmlNodeList textNodes = tileXml.GetElementsByTagName("text");
textNodes[0].InnerText = "MyDesktopApp";
textNodes[1].InnerText = "Status: ";
textNodes[2].InnerText = comboBox1.SelectedItem.ToString();
textNodes[3].InnerText = DateTime.Now.ToString("HH:mm:ss");

TileNotification tileNotification = new TileNotification(tileXml);
TileUpdateManager.CreateTileUpdaterForApplication().Update(tileNotification);
}



Step 3: Extend


In addition to calling more UWP APIs, we can now also add UWP components to our app package to take advantage of other exciting UWP features. Examples of this include adding
Please, Log in or Register to view URLs content!
, becoming a sharing target or a file picker, or adding background tasks.

With background tasks, an app can respond to triggers to execute code even when the app is not running—even if the device is in connected stand-by mode. This way, you can always keep your app up to date and create an experience of an app that is always alive. To illustrate this in our example, we want to use the TimeTrigger to periodically check the status and notify the user via a toast notification:

Please, Log in or Register to view URLs content!


Now that your Winforms app has identity and runs in the UWP app model. You can declare background tasks in your manifest and register them from your code the same way a UWP can do today:



RegisterBackgroundTask("Timer", new TimeTrigger(15, false));



When the TimeTrigger fires, the system will invoke your registered background task in a UWP app container process and you can run code even if your app is not currently running:



public sealed class BgTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
ShowToast("Status: " + currentStatus);
}
}


Step 4: Migrate


To take advantage of UWP UI features such as a touch-friendly UI and smooth animations, let’s migrate the user experience of our project to a XAML front-end. In practice, the details of the UI migration depend on the Win32 UI framework your app has been using and the complexity of your UI. In this simple example, the changes are straightforward. In most real apps, however, this step will require more developer resources.

At this time, we can delete our now obsolete Windows Forms related UI code. However, we still want to keep around our desktop app process for our existing business logic (i.e. managing the app’s state via registry). To communicate between the two processes, we are using an App Service as communication pipeline.

Please, Log in or Register to view URLs content!


Please, Log in or Register to view URLs content!




private async void cmbBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ValueSet valueSet = new ValueSet();
valueSet.Add("StatusUpdate", cmbBox.SelectedItem.ToString());

if (App.Connection != null)
{
AppServiceResponse response = await
App.Connection.SendMessageAsync(valueSet);
}


Step 5: Expand


In the final step, we will now expand the reach of our application to all Windows 10 devices that run UWP apps by moving the last remaining pieces of code into the UWP app container process. To do so, we will replace the registry-based state with the UWP-compliant
Please, Log in or Register to view URLs content!
. Thus, we can now deploy our app to phones, Xbox One, HoloLens and other Windows devices that run UWP apps.

Please, Log in or Register to view URLs content!




private void cmbBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ApplicationData.Current.LocalSettings.Values["Status"] = cmbBox.SelectedValue.ToString();
}


Conclusion


The Desktop Bridge offers an exciting path to migrate your existing desktop apps to UWP. It enables you to gradually move across the bridge at your own pace while keeping your existing code investments running at all times along the way.

As you explore the tools on current preview builds, we’re very keen to hear your experiences and feedback. The best place to make feature suggestions is on the
Please, Log in or Register to view URLs content!
site. For questions and bug reports, please head over to the
Please, Log in or Register to view URLs content!
.

Additional resources

  • Please, Log in or Register to view URLs content!
  • Please, Log in or Register to view URLs content!
    – Bringing Existing Desktop Applications to the Universal Windows Platform by John Sheehan, Peyman Zanjani
  • Please, Log in or Register to view URLs content!
    – Bringing Desktop Apps to the UWP Using Desktop App Converter by David Tepper, Avneet Singh
  • Please, Log in or Register to view URLs content!
    – John Sheehan, David Tepper, Stefan Wick

Written by Stefan Wick, Principal Program Manager Lead for the Desktop Bridge

Please, Log in or Register to view URLs content!
 

Users who are viewing this thread

Top