首页 > 解决方案 > UWP FullTrustProcessLauncher 在启动时给出“找不到元素”异常

问题描述

我在 Visual Studio 中有一个解决方案,其中包含一个 Windows 应用程序打包项目、一个 UWP 项目和一个控制台应用程序项目。UWP 应用程序包含一个按钮,按下该按钮时应该将控制台应用程序作为完全信任进程启动。解决方案资源管理器如下所示:

解决方案资源管理器

Windows 应用程序打包项目设置为启动项目。它的入口点设置为 UWP 应用。UWP 应用和控制台应用都作为引用添加到打包项目中。

包参考

配置管理器如下所示:

配置管理器

控制台应用设置为 UWP 应用的依赖项。UWP 应用程序的依赖项如下所示:

UWP 依赖项

构建顺序如下所示:

建造顺序

代码

这是控制台应用程序的代码:

using System;
using System.Diagnostics;

namespace ShellHost
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
    }
}

正如我所提到的,UWP 应用程序只有一页 (MainPage.xaml),其背后的代码如下所示:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.ApplicationModel;

namespace OnScreenDeviceManager
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private async void button_Click(object sender, RoutedEventArgs e)
        {
            await FullTrustProcessLauncher.LaunchFullTrustProcessForAppAsync("Default");
        }
    }
}

UWP 应用的清单未修改。包的清单已修改为包含完全信任功能,并且它包含完全信任过程的必要扩展。xml 文件如下所示:

<?xml version="1.0" encoding="utf-8"?>

<Package
  xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
  xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
  xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
  xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"
  IgnorableNamespaces="uap rescap desktop">

  <Identity
    Name="d3e964dc-9265-4243-b97c-2dacb7c11dac"
    Publisher="CN=Dell"
    Version="1.0.0.0" />

  <Properties>
    <DisplayName>Package</DisplayName>
    <PublisherDisplayName>Dell</PublisherDisplayName>
    <Logo>Images\StoreLogo.png</Logo>
  </Properties>

  <Dependencies>
    <TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" />
    <TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.14393.0" MaxVersionTested="10.0.14393.0" />
  </Dependencies>

  <Resources>
    <Resource Language="x-generate"/>
  </Resources>

  <Applications>
    <Application Id="App"
      Executable="$targetnametoken$.exe"
      EntryPoint="$targetentrypoint$">
      <uap:VisualElements
        DisplayName="Package"
        Description="Package"
        BackgroundColor="transparent"
        Square150x150Logo="Images\Square150x150Logo.png"
        Square44x44Logo="Images\Square44x44Logo.png">
        <uap:DefaultTile Wide310x150Logo="Images\Wide310x150Logo.png" />
        <uap:SplashScreen Image="Images\SplashScreen.png" />
      </uap:VisualElements>
      <Extensions>
        <desktop:Extension Category="windows.fullTrustProcess" Executable="ShellHost\ShellHost.exe">
            <desktop:FullTrustProcess>
                <desktop:ParameterGroup GroupId="SyncGroup" Parameters="/Sync"/>
                <desktop:ParameterGroup GroupId="OtherGroup" Parameters="/Other"/>
                <desktop:ParameterGroup GroupId="Default" Parameters=""/>
            </desktop:FullTrustProcess>
        </desktop:Extension>
      </Extensions>
    </Application>
  </Applications>

  <Capabilities>
    <Capability Name="internetClient"/>
    <rescap:Capability Name="runFullTrust" />
  </Capabilities>
</Package>

解决方案 exe 所在的 package/debug/bin 文件夹如下所示:

包目录

您可以在 ShellHost 文件夹中清楚地看到控制台应用程序 exe (ShellHost.exe)。

控制台应用程序已经过测试并且运行良好。UWP 应用程序运行良好,除非我单击按钮时出现“找不到元素”异常。

异常消息说:

System.Exception: 'Element not found. (Exception from HRESULT: 0x80070490)'

This exception was originally thrown at this call stack:
    [External Code]
    OnScreenDeviceManager.MainPage.button_Click(object, Windows.UI.Xaml.RoutedEventArgs) in MainPage.xaml.cs

谁能帮我解决这个问题?我错过了什么?

标签: c#.netuwpappxmanifestfull-trust

解决方案


LaunchFullTrustProcessForAppAsync API 意味着您需要使用指定的应用程序 ID 启动完全信任进程。

我建议您尝试使用 LaunchFullTrustProcessForCurrentAppAsync(String) API 来替换您的代码。

 private async void button_Click(object sender, RoutedEventArgs e)
 {
     await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync("Default");
 }

推荐阅读