C#winform軟體實現一次編譯,跨平臺windows和linux相容執行,相容Visual Studio原生介面Form表單開發

親善美發表於2023-10-27

一、背景:

微軟的.net core開發工具,目前來看,winform介面軟體還沒有打算要支援linux系統下執行的意思,要想讓c#桌面軟體在linux系統上執行,開發起來還比較麻煩。微軟只讓c#的控制檯軟體支援在linux執行。

二、解決方案:

我想到的一個方案是自定義封裝軟體的System.Windows.Forms元件,把支援windows和linux的介面框架GTK封裝進System.Windows.Forms中!

三、元件封裝

這個System.Windows.Forms是實現C#介面的關鍵元件,Form介面的所有控制元件都封裝在這個元件裡。在.net core環境裡,這個元件在框架Microsoft.WindowsDesktop.App.WindownsForms下。當開發工程的輸出模式是“windows應用程式”時,就會自動引用Microsoft.WindowsDesktop.App.WindownsForms,如果開發工程的輸出模式是“控制檯應用程式”時,工程不會引用Microsoft.WindowsDesktop.App.WindownsForms,也無法開桌面軟體的介面。

為了相容VS原生介面表單開發,我開發了這個元件GTKSystem.Windows.Forms,這個元件的控制元件類庫名稱空間和類名稱沿用了原生System.Windows.Forms的類庫名稱,可以在原生開發的C#軟體工程裡,直接引用GTKSystem.Windows.Forms就能相容執行。

四、技術開發

目前實現的控制元件:Form、Button、CheckBox、CheckedListBox、ComboBox、DataGridView、DateTimePicker、GroupBox、Label、LinkLabel、MaskedTextBox、MenuStrip、MonthCalendar、NumericUpDown、Panel、PictureBox、RadioButton、RichTextBox、SplitContainer、SplitterPanel、TabControl、TextBox、TreeView

以上控制元件都只實現了常用功能的屬性和方法,事件主要實現了滑鼠事件、驗證事件、載入事件,還有很多平常不用的屬性事件已經實現了介面,但是沒有實現執行功能,主要是因為程式量太多,沒有去做。對於有能力的開發人員,元件也是可以拿到相關的屬性事件(如WidgetEvent)去實現需要的功能。

重寫的類也很多,重點說一下這幾個類:Bitmap、Image、System.ComponentModel.ComponentResourceManager、System.Resources.ResourceManager。這幾個類是Form介面引用影像資源必需的。在控制檯程式架構裡,是沒有Bitmap、Image類庫的,而且ComponentResourceManager和ResourceManager都不能讀取資源影像資料。我在GTKSystem.Windows.Forms裡封裝實現了Bitmap和Image類,實現了ComponentResourceManager和ResourceManager讀取資源影像資料。

Bitmap和Image類除了常用屬性外,新增屬性Image.PixbufData存放影像資料,用於GTKSystem的使用。

ComponentResourceManager和ResourceManager主要是實現了GetObject方法,讀取資源資料。

五、使用方法

修改.net core的windows應用程式工程屬性,把輸出型別改為“控制檯應用程式”,或者把windows窗體配置勾選去掉,配置變為<UseWindowsForms>false</UseWindowsForms>。

<PropertyGroup>
<OutputType>WinExe</OutputType>
<UseWindowsForms>false</UseWindowsForms>
</PropertyGroup>

1、新建System.Resources.ResourceManager類
在專案下新建System.Resources.ResourceManager類,繼承GTKSystem.Resources.ResourceManager,用於覆蓋原生System.Resources.ResourceManager類。
GTKSystem.Resources.ResourceManager實現了專案資原始檔和影像檔案讀取。
如果專案裡沒有使用資源影像檔案,可以不用新建此檔案。

2、新建System.ComponentModel.ComponentResourceManager類
在專案下新建System.ComponentModel.ComponentResourceManager類,繼承GTKSystem.ComponentModel.ComponentResourceManager,用於覆蓋原生System.ComponentModel.ComponentResourceManager類。
GTKSystem.ComponentModel.ComponentResourceManager實現了專案資原始檔和影像檔案讀取(呼叫GTKSystem.Resources.ResourceManager)。
如果專案裡沒有使用資源影像檔案,可以不用新建此檔案。

3、GTKWinFormsApp.csproj
配置UseWindowsForms為false,或者使用控制檯應用程式
<UseWindowsForms>false</UseWindowsForms>

4、引用GTKSystem.Windows.Forms、System.Resources.Extensions
System.Resources.Extensions是空程式dll,VS載入Form介面時驗證需要此dll.

5、GTKWinFormsApp\obj\Debug\netcoreapp3.1\GTKWinFormsApp.designer.runtimeconfig.json
GTKWinFormsApp\obj\Release\netcoreapp3.1\GTKWinFormsApp.designer.runtimeconfig.json
將name設定為Microsoft.WindowsDesktop.App,用於VS支援視覺化Form表單,重新載入工程或重啟VS
"runtimeOptions": {
"framework": {
"name": "Microsoft.WindowsDesktop.App"
},

六、使用效果:

VS開發介面:

執行效果:

 

最後:

此程式在統信系統(linux)上測試完美執行,實現一次編譯,跨平臺執行,顯示介面樣式與windows上執行的顯示效果基本一樣。

目前這個元件沒有完全完成,但是主要功能和技術難點都已經解決,現公佈出來給有需要的開發人員參考。

專案下載:https://files.cnblogs.com/files/easywebfactory/WinFormsAppDemo.zip?t=1698380585&download=true

 

相關文章