我有一个 UWP 应用程序,它在 UWP 应用程序关闭时启动 FullTrustProcess:
MainPage.xaml.cs(UWP 应用程序):
private async void HandleSystemNavigationManagerCloseRequested(object sender, SystemNavigationCloseRequestedPreviewEventArgs e)
{
if (ApiInformation.IsApiContractPresent("Windows.ApplicationModel.FullTrustAppContract", 1, 0))
{
await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
}
}
进程运行时,您可以右键单击系统托盘图标并再次打开 UWP 应用程序。当 UWP 应用程序再次运行时,进程将关闭:
private async void OpenUWPApp(object sender, EventArgs e)
{
// Launch the UWP app:
var appListEntries = await Package.Current.GetAppListEntriesAsync();
await appListEntries.First().LaunchAsync();
// Close the process:
Application.Exit();
}
问题是,如果用户关闭 UWP 应用程序,启动 FullTrustProcess 应用程序,然后通过双击桌面上的 UWP 应用程序图标再次打开 UWP 应用程序,然后 UWP 应用程序再次启动,但 FullTrustProcess 应用程序是从上一个 UWP 会话启动后仍在运行。
所以我需要知道如何(从 UWP 应用程序)检查 FullTrustProcess 应用程序是否正在运行,如果是,则关闭它。因为我只想运行 UWP 应用程序或运行 FullTrustProcess,但不能同时运行两者。
我正在考虑使用 AppServiceConnection 从 UWP 应用程序向进程发送消息,但是连接仅在 UWP 应用程序关闭后建立,因此在新的 UWP 应用程序会话中,启动时连接为空。
App.xaml.cs(UWP 应用程序):
public static AppServiceConnection Connection = null;
protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args)
{
base.OnBackgroundActivated(args);
if (args.TaskInstance.TriggerDetails is AppServiceTriggerDetails details)
{
AppServiceDeferral = args.TaskInstance.GetDeferral();
args.TaskInstance.Canceled += OnTaskCanceled;
Connection = details.AppServiceConnection;
Connection.RequestReceived += HandleSystemTrayConnectionRequestReceived;
Connection.ServiceClosed += HandleSystemTrayConnectionServiceClosed;
}
}
供参考,这是我遵循的指南:https://stefanwick.com/2018/04/16/uwp-with-desktop-extension-part-3/
提前致谢。