如何在 WinUi3 中实现 AppService?

huangapple go评论64阅读模式
英文:

How can I implement an AppService in WinUi3?

问题

I'm attempting to upgrade my UWP app to WinUI3 and I cannot get the AppService working in WinUI3.

So far I've tried the following:

  • Upgrade tool on working UWP minimal example. - AppUnavailable
  • Use Universal Windows project. - Error versions not compatible.
  • .Net 7 project - AppUnavailable
  • OutputType: winmdobj - Error versions not compatible.
  • Experimental Windows Runtime Component C# - Error versions not compatible.

The Microsoft documentation is lacking around AppServices and only the UWP documentation is available.

Is it possible to get an AppService working with WinUI3? And if so, what are the requirements of the projects?

Here's my current setup, not much is different from UWP version.

RuntimeComponent1.csproj



net7.0-windows10.0.22621
Library
en-US
10.0.17763.0
false
true
true
app.manifest
true
x86;x64;arm64
win10-$(Platform).pubxml
win10-x86;win10-x64;win10-arm64






MyAppService.cs

using System;
using Windows.ApplicationModel.Background;

namespace RuntimeComponent1
{
public sealed class MyAppService : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
//breakpoint
Console.WriteLine("Hello World");
}
}
}

英文:

I'm attempting to upgrade my UWP app to WinUI3 and I cannot get the AppService working in WinUI3.

So far I've tried the following:

  • Upgrade tool on working UWP minimal example. - AppUnavailable
  • Use Universal Windows project. - Error versions not compatible.
  • .Net 7 project - AppUnavailable
  • OutputType: winmdobj - Error versions not compatible.
  • Experimental Windows Runtime Component C# - Error versions not compatible.

The Microsoft documentation is lacking around AppServices and only the UWP documentation is available.

Is it possible to get an AppService working with WinUI3? And if so, what are the requirements of the projects?

Here's my current setup, not much is different from UWP version.

如何在 WinUi3 中实现 AppService?

RuntimeComponent1.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net7.0-windows10.0.22621</TargetFramework>
    <OutputType>Library</OutputType>
    <DefaultLanguage>en-US</DefaultLanguage>
    <TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
    <AllowCrossPlatformRetargeting>false</AllowCrossPlatformRetargeting>
    <CsWinRTComponent>true</CsWinRTComponent>
    <UseWinUI>true</UseWinUI>
    <ApplicationManifest>app.manifest</ApplicationManifest>
    <EnablePreviewMsixTooling>true</EnablePreviewMsixTooling>
    <Platforms>x86;x64;arm64</Platforms>
    <PublishProfile>win10-$(Platform).pubxml</PublishProfile>
    <RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
    <OutputPath>bin\x86\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
    <NoWarn>;2008</NoWarn>
    <UseVSHostingProcess>false</UseVSHostingProcess>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
    <OutputPath>bin\x86\Release\</OutputPath>
    <DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
    <NoWarn>;2008</NoWarn>
    <UseVSHostingProcess>false</UseVSHostingProcess>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|ARM'">
    <OutputPath>bin\ARM\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
    <NoWarn>;2008</NoWarn>
    <UseVSHostingProcess>false</UseVSHostingProcess>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|ARM'">
    <OutputPath>bin\ARM\Release\</OutputPath>
    <DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
    <NoWarn>;2008</NoWarn>
    <UseVSHostingProcess>false</UseVSHostingProcess>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|ARM64'">
    <OutputPath>bin\ARM64\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
    <NoWarn>;2008</NoWarn>
    <UseVSHostingProcess>false</UseVSHostingProcess>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|ARM64'">
    <OutputPath>bin\ARM64\Release\</OutputPath>
    <DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
    <NoWarn>;2008</NoWarn>
    <UseVSHostingProcess>false</UseVSHostingProcess>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
    <OutputPath>bin\x64\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
    <NoWarn>;2008</NoWarn>
    <UseVSHostingProcess>false</UseVSHostingProcess>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
    <OutputPath>bin\x64\Release\</OutputPath>
    <DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
    <NoWarn>;2008</NoWarn>
    <UseVSHostingProcess>false</UseVSHostingProcess>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.WindowsAppSDK" Version="1.1.0" />
	  <PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.0.2" />
  </ItemGroup>
</Project>

MyAppService.cs

using System;
using Windows.ApplicationModel.Background;

namespace RuntimeComponent1
{
    public sealed class MyAppService : IBackgroundTask
    {
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            //breakpoint
            Console.WriteLine("Hello World");
        }
    }
}

答案1

得分: 0

目前,在WinUI3应用程序中无法使用mechanism。我建议您可以选择使用命名管道或其他IPC机制。

英文:

Currently, there is no way to use the mechanism. in a WinUI3 application. I'd suggest that you could go with a named pipe or other IPC mechanism.

答案2

得分: 0

我发现了一种解决方法。

我可以使用Windows应用程序打包项目(.wapproj)将UWP应用程序与WinUI3应用程序组合在一起。

步骤如下:

  • 创建UWP Runtime组件项目,与通常的方式一样。
  • 从UWP应用程序项目中引用UWP Runtime组件项目。
  • 从UWP项目中删除除清单之外的所有内容,并创建一个虚拟的主方法。
  • 创建具有WAP项目的WinUI3项目。如果使用现有项目,请确保WinUI3项目是WAP项目的默认应用程序。
  • 从WAP项目中引用UWP项目。
  • 在WAP清单中确保入口点是WinUI3项目。
  • 在WAP清单中的声明中设置AppService,指向Runtime组件中的AppService类。
英文:

I've discovered a workaround.

I can use a Windows Application Packaging Project (.wapproj) to combine a UWP app with a WinUI3 app.

The steps are:

  • Create UWP Runtime Component project for the AppService as per usual.
  • Reference the UWP Runtime Component project from the UWP Application project.
  • Remove everything from UWP project except the manifest and create a dummy main method.
  • Create WinUI3 project with WAP project. Or if using existing, ensure WinUI3 project is default application of WAP project
  • Reference UWP project from WAP project.
  • In WAP manifest ensure entry point is WinUI3 project.
  • In WAP manifest declarations set up AppService by pointing to AppService class in Runtime Component.

huangapple
  • 本文由 发表于 2023年4月19日 16:51:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定