Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

c# - How to launch a Windows Universal App from winform

I am trying to run a Windows Universal App from my winform using the following code but unfortunately it opens the documents folder. I am new in UWP app development. Is it the correct way to launch a UWP app?

Process p = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = "explorer.exe";
            startInfo.Arguments = @"shell:appsFolderMicrosoft.SDKSamples.CameraAdvancedCapture.CS_8wekyb3d8bbwe!App";
            p.StartInfo = startInfo;
            p.Start();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You really have two questions here:

  1. How do you launch a protocol from a WinForms app
  2. How to properly launch a UWP app.

To launch a protocol from your WinForms app use the Process object with UseShellExecute = true. Don't try launching it with Explorer.exe as the process.

The best way to launch an app is via protocol, so long as the app defines one. If you control the app then you can define a protocol as shown by @Romasz: Handle URI activation

The shell:appsFolder trick you used on your command line is a handy scripting hack, but it's not documented or guaranteed. Don't ship code dependent on it.

Once you have a protocol you can launch it with Process.Start:

Here's the shell hack to launch the People app:

Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.FileName =  startInfo.FileName =  @"shell:appsFolderMicrosoft.People_8wekyb3d8bbwe!App";
p.StartInfo = startInfo;
p.Start();

Since the People app defines a documented protocol it'd be better to launch it that way. This can also let us choose which contact we want:

Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.FileName = @"ms-people:viewcontact?PhoneNumber=8675309";
p.StartInfo = startInfo;
p.Start();

The correct way to launch a UWP app that doesn't define a protocol is to use the IApplicationActivationManager. This is what the shell will use internally, and it can give you more control over what you're launching and how.

There is a stackoverflow Q/A on using IApplicationActivationManager from C# at IApplicationActivationManager::ActivateApplication in C#?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...