ProcessForUWP.Desktop 0.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package ProcessForUWP.Desktop --version 0.0.2                
NuGet\Install-Package ProcessForUWP.Desktop -Version 0.0.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ProcessForUWP.Desktop" Version="0.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ProcessForUWP.Desktop --version 0.0.2                
#r "nuget: ProcessForUWP.Desktop, 0.0.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ProcessForUWP.Desktop as a Cake Addin
#addin nuget:?package=ProcessForUWP.Desktop&version=0.0.2

// Install ProcessForUWP.Desktop as a Cake Tool
#tool nuget:?package=ProcessForUWP.Desktop&version=0.0.2                

Process for UWP

一种适用于 UWP 平台的 Process 方法

基于 @Silver-Fang 的项目 (Github)

LICENSE Issues Stargazers

UWP Desktop

目录

简介

本项目基于 @Silver-Fang 的项目 ProcessForUWP,与其不同的是,本项目利用了通信接口 AppServiceConnection 来进行应用间的通信,所以不用执行一次就弹一次 UAC,但是使用起来会比较麻烦。目前仍在开发当中,如果有兴趣欢迎加入。

如何使用

在你的解决方案中添加一个打包项目和一个空白桌面应用项目。在打包项目中引用你的 UWP 项目和桌面应用项目。在 UWP 项目中添加引用 ProcessForUWP.UWP,在桌面应用项目中引用 ProcessForUWP.Desktop

然后在打包项目的 Package.appxmanifest 中添加:

<Package
    ...
    xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10" 
    IgnorableNamespaces="uap rescap desktop">
    ...
    <Applications>
        <Application>
            ...
            <Extensions>
                <uap:Extension Category="windows.appService">
                    <uap:AppService Name="ProcessForUWP.Delegate"/>
                </uap:Extension>
                <desktop:Extension Category="windows.fullTrustProcess" Executable="【桌面应用项目的路径,如:ProcessForUWP.Demo.Delegate\ProcessForUWP.Demo.Delegate.exe】" />
            </Extensions>
        </Application>
    </Applications>
    ...
</Package>

在 UWP 项目的 App.xaml.cs 中添加:

public sealed partial class App : Application
{
    public App()
    {
        ...
        EnteredBackground += App_EnteredBackground;
        LeavingBackground += App_LeavingBackground;
    }
    ...
    protected override async void OnLaunched(LaunchActivatedEventArgs e)
    {
        await InitializeConnection();
        ...
    }

    private async Task InitializeConnection()
    {
        if (Connection == null)
        {
            if (ApiInformation.IsApiContractPresent("Windows.ApplicationModel.FullTrustAppContract", 1, 0))
            {
                try
                {
                    await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
                    AppServiceConnected += (sender, e) =>
                    {
                        Connection.RequestReceived += ProcessHelper.Connection_RequestReceived;
                        ProcessHelper.SendMessage = (value) =>
                        {
                            string json = JsonConvert.SerializeObject(value);
                            try
                            {
                                ValueSet message = new ValueSet() { { "UWP", json } };
                                _ = Connection.SendMessageAsync(message);
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine(ex);
                                Debug.WriteLine(json);
                            }
                        };
                    };
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                }
            }
        }
    }
    ...
    public static BackgroundTaskDeferral AppServiceDeferral = null;
    public static AppServiceConnection Connection = null;
    public static event EventHandler AppServiceDisconnected;
    public static event EventHandler<AppServiceTriggerDetails> AppServiceConnected;
    public static bool IsForeground = false;

    private void App_LeavingBackground(object sender, LeavingBackgroundEventArgs e)
    {
        IsForeground = true;
    }

    private void App_EnteredBackground(object sender, EnteredBackgroundEventArgs e)
    {
        IsForeground = false;
    }

    /// <summary>
    /// Handles connection requests to the app service
    /// </summary>
    protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args)
    {
        base.OnBackgroundActivated(args);
        
        if (args.TaskInstance.TriggerDetails is AppServiceTriggerDetails details)
        {
            // only accept connections from callers in the same package
            if (details.CallerPackageFamilyName == Package.Current.Id.FamilyName)
            {
                // connection established from the fulltrust process
                AppServiceDeferral = args.TaskInstance.GetDeferral();
                args.TaskInstance.Canceled += OnTaskCanceled;
                
                Connection = details.AppServiceConnection;
                AppServiceConnected?.Invoke(this, args.TaskInstance.TriggerDetails as AppServiceTriggerDetails);
            }
        }
    }

    /// <summary>
    /// Task canceled here means the app service client is gone
    /// </summary>
    private void OnTaskCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
    {
        AppServiceDeferral?.Complete();
        AppServiceDeferral = null;
        Connection = null;
        AppServiceDisconnected?.Invoke(this, null);
    }
}

在桌面项目的 Program.cs 中添加:

internal class Program
{
    private static void Main(string[] args)
    {
        Communication.InitializeAppServiceConnection();
        ...
        while (true)
        {
            Thread.Sleep(100);
        }
    }
    ...
}

在解决方案配置管理器中,三个项目的平台需保持一致,建议都设为x64。生成都要勾选。部署只需勾选打包项目,UWP 项目和桌面项目都不需部署。
解决方案的启动项目应设为打包项目。

注意事项

  1. 具体使用方法请查看 Demo
  2. 请不要在应用初始化过程中新建 Process 类,虽然我已经做了未加载完成的判断,但是这个判断可能会占用了线程导致应用初始化无法完成,如果真的要在应用初始化时新建 Process 类,请把它挪到别的线程去,或者提出 issue 帮助我解决这个问题。

Star 数量统计

Star 数量统计

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.5 299 3/16/2023
0.0.4 664 2/26/2022
0.0.3 439 2/19/2022
0.0.2 442 2/19/2022
0.0.1 439 2/19/2022