question

JosHuybrighs-1920 avatar image
0 Votes"
JosHuybrighs-1920 asked PeterPoirier-5235 commented

Support for Background Tasks in WPF desktop apps (.NET 5 and WinUI 3)

I am currently experimenting with the latest possibilities in Visual Studio 19 preview 2, to develop a packaged WPF desktop app that is using WinUI 3 for the UI.
I know that all of this is not yet intended for production use but I would like to understand whether somewhere in 2021 this could be a better approach for developing apps for the Windows Store instead of UWP, DesktopBridge, or WPF with XamlIslands.

In particular I would like to know whether WPF desktop apps on .NET 5 and WinUI 3 are supporting or eventually will support Background Tasks in the way they are supported on UWP apps. The beauty of the latter is that registration/deregistration of background tasks on installation/update/deinstallation of the app works seamlessly. It would be great if this would also work with a WPF Desktop app.

I experimented a little bit with this and discovered that I can write code to register an out-of-process background task using the standard provisioned api (Microsoft.Windows.SDK.NET.dll). However, it is not possible to reference a c# Windows Runtime Component that would implement the background task. There are currently only 2 type of Windows Runtime Component templates in Visual Studio 19, prev2:

  • Windows Runtime Component (WinUI in UWP) for a managed (c#) .winmd, and

  • Windows Runtime Component (WinUI) for a unmanaged (c++) .winmd.

None of them can be referenced / included in a .NET5 app.

My question basically is: does it make any sense to continue working in this direction or should I have to look for alternative ways to have some type of background tasks?

windows-uwpwindows-wpfdotnet-wpf-xaml
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You could use uwp backgroundtask in legacy desktop app that package with desktop-bridge, please check this blog for detail.


0 Votes 0 ·

Yes that works when it is a WPF app with WPF user controls but it will not work when one also wants to make use of the modern UI controls, like Xaml Islands.
See Can't make release build with a WPF desktop app that is using Xaml Islands and Background Tasks

My question is specifically about WPF using WinUI 3 and .Net 5. I just want to understand whether Background Tasks using that architecture should work now, will work in the future, or will never work.

0 Votes 0 ·
JosHuybrighs-1920 avatar image
1 Vote"
JosHuybrighs-1920 answered JosHuybrighs-1920 edited

It turns out that it is indeed possible to create a WinUI 3 desktop app that supports Background Tasks in the way that this is possible with a pure UWP or a UWP desktop bridge app.

Unfortunately Visual Studio 2019 prev.2 doesn't support this immediately with any of the available project templates. Luckily enough BrunoBlanes (see Question: Is it possible to create an out-of-process background task in a Desktop application as is with UWP?) explained me how to do this.

Because not everything is obvious I will describe the various steps to take.

This is what you have to do:

  1. Create a Windows Runtime Component (WRC) project to implement the background task. Do this in Visual Studio 2019 by choosing the c# Windows Runtime Component (WinUI in UWP) project template and selecting 10.0.19041.0 as target version. Add your background task class (inheriting from IBackgroundTask).

  2. Create a UWP hosting app for the above WRC. Choose the c# Blank App(WinUI in UWP) project template and select 10.0.19041.0 as target version.

  3. Do the following in the UWP hosting app project:
    1. Rename App.xaml and App.xaml.cs to Main.xaml and Main.xaml.cs en rename the App class to Main (in both Main.xaml and Main.xaml.cs).

    2. Delete MainPage.xaml and MainPage.xaml.cs.

    3. In Main.xaml.cs: Change using Microsoft.UI.Xaml to using Windows.UI.Xaml, keep only an empty Main class constructor, and delete all other usings.

    4. Delete the <Application.Resources> entry completely in Main.xaml.

    5. Add a reference to the above WRC project.

  4. Close the solution and remove the Microsoft.WinUI reference by deleting the WinUI PackageReference in both the WRC and the hosting project file.

  5. Open the solution. You then might still have to remove the Microsoft.ApplicationModel.Resources.winmd reference and all the VCLib references in both projects before the WinUI reference actually disappears.

You are now good to go.

  1. Add the hosting app to your desktop packaging project

  2. Add the necessary extensions in the Package.appxmanifest file of the packaging project

  3. Do the necessary background task registrations in the code of the Desktop App project.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AngelaZhang-8355 avatar image
0 Votes"
AngelaZhang-8355 answered PeterPoirier-5235 commented

C#/WinRT is working to provide support for this scenario. With C#/WinRT you can author WinRT types in C# .NET 5. The background task scenario currently involves some project hacks and this is ongoing work that will be finished in the upcoming few weeks. Here's a sample of the current state of how to author a background task and consume it from a .NET 5 WPF app: [https://github.com/microsoft/CsWinRT/tree/master/src/Samples/BgTaskComponent][1]

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Kind of related, but is it possible to support extension contracts in general with Winui 3 desktop apps? I saw here

https://docs.microsoft.com/en-us/windows/winui/api/microsoft.ui.xaml.application?view=winui-3.0

It says OnActivated() is not invoked for desktop apps.

0 Votes 0 ·