如何在Avalonia C#项目配置AppxManifest.xml注册到Windows共享菜单
在Avalonia C#项目中注册Windows 10共享菜单(无需付费数字证书)
核心思路
Windows 10的共享菜单依赖UWP的ShareTarget机制,但Avalonia作为桌面应用,可通过MSIX打包+自签名证书实现注册——不用购买商业证书,Windows开发者模式允许安装自签名的MSIX包,完全满足业余开源项目需求。
步骤1:开启Windows开发者模式
- 打开Windows设置 → 更新和安全 → 开发者选项
- 勾选「开发者模式」,允许安装未经过微软商店签名的应用包
步骤2:配置AppxManifest.xml
在你的Avalonia项目中添加并编辑Package.appxmanifest文件:
- 右键项目 → 添加 → 新建项 → 搜索「Package Manifest」并添加
- 替换文件内容为以下模板,根据你的需求修改:
<?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" IgnorableNamespaces="uap rescap"> <Identity Name="com.yourname.yourapp" Publisher="CN=你的名字" Version="1.0.0.0" /> <Properties> <DisplayName>你的程序显示名称</DisplayName> <PublisherDisplayName>你的名字</PublisherDisplayName> <Logo>Assets\StoreLogo.png</Logo> </Properties> <Dependencies> <TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.17763.0" MaxVersionTested="10.0.22621.0" /> </Dependencies> <Resources> <Resource Language="x-generate" /> </Resources> <Applications> <Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="Windows.FullTrustApplication"> <uap:VisualElements DisplayName="你的程序显示名称" Description="你的程序描述" BackgroundColor="transparent" Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png"> <uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png" /> </uap:VisualElements> <!-- 关键:添加共享目标扩展 --> <Extensions> <uap:Extension Category="windows.shareTarget"> <uap:ShareTarget> <uap:DisplayName>通过我的程序分享</uap:DisplayName> <uap:SupportedFileTypes> <!-- 替换成你的程序支持的文件类型 --> <uap:FileType>.txt</uap:FileType> <uap:FileType>.md</uap:FileType> <uap:FileType>.pdf</uap:FileType> </uap:SupportedFileTypes> <uap:DataFormat>Text</uap:DataFormat> <uap:DataFormat>StorageItems</uap:DataFormat> </uap:ShareTarget> </uap:Extension> </Extensions> </Application> </Applications> <Capabilities> <rescap:Capability Name="runFullTrust" /> <uap:Capability Name="sharedUserCertificates" /> </Capabilities> </Package>
- 注意:
EntryPoint必须设为Windows.FullTrustApplication,因为Avalonia是桌面全信任应用,不是UWP应用 - 按需修改
SupportedFileTypes和DataFormat,匹配你的程序能处理的内容类型
步骤3:处理共享数据接收逻辑
在Avalonia项目中添加代码处理共享过来的数据:
- 安装NuGet包:
Microsoft.Windows.SDK.Contracts(用于访问UWP的共享API) - 在程序入口添加激活检查逻辑:
using Avalonia; using Windows.ApplicationModel.Activation; using Windows.ApplicationModel.DataTransfer; namespace YourAppNamespace; class Program { [STAThread] public static void Main(string[] args) { var builder = BuildAvaloniaApp(); // 检查是否是共享激活触发 if (AppInstance.GetCurrent().GetActivatedEventArgs() is ShareTargetActivatedEventArgs shareArgs) { _ = HandleShareOperationAsync(shareArgs.ShareOperation); } builder.StartWithClassicDesktopLifetime(args); } private static async Task HandleShareOperationAsync(ShareOperation shareOperation) { try { // 处理共享的文件 if (shareOperation.Data.Contains(StandardDataFormats.StorageItems)) { var storageItems = await shareOperation.Data.GetStorageItemsAsync(); foreach (var item in storageItems) { if (item is Windows.Storage.StorageFile file) { var filePath = file.Path; // 在这里添加你的文件处理逻辑,比如打开文件 } } } // 处理共享的文本 if (shareOperation.Data.Contains(StandardDataFormats.Text)) { var sharedText = await shareOperation.Data.GetTextAsync(); // 处理文本逻辑 } } finally { // 必须标记共享操作完成,否则系统会认为程序未响应 shareOperation.ReportCompleted(); } } public static AppBuilder BuildAvaloniaApp() => AppBuilder.Configure<App>() .UsePlatformDetect() .WithInterFont() .LogToTrace(); }
步骤4:打包并安装(无需付费签名)
- 在Visual Studio中右键项目 → 发布 → 选择「创建MSIX包」
- 按照向导选择「旁加载」,然后点击「创建自签名证书」,填写证书名称(比如“我的开发证书”)
- 完成打包后,双击生成的MSIX文件安装——因为开启了开发者模式,系统会允许安装自签名包
- 测试:右键任意支持的文件 → 共享 → 选择你的程序,验证是否能正常接收数据
不推荐的替代方案:手动注册表注册
如果不想用MSIX,也可以手动修改注册表注册共享目标,但需要实现COM接口IShareTargetHandler并注册COM组件,步骤繁琐且容易出错,不适合业余开发者,这里就不展开了。
内容的提问来源于stack exchange,提问作者Bip901
相关产品推荐
相关产品推荐

