未加星标

Calling UWP APIs from a Desktop Application

字体大小 | |
[系统(windows) 所属分类 系统(windows) | 发布者 店小二03 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

This post is partly an aide memoir. If you have a desktop application then MSDN tells you that you can call UWP APIs from that application;

UWP APIs callable from a classic desktop app

Some of these ‘just work’ from any desktop app whereas others need an ‘app identity’ in order to function correctly and so could only be used by a desktop app that had been packaged as a UWP app by the ‘Centennial’ technology .

But how do you make this work? For a long time, the ever-brilliant Lucian Wischik has been maintaining a project that he’s now moved over to Github named ‘UwpDesktop’;

uwp-desktop project on github

which makes this fairly easy and so I can make a .NET Framework (>4.5) project in Visual Studio 2015 on my windows 10 machine and then let’s say I add a button to call a click event handler which looks like this.

void OnClick(object sender, RoutedEventArgs e) { System.Windows.Clipboard.SetText("I am on the clipboard via .NET"); } So far, so good
Calling UWP APIs from a Desktop Application
I’m using .NET APIs from a .NET app so everything there is as it should be. If I want to make calls to UWP APIs then I can add the NuGet package via the NuGet console as below;
Calling UWP APIs from a Desktop Application

and I then get a reference in my project to WindowsBase ;


Calling UWP APIs from a Desktop Application
and I generally forget how to do this each and every time I try it and that’s why I thought I’d write the post
Calling UWP APIs from a Desktop Application

Now, I can change my click handler to make use of a UWP API as per below;

void OnClick(object sender, RoutedEventArgs e) { var package = new Windows.ApplicationModel.DataTransfer.DataPackage(); package.SetText("I am on the clipboard via UWP"); Windows.ApplicationModel.DataTransfer.Clipboard.SetContent(package); }

Note that I left the full namespace names in the code just to make it clear what was going on.

Now, admittedly, I’m not sure that’s a very complete example because there’s clipboard functionality in both UWP and .NET and so you perhaps don’t need the UWP APIs to do this.

Perhaps a more realistic example might be something like the face detection APIs that are present in UWP but aren’t present in .NET although (of course) you can always call out across the network to the Cognitive APIs if you need face detection (and more) from a RESTful endpoint.

But how would I use UWP face detection (e.g.) from a WPF application? Here’s an example click handler bringing in that API to find some faces;

async void OnClick(object sender, RoutedEventArgs e) { // Image from the '3 amigos' film... var url = "http://wac.450f.edgecastcdn.net/80450F/screencrush.com/442/files/2012/05/d3ec4c0848f2.png"; using (var httpClient = new System.Net.Http.HttpClient()) { var bytes = await httpClient.GetByteArrayAsync(url); using (var randomAccessStream = new Windows.Storage.Streams.InMemoryRandomAccessStream()) { // Note that AsBuffer here is System.Runtime.InteropServices.WindowsRuntime.AsBuffer extension. await randomAccessStream.WriteAsync(bytes.AsBuffer()); var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync( randomAccessStream); // I don't think there's a common format between the decoder and the // face detector so I first go for BGRA8 and then I'll convert it // to a format supported by the FaceDetector. using (var bitmap = await decoder.GetSoftwareBitmapAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied)) { var detectorFormat = FaceDetector.GetSupportedBitmapPixelFormats().First(); // Convert for detection. using (var detectorBitmap = Windows.Graphics.Imaging.SoftwareBitmap.Convert(bitmap, detectorFormat)) { var detector = await Windows.Media.FaceAnalysis.FaceDetector.CreateAsync(); // Detect. var faces = await detector.DetectFacesAsync(detectorBitmap); // Report. MessageBox.Show($"I see {faces.Count} faces in that image"); } } } } }

Note again that I’ve tried to leave all the full namespace names in that code to show where the APIs are coming from and also note that I put that together in about 5 minutes so there’s no doubt other/better ways of doing it but it’s the idea that I’m focused on here.

Note that I stuck with MessageBox from .NET here rather than trying to use MessageDialog from the UWP because that latter API depends on having a top level window handle to operate with and in the context of a WPF application that top level window handle wouldn’t be there.

Similarly, if I were to invoke some API that involved the notion of having a ‘package identity’ then I’d get into trouble. A good example might be code that did something like;

void OnClick(object sender, RoutedEventArgs e) { var appFolder = Windows.Storage.ApplicationData.Current.LocalFolder; }

then I’m going to see an exception reported;


Calling UWP APIs from a Desktop Application

because that API relies on the notion of the calling app being installed as a UWP app on the system and that can only happen if it’s packaged as a UWP app in the first place and that leads back ‘Centennial’ as the means to produce a .appx package from a desktop app which can then run as a ‘true UWP’ app rather than a desktop app calling UWP APIs.

本文系统(windows)相关术语:三级网络技术 计算机三级网络技术 网络技术基础 计算机网络技术

主题: WindowsUWWPFVisual StudioVisual StudiRESTSDN.NetGit
分页:12
转载请注明
本文标题:Calling UWP APIs from a Desktop Application
本站链接:http://www.codesec.net/view/481373.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(windows) | 评论(0) | 阅读(42)