dotnet 使用 XWT 构建跨平台客户端 入门篇

  • A+
所属分类:.NET技术
摘要

本文告诉大家如何入门开始开发一个基于 mono 组织开源的 XWT 跨平台客户端 UI 框架的应用,本文的 xwt 是在 GitHub 上完全开源的,基于 MIT 协议的,底层采用 GTK# 的 UI 框架

本文告诉大家如何入门开始开发一个基于 mono 组织开源的 XWT 跨平台客户端 UI 框架的应用,本文的 xwt 是在 GitHub 上完全开源的,基于 MIT 协议的,底层采用 GTK# 的 UI 框架

此 xwt 在设计上是通过 mono 进行跨平台运行的,而非采用 dotnet core 方式进行跨平台运行的,因此暂时只能使用 dotnet framework 4.7.2 版本,而不支持 dotnet core 版本。至少在 2021.08.09 依然是不支持在 dotnet core 运行

在开始之前,将会因为 xwt 依赖 GTK# 的环境而需要先部署 GTK# 的开发环境

官网 下载 GTK# 的 x86 安装包或 mono x86 的应用

安装到默认路径,也就是在 C:Program Files (x86)GtkSharp2.12bin 路径,默认安装的时候会加入到环境变量。详细请参阅 Xamarin 使用 GTK 提示找不到 libglib-2.0-0.dll 找不到

接着打开 VisualStudio 完成 .NET Framework 4.7.2 和 dotnet core 负载(为了mono的默认安装)的安装,以及桌面开发 (xwt 可以基于 wpf 版本)的安装

以上就是所有的环境部署步骤,对于大部分开发者,只需要去下载 GtkSharp 安装即可。我依然推荐安装到默认路径,无论是 GtkSharp 还是 VisualStudio 都放在默认路径

新建一个 dotnet core 控制台项目

修改 csproj 项目文件,替换为如下代码

<project sdk="Microsoft.NET.Sdk">    <propertygroup>     <outputtype>Exe</outputtype>     <targetframeworks>net472;netcoreapp3.1</targetframeworks>     <platformtarget>x86</platformtarget>   </propertygroup>    <itemgroup>     <none include="C:Program Files %28x86%29GtkSharp2.12bin*.dll" link="%(FileName).dll">       <copytooutputdirectory>PreserveNewest</copytooutputdirectory>     </none>   </itemgroup>    <itemgroup>     <packagereference include="Xwt.Gtk" version="0.2.247">   </packagereference></itemgroup>  </project> 

以上代码就是设置框架是 .NET Framework 4.7.2 和 dotnet core 3.1 两个版本,当然当前是不能跑 dotnet core 3.1 的版本的。接着因为 GTK# 当前只支持客户端的 x86 版本,因此需要设置 PlatformTarget 作为 x86 版本

默认是不会去找到 GTK Sharp 的 DLL 内容的,因此通过如下代码引用所有的 DLL 用来输出

  <itemgroup>     <none include="C:Program Files %28x86%29GtkSharp2.12bin*.dll" link="%(FileName).dll">       <copytooutputdirectory>PreserveNewest</copytooutputdirectory>     </none>   </itemgroup> 

打开 Program.cs 文件,添加如 xwt 官方开源仓库 https://github.com/mono/xwt 的例子

        [STAThread]         static void Main(string[] args)         {             Application.Initialize(ToolkitType.Gtk);              var mainWindow = new Window()             {                 Title = "Xwt Demo Application",                 Width = 500,                 Height = 400             };             mainWindow.Show();             Application.Run();         } 

选择 .NET Framework 4.7.2 版本,尝试运行一下应用。默认即可看到显示了窗口

dotnet 使用 XWT 构建跨平台客户端 入门篇

本文所有代码在 githubgitee 上完全开源

不嫌弃麻烦的话,还请自行下载代码,自己构建。可以通过如下方式获取本文的源代码,先创建一个空文件夹,接着使用命令行 cd 命令进入此空文件夹,在命令行里面输入以下代码,即可获取到本文的代码

git init git remote add origin https://gitee.com/lindexi/lindexi_gd.git git pull origin 11125ca50dc91e50cf581c36476f03b853bc7ef8 

以上使用的是 gitee 的源,如果 gitee 不能访问,请替换为 github 的源

git remote remove origin git remote add origin https://github.com/lindexi/lindexi_gd.git 

获取代码之后,进入 LarwearceceyokuDokealuwo 文件夹