Maui Blazor windows程序无法通过双击 bin 文件夹中的 exe打开程序的解决办法


使用Visual Studio创建了一个.NET Maui Blazor 应用,通过Visual Studio调试Windows应用时,需要打开开发者模式

在这里插入图片描述

打开开发者模式,调试一切正常,但如果直接运行bin文件夹下对应目录的exe时,没有任何反应,事件查看器中,会有这样的提示:

Exception Info: System.DllNotFoundException: Unable to load DLL 'Microsoft.ui.xaml.dll' or one of its dependencies: 找不到指定的模块。

那么如何才可以打包exe文件呢?当然少不了搜索操作了。
“查找资料"发现,要运行Maui生成的exe文件,必须通过MSIX Packaging Tool来生成。

也有高手说,通过Fuslogvw找到所有的依赖项,如果运气好的话,可以成功运行,也可以打包成zip发送给其他电脑上。

那么如何制作制作一个 “可部署” 的windows应用呢?

创建签名证书

备注:创建和使用自签名证书时,只有安装和信任证书的用户才能运行应用。

1、PowerShell 终端,并使用项目导航到目录。
2、使用New-SelfSignedCertificate使用命令生成自签名证书。

New-SelfSignedCertificate -Type Custom `
                          -Subject "CN=Test" `
                          -KeyUsage DigitalSignature `
                          -FriendlyName "My temp dev cert" `
                          -CertStoreLocation "Cert:\CurrentUser\My" `
                          -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.3", "2.5.29.19={text}")

3、使用Get-ChildItem命令查询已创建的证书的证书存储:

Get-ChildItem "Cert:\CurrentUser\My" | Format-Table Subject, FriendlyName, Thumbprint

可以看到以下类似结果:

Thumbprint                               Subject                                  FriendlyName
----------                               -------                                  ------------

07AD38F3B646F5AAC16F2F2570CAE40F4842BBE0 CN=Contoso                               My temp dev cert

4、记录下Thumbprint的值,后面需要使用。

配置生成设置

双击项目名称,或导航到项目根目录,打开.csproj文件,在与之间,加上如下配置项

<PropertyGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows' and '$(Configuration)' == 'Release'">
    <AppxPackageSigningEnabled>true</AppxPackageSigningEnabled>
    <PackageCertificateThumbprint>07AD38F3B646F5AAC16F2F2570CAE40F4842BBE0</PackageCertificateThumbprint>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows' and '$(RuntimeIdentifierOverride)' != ''">
    <RuntimeIdentifier>$(RuntimeIdentifierOverride)</RuntimeIdentifier>
</PropertyGroup>

将PackageCertificateThumbprint节点的值,换成刚才记录下的Thumbprint的值。整个文件看起来是这样的:

<Project Sdk="Microsoft.NET.Sdk.Razor">

    <PropertyGroup>
        <TargetFrameworks>net6.0-android;net6.0-ios;net6.0-maccatalyst</TargetFrameworks>
        <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net6.0-windows10.0.19041.0</TargetFrameworks>
        <!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
        <!-- <TargetFrameworks>$(TargetFrameworks);net6.0-tizen</TargetFrameworks> -->
        <OutputType>Exe</OutputType>
        <RootNamespace>MauiApp1</RootNamespace>
        <UseMaui>true</UseMaui>
        <SingleProject>true</SingleProject>
        <ImplicitUsings>enable</ImplicitUsings>
        <EnableDefaultCssItems>false</EnableDefaultCssItems>

        <!-- Display name -->
        <ApplicationTitle>MauiApp1</ApplicationTitle>

        <!-- App Identifier -->
        <ApplicationId>com.companyname.mauiapp1</ApplicationId>
        <ApplicationIdGuid>E0ADEA09-F808-4CAC-B28B-0C409C8B032B</ApplicationIdGuid>

        <!-- Versions -->
        <ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
        <ApplicationVersion>1</ApplicationVersion>

        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">14.2</SupportedOSPlatformVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">14.0</SupportedOSPlatformVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">24.0</SupportedOSPlatformVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
        <TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
    </PropertyGroup>

    <ItemGroup>
        <!-- App Icon -->
        <MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />

        <!-- Splash Screen -->
        <MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />

        <!-- Images -->
        <MauiImage Include="Resources\Images\*" />
        <MauiImage Update="Resources\Images\dotnet_bot.svg" BaseSize="168,208" />

        <!-- Custom Fonts -->
        <MauiFont Include="Resources\Fonts\*" />

        <!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
        <MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
    </ItemGroup>
	<PropertyGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows' and '$(Configuration)' == 'Release'">
		<AppxPackageSigningEnabled>true</AppxPackageSigningEnabled>
		<PackageCertificateThumbprint>A10612AF095FD8F8255F4C6691D88F79EF2B135E</PackageCertificateThumbprint>
	</PropertyGroup>
	<PropertyGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows' and '$(RuntimeIdentifierOverride)' != ''">
		<RuntimeIdentifier>$(RuntimeIdentifierOverride)</RuntimeIdentifier>
	</PropertyGroup>
</Project>

发布

打开 VS 终端的开发人员命令提示符 并导航到.NET MAUI 应用项目的文件夹,运行dotnet publish 命令。并提供一下参数

参数
-f net6.0-windows{version}目标框架,它是 Windows TFM,例如 net6.0-windows10.0.19041.0。 确保此值与 .csproj 文件中节点的值相同。
-c Release设置生成配置,即 Release。
/p:RuntimeIdentifierOverride=win10-x64 或 /p:RuntimeIdentifierOverride=win10-x86避免 WindowsAppSDK 问题 #2940 中详述的 bug。 -x64根据目标平台选择参数的或-x86版本。

例如:

dotnet publish -f net6.0-windows10.0.19041.0 -c Release /p:RuntimeIdentifierOverride=win10-x64

在这里插入图片描述
如果一切顺利,在bin目录下,会生成 Release\net6.0-windows10.0.19041.0\win10-x64\AppPackages\MauiApp1_1.0.0.1_Test文件夹,MauiApp1_1.0.0.1_Test 是我生成的目录,他在实际中,应该是实际创建的应用名称,在MauiApp1_1.0.0.1_Test目录下,会有一个msix 文件,这就是生成的 应用包,应用包并非传统的exe或者msi文件,而是全新的安装包格式

安装应用

若要安装应用,必须使用已信任的证书进行签名。 如果不是,Windows 不会让你安装应用。 将显示如下所示的对话框,其中禁用了“安装”按钮:

在这里插入图片描述
这里需要信任证书操作,
1、右键单击 .msix 文件,然后选择 “属性”。

2、选择 “数字签名 ”选项卡。

3、选择证书,然后点击 详细信息

在这里插入图片描述
4、选择 “查看证书”。

5、选择 “安装证书…

6、选择 “本地计算机”,然后选择“ 下一步”。

7、如果用户帐户控制提示 你希望允许此应用对设备进行更改?,选择“ ”。

8、在“证书导入向导” 窗口中,选择“将所有证书放在以下存储区中”。

9、选择“浏览…”,然后选择“受信任人”。 选择“确定”关闭对话框。

在这里插入图片描述
这时会提示导入成功

在这里插入图片描述
点击确定关闭对话框,再次双击 .msix,已可以正常安装了。

在这里插入图片描述
至此,成功打包了可安装的应用,安装完成后,在开始菜单就可以找到安装好的应用程序。并可以正常打开了
在这里插入图片描述
目前来说,MAUI应用程序只支持所谓的“打包”应用。需要发布到 MSIX 并安装它们才能运行。未打包即可使用的场景,官方也正在努力。我们可以到https://github.com/dotnet/maui/issues/3166 跟踪进度。


参考:
发布适用于Windows的.NET MAUI应用:https://learn.microsoft.com/zh-cn/dotnet/maui/windows/deployment/overview

创建用于包签名的证书:https://learn.microsoft.com/zh-cn/windows/msix/package/create-certificate-package-signing

New-SelfSignedCertificate:https://learn.microsoft.com/zh-cn/powershell/module/pki/new-selfsignedcertificate?view=windowsserver2019-ps&preserve-view=true

什么是 MSIX?:https://learn.microsoft.com/zh-cn/windows/msix/overview

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

倾斜的水瓶座

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值