如何从 winform 启动 Windows 通用应用程序 [英] How to launch a Windows Universal App from winform
问题描述
我正在尝试使用以下代码从我的 winform 运行 Windows 通用应用程序,但不幸的是它打开了文档文件夹.我是 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:appsFolderMicrosoft.SDKSamples.CameraAdvancedCapture.CS_8wekyb3d8bbwe!App";
p.StartInfo = startInfo;
p.Start();
推荐答案
你真的有两个问题:
- 如何从 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: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();
启动未定义协议的 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 通用应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!