700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Maui Blazor windows程序无法通过双击 bin 文件夹中的 exe打开程序的解决办法

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

时间:2020-02-18 20:35:30

相关推荐

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=".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: /Samsung/ --><!-- <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>panyname.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命令。并提供一下参数

例如:

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 并安装它们才能运行。未打包即可使用的场景,官方也正在努力。我们可以到/dotnet/maui/issues/3166 跟踪进度。

参考:

发布适用于Windows的.NET MAUI应用:/zh-cn/dotnet/maui/windows/deployment/overview

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

New-SelfSignedCertificate:/zh-cn/powershell/module/pki/new-selfsignedcertificate?view=windowsserver-ps&preserve-view=true

什么是 MSIX?:/zh-cn/windows/msix/overview

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。