如何从Winform启动Windows Universal App [英] How to launch a Windows Universal App from winform
问题描述
我正在尝试使用以下代码从Winform运行Windows Universal App,但是很遗憾,它打开了documents文件夹。我是UWP应用开发的新手。是启动UWP应用程序的正确方法吗?
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:appsFolder\Microsoft.SDKSamples.CameraAdvancedCapture.CS_8wekyb3d8bbwe!App";
p.StartInfo = startInfo;
p.Start();
推荐答案
您在这里确实有两个问题:
You really have two questions here:
- 如何从WinForms应用程序启动协议
- 如何正确启动UWP应用程序。
要从WinForms应用程序启动协议,请使用带有 UseShellExecute = true 的Process对象。不要尝试使用Explorer.exe作为启动过程。
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.
启动应用的最佳方法是通过协议,只要该应用定义了一个即可。如果您控制应用程序,则可以定义@Romasz所示的协议:处理URI激活
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
您在命令行上使用的shell:appsFolder技巧是一个方便的脚本黑客,但未记录在案或保证。
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.
一旦有了协议,就可以使用Process.Start启动它。
Once you have a protocol you can launch it with Process.Start:
这是启动People应用程序的shell hack:
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:appsFolder\Microsoft.People_8wekyb3d8bbwe!App";
p.StartInfo = startInfo;
p.Start();
由于People应用程序定义了已记录的协议最好以这种方式启动它。这也可以让我们选择所需的联系人:
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();
启动未定义协议的UWP应用的正确方法是使用IApplicationActivationManager。这就是Shell内部使用的功能,它可以让您更好地控制正在启动的内容和操作方式。
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.
在 C#中的IApplicationActivationManager :: ActivateApplication吗?
这篇关于如何从Winform启动Windows Universal App的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!