Using Windows 10 UWP APIs from a .NET Core application
One of the things that Microsoft talked about at Build 2019 was how they intended to broaden availability of UWP APIs so that they can be consumed by non-UWP (in the "store only / sideload only" sense) apps. Some of the APIs were already consumable, others not, but Microsoft have recently published the Microsoft.Windows.SDK.Contracts NuGet package that provides access to thse APIs. What I haven't seen is much of any information regarding how to achieve this, so here goes!
Showing a toast notification from a .NET Core console app
The first thing to do is ensure that you've got the right development environment for this; that's Visual Studio 2019 and, as of the time of writing, .NET Core 3.0 Preview 5. You can check the version of .NET Core you have installed by running dotnet --version from the command line, or dotnet --info | more to check that you've got the correct SDK installed. The specific version I have is 3.0.100-preview5-011568 which I downloaded and installed from https://dotnet.microsoft.com/download/thank-you/dotnet-sdk-3.0.100-preview5-windows-x64-installer.
Once that's installed, create a new .NET Core project using the tooling of your choice (dotnet new, Visual Studio, Visual Studio Code, it makes no difference) and add a reference to the Microsoft.Windows.SDK.Contracts package. Next up, add the following code to your console apps Main:
var template = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01); var textNodes = template.GetElementsByTagName("text"); textNodes.Item(0).InnerText = "I'm a toast notification!"; var notifier = ToastNotificationManager.CreateToastNotifier("My .NET Core App"); var notification = new ToastNotification(template); notifier.Show(notification);
Once that's done, run the app up and you should see a toast notification appear, and once it's disappeared it should be sat in your notifications area:
Do make sure you've got Focus Assist switched off though. If it's on, you might think your code isn't working and sit scratching your head for 10 minutes until you look in the notifications area. Not that I've got experience with that.......
What other APIs are available?
There's quite a selection of othe APIs available, from Facial Recognition to Geolocation, Maps, Gaming and a lot more.
It is worth being aware that just because a class is exposed by the Microsoft.Windows.SDK.Contracts NuGet package, it doesn't mean you can use it in a "plain-vanilla" .NET Core 3 based app. Some classes are dependent on the app having a package identity (which desktop apps don't). This can be worked around by packaging a desktop app into an MSIX package (basically what's been referred to as 'Desktop Bridge') which you can read a bit more about here.