C#winform software implements one-time compilation, is compatible with cross-platform windows, linux, and mac, and is compatible with Visual Studio native interface Form form development
1. Background:
Microsoft’s .net core development tool, currently, the winform interface software is not intended to support running on the Linux system. If you want C# desktop software to run on the Linux system, it is still troublesome to develop. Microsoft only allows C# console software to run on Linux.
2. Solution:
One solution I thought of was to customize the System.Windows.Forms component that encapsulates the software, and encapsulate the interface framework GTK that supports windows and Linux into System.Windows.Forms!
This plan has two characteristics:
1. Compatible with native C# form controls, the original C# software does not need to be redeveloped, just replace it with this component and recompile
2. You can use Visual Studio for visual development. There is no need to learn the framework, just reference the DLL
3. Component packaging
This System.Windows.Forms is the key component to implement the C# interface. All controls of the Form interface are encapsulated in this component. In the .net core environment, this component is under the framework Microsoft.WindowsDesktop.App.WindownsForms. When the output mode of the development project is “windows application”, Microsoft.WindowsDesktop.App.WindownsForms will be automatically referenced. If the output mode of the development project is “console application”, the project will not reference Microsoft.WindowsDesktop.App. .WindownsForms, and the desktop software interface cannot be opened.
In order to be compatible with VS native interface form development, I developed this component GTKSystem.Windows.Forms. The control class library namespace and class name of this component follow the class library name of the native System.Windows.Forms, and can be developed natively. In the C# software project, you can directly reference GTKSystem.Windows.Forms to run it compatiblely.
4. Technology development
Currently implemented controls:Form, Button, CheckBox, CheckedListBox, ComboBox, DataGridView, DateTimePicker, GroupBox, Label, LinkLabel, MaskedTextBox, MenuStrip, MonthCalendar, NumericUpDown, Panel, PictureBox, RadioButton, RichTextBox, SplitContainer, SplitterPanel , TabControl, TextBox, TreeView.
Implemented window components: MessageBox, ColorDialog, OpenFileDialog, SaveFileDialog, FolderBrowserDialog
The above controls only implement the properties and methods of common functions. The events mainly implement mouse events, verification events, and loading events. There are also many commonly used attribute events that have implemented interfaces, but have not implemented execution functions. Mainly Because there were too many procedures, I didn’t do it. For capable developers, components can also get related attribute events (such as WidgetEvent) to implement the required functions.
There are also many rewritten classes, focusing on these classes: Bitmap, Image, System.ComponentModel.ComponentResourceManager, System.Resources.ResourceManager. These classes are necessary for the Form interface to reference image resources. In the console program architecture, there are no Bitmap and Image class libraries, and neither ComponentResourceManager nor ResourceManager can read resource image data. I encapsulated and implemented Bitmap and Image classes in GTKSystem.Windows.Forms, and implemented ComponentResourceManager and ResourceManager to read resource image data.
In addition to the common attributes of the Bitmap and Image classes, the new attribute Image.PixbufData stores image data for use in GTKSystem.
ComponentResourceManager and ResourceManager mainly implement the GetObject method to read resource data.
5. How to use
Modify the windows application project properties of .net core and change the output type to “console application”, or uncheck the windows form configuration and change the configuration to false.
WinExe
false
1. Create a new System.Resources.ResourceManager class
Create a new System.Resources.ResourceManager class under the project and inherit GTKSystem.Resources.ResourceManager to overwrite the native System.Resources.ResourceManager class. .
GTKSystem.Resources.ResourceManager implements reading of project resource files and image files.
If the resource image file is not used in the project, there is no need to create this file.
2. Create a new System.ComponentModel.ComponentResourceManager class
Create a new System.ComponentModel.ComponentResourceManager class under the project and inherit GTKSystem.ComponentModel.ComponentResourceManager to overwrite the native System.ComponentModel.ComponentResourceManager class. .
GTKSystem.ComponentModel.ComponentResourceManager implements reading project resource files and image files (calling GTKSystem.Resources.ResourceManager).
If the resource image file is not used in the project, there is no need to create this file.
3. GTKWinFormsApp.csproj
Configure UseWindowsForms to false, or use a console application
false
4. Reference GTKSystem.Windows.Forms, System.Resources.Extensions
System.Resources.Extensions is an empty program dll. This dll is required for verification when VS loads the Form interface.
5. GTKWinFormsApp\obj\Debug\netcoreapp3.1\GTKWinFormsApp.designer.runtimeconfig.json
GTKWinFormsApp\obj\Release\netcoreapp3.1\GTKWinFormsApp.designer.runtimeconfig .json
Set the name to Microsoft.WindowsDesktop.App for VS to support visualizing the Form form on the form designer, reload the project or restart VS (When using this configuration to open the form on the VS form designer, the debugging switch will be slower because of the loading time. It is recommended to close the form designer when not developing the interface. form interface!)
“runtimeOptions”: {
“framework”: {
“name”: “Microsoft.WindowsDesktop.App”
},
6. Effect of use:
VS development interface:
Operating effect:
Running effect on Tongxin system:
Finally:
This program has been tested and run perfectly on the Tongxin system (Linux). It can be compiled once and run across platforms. The display interface style is basically the same as the display effect when running on Windows.
Currently, this component is not completely completed, but the main functions and technical difficulties have been solved, and it is now released for reference by developers in need.
gitee open source project address: https://gitee.com/easywebfactory/gtksystem-windows-forms
Update log: https://www.cnblogs.com/easywebfactory/p/17803567.html
Update and supplement:
After testing, this solution is also compatible with running on the mac platform, so it can support running on windows, linux, and mac platforms. Thanks to netizen @Sheepherder for testing.