'Using C#, .NET Core 3 and GTK# for cross-platform Programming (and alternatives)
I am about to start development of a software project that should run on Linux and Windows if possible. As I already have some experience with C# I am eager to use it for this project. I assumed that with .NET Core 3 and GTK# 3.22 this shouldn't be a problem since .NET Core App should be cross-platform out of the box. GTK# - from my understanding - should work everywhere GTK+ in the same version is also available.
Why c#? Well I just like the language and there is an ECS Framework for c# I'd like to use.
So far I have setup a test Console App project in Visual Studio targeting .NET Core 3 and added an GTK# specific NuGet package.
I wrote a simple Hello World program for testing of the environment.
using System;
using Gtk;
namespace GTKTest
{
class Program
{
static void Main(string[] args)
{
Application.Init();
Window win = new Window("Hello GTK");
Label lbl = new Label("This is a test GTK App written with C# for GNU/Linux and Windows");
win.DeleteEvent += Win_DeleteEvent;
win.Add(lbl);
win.ShowAll();
Application.Run();
win.Dispose();
Console.Write("Press any key...");
Console.ReadKey();
}
private static void Win_DeleteEvent(object o, DeleteEventArgs args)
{
Application.Quit();
args.RetVal = true;
}
}
}
When I run this code from Visual Studio 2019 I get
System.TypeInitializationException
HResult=0x80131534
Message=The type initializer for 'Gtk.Application' threw an exception.
Source=GtkSharp
StackTrace:
at Gtk.Application.Init()
at GTKTest.Program.Main(String[] args) in D:\Workspace\VSRepos\C#\GTKTest\Program.cs:line 10
Inner Exception 1:
DllNotFoundException: Gtk
While searching for a solution I installed mono and GTK# for Windows from this page. The mono part shouldn't be necessary if I stick to .NET Core I think.
What am I missing? What am I doing wrong? Is what I'm trying to achieve even possible like I am imaging it? I'm also interested in some alternatives how to program cross-platform GUI-Software with C#. I stumbled upon electron.js but I heard it has some big Memory overhead and I'm not really into javascript. AvaloniaUI sounded interesting but I thought that the above approach would be better.
Edit: After adding msys path like suggested here in step 3 I get following error preceding the exception from above. The error states that the procedure entry point couldn't be found in the dll.
Solution 1:[1]
I know this is an old question, yet Google likes it. I have had the similar problem. There are lots of guides that are easy to google but hard to make them work. Here is the guide that helped me as of April 2022:
Step 1: Install MSYS2 Download the MSYS2 installer and follow the installation instructions.
Step 2: Install GTK+3 Open a MSYS2 shell, and run: pacman -S mingw-w64-x86_64-gtk3
Step 3: Modify PATH variable so that the library can be found
Open up Control Panel
Go to System and Security > System
Click on the Advanced system settings link
Click on Environment Variables... button
Under System variables find the Path variable and select it
Click the Edit button
Add either ;C:\msys64\mingw64\bin or ;C:\msys32\mingw32\bin to the end of the variable, depending on your system architecture
Click OK, and you are done
Restart your system for the changes to apply
To run GtkSharp Samples project you would also need:
pacman -S mingw-w64-x86_64-gtksourceview4
Otherwise it throws ...System.DllNotFoundException: GtkSource: libgtksourceview-4-0.dll...
Then you may either install GtkSharp from here or from nuget.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |