Start A New Gtk# Project in .Net Core

Matt Perley
3 min readSep 24, 2020

Are you starting a new application for desktop computers? Would you like it to run cross-platform? Want to try something a bit different from Winforms or WPF? If you answered yes to any of those questions you should try out Gtk# with .Net Core. Microsofts .Net Core allows you to develop applications and web apps on several OS types, from linux to mac, you are no longer restricted to the MS platform!

Lets get started!

First open a folder and launch a terminal, once done enter:

dotnet new gtkapp youappname, or just, dotnet new gtkapp (will take name of directory)

If you open the project now in VS Code you should see this in the filetree:

Now that we have our project we can take a look at whats happening. Open program.cs and it should look like this:

As you can see it creates a new instance of Application and then registers with Glib. Once registered it creates an instance of MainWindow and runs the application. Note: win.Show() tells Gtk to show changes to the UI.

Now open your MainWindow.cs/MainWindow.glade and you should see this:

The Label and Button are declared here and bound to the code behind.
Code behind for your glade file.

The [UI] decorator indicates a binding with the glade markup file. Builder(“MainWindow.glade”) will get an instance of your markup. Like many frameworks in .Net you can set clicked or activated methods with += from the constructor or method. E.G. button.clicked += somefunction || button.clicked += (sender, args) => {…};. To exit the application programatically all you do is call Application.Quit(); and its over.

Gtk is a solid framework that has all of the functionality that you might need. I would note though that there does not seem to be a C# compatible webbrowser class, if there is I have not found it.

In order to build the project you created all you do is open a terminal and enter: dotnet run. Build: dotnet build, or if you want to add a new widget to the application, create a new folder and using that folder path, open a terminal and enter: dotnet new gtkwidget widgetname, or simple dotnet new gtkwidget.

In a future post I will talk about adding new UI to your app. We will build off of this project going forward.

Until next time, take care!

--

--