Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass string to UWP app and launch it from a Windows Forms app

Tags:

c#

winforms

uwp

This official MSDN document describes how to share data between Universal Windows Platform (UWP) apps. But can we pass a string to an UWP app from a Windows Forms app and launch the UWP app from Windows Forms app? I did not find any document for this scenario. I'm using C# but the language does not matter in this scenario.

NOTE: In our case, it's a long string that contains data that Windows Forms app wants to pass to the UWP app that uses that data.

like image 571
nam Avatar asked Jan 02 '23 21:01

nam


2 Answers

The easiest way to do this would be to use a custom URI scheme and then launch the app using that.

Registering and handling custom URIs is thoroughly described in the official UWP documentation, so I encourage you to follow that. In short, you first register a custom URI scheme in your Package.appxmanifest and then override the OnActivated in App.xaml.cs and check if the IActivatedEventArgs.Kind is ActivationKind.Protocol. Then you cast IActivatedEventArgs to ProtocolActivatedEventArgs and use its Uri property to access the launched URI.

On the Windows Forms side of things you then just have to launch the URI and that will cause the UWP app to launch / activate. You do so by starting a process with the URI:

var url = "myapp:?someparam=somevalue";
var psi = new ProcessStartInfo();
psi.UseShellExecute = true;
psi.FileName = url; 
Process.Start(psi);

You can use the URI to pass in any string values to the UWP app by virtue of using query string parameters in the URI. You can read those in the OnActivated method in UWP app.

Of course, this is not the only solution, as you can now also use UWP APIs in Windows Forms and so you could for example pass data between the two apps using App Services. If you however need just one-way communication, custom URI is the way to go.

Alternative method - App Execution Alias

Since build 16226 UWP supports App Execution Alias, which allows you to declare a system-wide app name that can be used to launch the app from the command-line from anywhere in the system. You can read more about it in this blogpost. This way, you could launch the app directly by its name and just pass the arguments as process args.

like image 168
Martin Zikmund Avatar answered Jan 04 '23 10:01

Martin Zikmund


In addition to @Martin Zikmund's answer; this article explains how you can start the UWP app from the command line, which is basically doing the same as Process.Start but might be easier in scripting scenario's. Doesn't work on all devices though.

c:\> explorer.exe shell:appsFolder\put-your-PackageFamilyName-here!put-your-app-ID-here

The query string addendum should work as well.

Take special note on how to get the parameters if you didn't set them yourself:

  1. Browse to the InstallLocation corresponded to the PackageFamilyName 13. Open the AppxManifest.xml 14. Look for "Executable=" for the app you want. (As some app package contains more than one app, such mail and calendar are under the same packetage) 15. On the same line, find the ID of the app

Example to open mail app:

@Eric mentions "an even better command line option for launching the mail app"

c:\> explorer outlookmail:

which works perfectly! (thanks @Eric)


Originally I provided this approach:

c:\> explorer.exe shell:appsFolder\microsoft.windowscommunicationsapps_8wekyb3d8bbwe!microsoft.windowslive.mail

And, another use full command list

like image 37
Stefan Avatar answered Jan 04 '23 09:01

Stefan