乘风破浪,基于XAML岛(Xaml Islands)唤新WPF和WinForm应用,创建现代化应用体验

什么是XAML岛(Xaml Islands)

简介

Windows10 1903版开始,可以使用称为“XAML岛(Xaml Islands)”的功能在非UWP桌面应用程序中托管WinRT XAML控件。可以通过此功能来改进现有WPFWindows窗体C++桌面(Win32)应用程序的外观和功能,并使用只能通过WinRT XAML控件使用的最新Windows10 UI功能。这意味着,可以在现有的WPFWindows窗体C++桌面应用程序中使用UWP功能(例如Windows Ink)和支持Fluent Design System的控件。

image

可以托管派生自Windows.UI.Xaml.UIElement的任何WinRT XAML控件,其中包括:

  • Windows SDKWinUI2.x库提供的任何第一方WinRT XAML控件。
  • 任何自定义WinRT XAML控件(例如,包含多个可一起使用的WinRT XAML控件的用户控件)。必须有自定义控件的源代码,才能通过应用程序对其进行编译。

从根本上讲,XAML岛使用UWP XAML托管API创建。此API包含Windows 10版本1903 SDK中引入的几个Windows运行时类和COM接口。我们还在Windows社区工具包中提供了一组XAML岛.NET控件(此工具包在内部使用UWP XAML托管API),并针对WPF和Windows窗体应用提供了更方便的开发体验。

XAML岛(Xaml Islands)的体系结构

image

XAML岛具有以下运行时要求

  • Windows 10版本1903或更高版本。
  • 如果你的应用程序未打包到MSIX包中用于部署,则必须在计算机上安装Visual C++运行时。

反馈问题

基于XAML岛(Xaml Islands)唤新WPF应用和WinForm应用

https://github.com/TaylorShi/HelloXamlIslands

创建名为HelloXamlIslands的解决方案

通过Dotnet-Cli创建一个名为HelloXamlIslands的解决方案。

dotnet new sln -o HelloXamlIslands

image

切换到它的目录中。

cd .\HelloXamlIslands\

创建.Net Core 3.1的WPF应用

通过Dotnet-Cli创建一个.Net Core版本为3.1的Wpf应用,我们叫它demoForWpf3.1吧。

dotnet new wpf -o demoForWpf3.1 -f netcoreapp3.1

image

将其加入HelloXamlIslands解决方案中。

dotnet sln add .\demoForWpf3.1\demoForWpf3.1.csproj

image

切换到它目录。

 cd .\demoForWpf3.1\

通过Dotnet-CliRun命令来运行它。

dotnet watch run

image

运行效果如图:

image

添加Microsoft.Toolkit.Wpf控件库包

dotnet add package Microsoft.Toolkit.Wpf.UI.Controls

https://www.nuget.org/packages/Microsoft.Toolkit.Wpf.UI.Controls

image

创建.Net Core 3.1的WinForm应用

通过Dotnet-Cli创建一个.Net Core版本为3.1的WinForm应用,我们叫它demoForWinform3.1吧。

dotnet new winforms -o demoForWinform3.1 -f netcoreapp3.1

image

将其加入HelloXamlIslands解决方案中。

dotnet sln add .\demoForWinform3.1\demoForWinform3.1.csproj

image

切换到它目录。

 cd .\demoForWinform3.1\

通过Dotnet-CliRun命令来运行它。

dotnet watch run

image

运行效果如图:

image

添加Microsoft.Toolkit.Forms控件库包

dotnet add package Microsoft.Toolkit.Forms.UI.Controls

https://www.nuget.org/packages/Microsoft.Toolkit.Forms.UI.Controls

image

将解决方案配置为面向X86/X64平台

大多数XAML岛方案在面向任何CPU的项目中不受支持。

  1. 在“解决方案资源管理器”中,右键单击相应的解决方案节点,选择“属性” -> “配置属性” -> “配置管理器” 。
  2. 在“活动解决方案平台” 下,选择“新建” 。
  3. 在“新建解决方案平台”对话框中,选择“x64”或“x86”,并按“确认” 。
  4. 关闭打开的对话框。

创建目标不低于Windows 10 1903的UWP应用

先用Vs2019打开sln文件。

image

在解决方案上右键添加,选择新建项目

image

搜索项目模板关键词UWP,选择空白应用(通用Windows)模板进行下一步

image

输入项目名demoForUwp,点击创建按钮。

image

项目的最低目标版本需要用Windows10, Version 1903(10.0;版本 18362),至于最高目标版本就默认值吧。

image

点击确定,完成创建。

image

添加Microsoft.Toolkit.Win32控件库包

https://www.nuget.org/packages/Microsoft.Toolkit.Win32.UI.XamlApplication

demoForUwp上右键,找到管理Nuget程序包选项。

image

搜索关键词Microsoft.Toolkit.Win32.UI.XamlApplication,进行安装。

image

image

切换到X86或者X64模式,运行一下看看。

image

改造UWP项目,准备Islands

找到demoForUwp项目中的App.xaml文件,双击打开它。

<Application
    x:Class="demoForUwp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:demoForUwp">

</Application>

image

App.xaml上述内容替换下:

<xaml:XamlApplication
    x:Class="demoForUwp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:xaml="using:Microsoft.Toolkit.Win32.UI.XamlHost"
    xmlns:local="using:demoForUwp">
</xaml:XamlApplication>

或者简化为:

<xaml:XamlApplication
    x:Class="demoForUwp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:demoForUwp"
    xmlns:xaml="using:Microsoft.Toolkit.Win32.UI.XamlHost" />

image

然后找到App.xaml.cs,打开它。

image

替换所有内容为:

namespace demoForUwp
{
    public sealed partial class App : Microsoft.Toolkit.Win32.UI.XamlHost.XamlApplication
    {
        public App()
        {
            this.Initialize();
        }
    }
}

image

然后删除掉项目模板自动生成的MainPage.xaml及其MainPage.xaml.cs文件。

image

重新生成一次项目。

image

在WPF项目中引进UWP项目

参考

posted @ 2021-07-13 00:11  TaylorShi  阅读(1643)  评论(0编辑  收藏  举报