按照微軟的官方檔案,大部分的檔案都會說如果用了 XAML Islands 等技術的時候,需要新建一個打包專案,將 WPF 應用打包為 msix 等才可以進行分發和使用,但是實際上不打包也可以,此時可以和此前的 Win32 應用一樣的分發方式進行分發,可以支持到 Win7 系統,當然了在 Win7 系統上可用不了 UWP 的控制元件,但是至少應用軟體自身可以在 Win7 繼續運行的,可以通過判斷系統版本決定功能是否開放,如是 Win10 版本,那么開放 UWP 控制元件部分的使用
如果新建一個空的 .NET Core 3.1 的 WPF 專案,然后只是安裝了必要的 NuGet 包之后,就在 XAML 界面里面添加了 UWP 的控制元件,如筆跡控制元件,那么此時將不能成功運行應用,如下面的代碼方式,在 csproj 添加如下的代碼
<ItemGroup>
<PackageReference Include="Microsoft.Toolkit.Win32.UI.SDK" Version="6.1.2" />
<PackageReference Include="Microsoft.Toolkit.Win32.UI.XamlApplication" Version="6.1.3" />
<PackageReference Include="Microsoft.Toolkit.Wpf.UI.Controls" Version="6.1.2" />
<PackageReference Include="Microsoft.Toolkit.Wpf.UI.XamlHost" Version="6.1.2" />
</ItemGroup>
在 XAML 里面使用如下代碼參考了 UWP 的筆跡控制元件
<Window x:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:LaykearduchuNachairgurharhear"
xmlns:controls="clr-namespace:Microsoft.Toolkit.Wpf.UI.Controls;assembly=Microsoft.Toolkit.Wpf.UI.Controls"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<controls:InkCanvas x:Name="InkCanvas" DockPanel.Dock="Top" Loaded="InkCanvas_Loaded" />
</Grid>
</Window>
此時運行應用將會報錯,提示沒有在 Win10 下運行,如下面代碼
Exception: 災難性故障
WindowsXamlManager and DesktopWindowXamlSource are supported for apps targeting Windows version 10.0.18226.0 and later. Please check either the application manifest or package manifest and ensure the MaxTestedVersion property is updated.
解決方法有兩個,第一個是通過微軟檔案說的打包的方法,再新建一個打包工程,在這個工程里面打包作為 MSIX 安裝包,第二個方法是在原有的 WPF 專案中添加應用清單,在應用清單設定可以在 Win10 使用
右擊專案添加應用清單 App.manifest 檔案,在此檔案添加 <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" /> 表示支持 Win10 系統以及加上 DPI 功能,以下是我的 App.manifest 檔案
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- 設計此應用程式與其一起作業且已針對此應用程式進行測驗的
Windows 版本的串列,取消評論適當的元素,
Windows 將自動選擇最兼容的環境, -->
<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>
<!-- 指示該應用程式可以感知 DPI 且 Windows 在 DPI 較高時將不會對其進行
自動縮放,Windows Presentation Foundation (WPF)應用程式自動感知 DPI,無需
選擇加入,選擇加入此設定的 Windows 表單應用程式(目標設定為 .NET Framework 4.6 )還應
在其 app.config 中將 "EnableWindowsFormsHighDpiAutoResizing" 設定設定為 "true",-->
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/PM</dpiAware>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2, PerMonitor</dpiAwareness>
</windowsSettings>
</application>
</assembly>
此時即可讓此 WPF 應用運行,如果想要發布出去,還請右擊專案選擇發布,就和發布其他 .NET Core 應用的方式進行發布
以上的代碼放在 github 和 gitee 歡迎訪問
可以通過如下方式獲取本文的源代碼,先創建一個空檔案夾,接著使用命令列 cd 命令進入此空檔案夾,在命令列里面輸入以下代碼,即可獲取到本文的代碼
git init
git remote add origin https://gitee.com/lindexi/lindexi_gd.git
git pull origin eb289cd0d1c7e273df4c7bae6a7bcd17fb12aa6a
以上使用的是 gitee 的源,如果 gitee 不能訪問,請替換為 github 的源
git remote remove origin
git remote add origin https://github.com/lindexi/lindexi_gd.git
獲取代碼之后,進入 LaykearduchuNachairgurharhear 檔案夾
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/294336.html
標籤:.NET技术
