.NET GUI on Linux: First Steps

.NET GUI on Linux: First Steps

Configure your environment to create GTK Linux applications with .NET

Nov 13, 2023ยท

2 min read

Many .NET developers use Linux as a daily driver - me included. We use .NET because we love both C# and the .NET ecosystem. But we use Linux because of its simplicity, performance and, on some occasions, price.

We know that .NET is now a multiplatform and open source development environment. So we should theoretically be able to create applications for most operating systems... right?

Well, yes. But with some limitations. We can create console applications and web services that will run in Windows, Mac OS and Linux, but when it comes to desktop applications, there's only Xamarin and MAUI which both leave Linux out of the scope.

GTK

On the other hand, most Linux native application development is done with two main libraries:

  • GTK: The one used by Gnome and many others like Cinnamon, XFCE and more.

  • QT: The one used by KDE.

GTK is the most popular one and provides bindings for some programming languages like C, Python, Rust and others. However, there's no binding for .NET.

There's an open source project called GTK# which is a .NET wrapper on the GTK library. It allows coding GTK applications using .NET.

In this post, I'll show you how to create your first app using it.

Tools

SDK

Make sure you have the .NET SDK installed. For instructions on how to install it, refer to this site.

Editor

If you're on Linux, you won't be able to use Visual Studio. I recommend you download VS Code and install the C# extension. You can follow this guide.

GTK

For this step, we'll refer to GTK#'s GitHub repo, which says that we need to go to a terminal and execute the following command:

dotnet new --install GtkSharp.Template.CSharp

With this, we can already start coding cool GTK apps, that can potentially run on different operating systems. However, I recommend installing one more tool to make your life easier.

Glade

Glade is a UI editor that will let you create your GTK interfaces graphically instead of making you type all the code for it. We can install it just to draw our app, generate the XML, and then use it in our application by binding all the controls with the code-behind.

To install it, run the following:

sudo apt install glade

Closing notes

At this point, we have our environment ready to start creating desktop apps with .NET and GTK. In the next post we will be creating our first one.

References

https://github.com/GtkSharp/GtkSharp

https://matt-perley-21.medium.com/start-a-new-gtk-project-in-net-core-b89634d395f9

ย