Net core system windows forms

System.Windows.Forms for .Net Core running on Windows and Linux

This is a early stage and experimental port of Mono’s System.Windows.Forms and a few additional libraries to .Net 6.0+. This allows Winforms code to run on both Windows and Linux (and possibly MacOS in the future).

Rather then porting Mono’s implementation of System.Drawing to .Net 6.0 I ported Mono’s System.Windows.Forms onto System.Drawing.Common. Note that on Linux you will need some native libraries.

Even though this is highly experimental and unsupported it does work (at least for me).

Builds are available via Nuget from the package Core.System.Windows.Forms or at https://www.nuget.org/packages/Core.System.Windows.Forms/.

As of Alpha 4 this targets .Net 6.0 instead of .Net Core 3.1.

A Demo on my Raspberry Pi 4, built and running on the Pi using .Net Core 3.1.

Platform Requirements

Windows — None known

Linux — Several native libraries, at least libx11 (libx11-dev on Ubuntu) and libgdiplus (libgdiplus on Ubuntu)

Mac — Not currently working. The Mono code that this is based on was 32 bit only. .Net Core requires 64 bit so until that is done this will not work.

Samples

There are three samples: SimpleTest (which uses my .Net Core Winforms), SimpleTest.NetCore (which uses the official .Net Core Winforms) and SimpleTest.NetFX which uses Net Framework. This also shows how to use the same source code with all three different platforms as the code is added as a link to the NetCore and NetFX projects. This way you can use the officially supported and nice looking Winforms on Windows.

To test this on Linux simply dotnet run in the SimpleTest folder.

More samples are coming soon TM. This will include more complex forms and more demonstrations of using the different libraries for different platforms.

I tested the SimpleTest example on Windows, Raspbian Buster and Ubuntu 20.04 using .Net Core 3.1 Release.

Future Work

Unknown right now. I will likely add more samples and fix issues as I go. If Mono adds support for Mac 64 bit I will port that over.

I will add better Linux documentation as I have time.

License

The Mono code is under the MIT license (https://github.com/mono/mono/blob/master/LICENSE). My contributions are under the MIT license as well.

  • README

  • Frameworks

  • Dependencies

  • Used By

  • Versions

  • Release Notes

A .Net Core implementation of the Mono System.Windows.Forms library. This is built on top of System.Drawing.Common.

Product

Compatible and additional computed target framework versions.

.NET

net6.0 is compatible. 

net6.0-android was computed. 

net6.0-ios was computed. 

net6.0-maccatalyst was computed. 

net6.0-macos was computed. 

net6.0-tvos was computed. 

net6.0-windows was computed. 

net7.0 was computed. 

net7.0-android was computed. 

net7.0-ios was computed. 

net7.0-maccatalyst was computed. 

net7.0-macos was computed. 

net7.0-tvos was computed. 

net7.0-windows was computed. 

net8.0 was computed. 

net8.0-android was computed. 

net8.0-browser was computed. 

net8.0-ios was computed. 

net8.0-maccatalyst was computed. 

net8.0-macos was computed. 

net8.0-tvos was computed. 

net8.0-windows was computed. 

net9.0 was computed. 

net9.0-android was computed. 

net9.0-browser was computed. 

net9.0-ios was computed. 

net9.0-maccatalyst was computed. 

net9.0-macos was computed. 

net9.0-tvos was computed. 

net9.0-windows was computed. 

  • net6.0

    • Accessibility
      (>= 4.6.0-preview3-27504-2)
    • Core.Mono.WebBrowser
      (>= 1.0.0-alpha6)
    • Core.System.Drawing.Design
      (>= 1.0.0-alpha6)
    • Microsoft.Win32.Registry
      (>= 4.7.0)
    • Mono.Posix.NETStandard
      (>= 1.0.0)
    • System.Configuration.ConfigurationManager
      (>= 6.0.0)
    • System.Drawing.Common
      (>= 6.0.0)
    • System.Resources.Extensions
      (>= 6.0.0)
    • System.Security.Permissions
      (>= 6.0.0)

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Core.System.Windows.Forms:

Package Downloads

Core.System.Windows.Forms.DataVisualization

A .Net Core implementation of the Mono System.Windows.Forms.DataVisualization library. This is built on top of System.Drawing.Common.

576

RoundedShape2

Package Description

294

GitHub repositories

This package is not used by any popular GitHub repositories.

Initial Release

July 18, 2023
1 min read
views 4672
Arthur C. Codex
chats: 2
Engineering
.Net

In this tutorial, we’ll go through the process of creating and deploying a Windows Forms Application using .NET Core.

How to Create and Deploy a Windows Forms Application with .NET Core image

Setting Up the Environment

First, you need to ensure that the .NET Core SDK is installed on your machine. You can download it from the official Microsoft .NET site.

Creating a New Project

Open a new command prompt and navigate to the directory where you’d like to create your new project. Once there, run the following command:


dotnet new winforms -o MyNewApp

This will create a new Windows Forms project in a directory named MyNewApp.

Writing the Application

Next, open Program.cs in your favorite code editor. You’ll see something similar to the code below:


using System;
using System.Windows.Forms;

namespace MyNewApp
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Let’s add a button to our form. Open Form1.cs and replace the existing code with the following:


using System;
using System.Windows.Forms;

namespace MyNewApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Button button = new Button();
            button.Text = "Click Me";
            button.Click += new EventHandler(Button_Click);
            Controls.Add(button);
        }

        private void Button_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hello, World!");
        }
    }
}

Deploying the Application

Back in the command prompt, navigate to the project directory and run the following command:


dotnet publish -c Release -r win10-x64 --self-contained

This will create a standalone application that can be run on any Windows 10 x64 machine.

If you need help with more complex applications, you might want to hire .NET remote developers.

Conclusion

And that’s it! You’ve just created and deployed a Windows Forms application using .NET Core. There’s a lot more you can do with .NET Core and Windows Forms, so don’t stop here!

Я добавил в проект form и теперь при старте пишет ошибку:

Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Reference assemblies cannot be loaded for execution. (0x80131058)"

Как это можно исправить?


  • Вопрос задан

  • 196 просмотров

Судя по версии вы грузите System.Windows.Forms из .net framework, у .Net Core (и просто .Net) свои библиотеки.
Например при создании Windows Forms в студии под .NET 8.0 вот так выглядит импорт.

669a247ed5f0e934513448.png

csproj при этом выглядит так

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWindowsForms>true</UseWindowsForms>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>
</Project>

Пригласить эксперта

Windows.Forms доступен только в .net framework. В .net core он не доступен

Войдите, чтобы написать ответ


  • Показать ещё
    Загружается…

Минуточку внимания

Понравилась статья? Поделить с друзьями:
0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии
  • Почему не работает запись экрана на windows 10
  • Фото обоев windows 10
  • Некоторые параметры скрыты или управляются вашей организацией windows 10 персонализация
  • Не работает блокнот в windows 11
  • Как отключить превью windows 10