Code

Cross-platform GUI application in C#

Cross-platform GUI application in C#

Free Course: "Quick Start with Python"

Learn More

Microsoft has released the third version of the open-source, cross-platform .NET Core, but there is still no full-fledged tool for developing graphical interfaces. This limitation creates certain difficulties for developers seeking to create visually appealing applications on this platform. Developing graphical interfaces remains an important need, and users expect that in the future, Microsoft will provide the necessary tools for effectively creating UIs in .NET Core.

Xamarin currently supports developing applications for Windows 10 and mobile devices only. However, there is a need for developers seeking to create graphical interfaces for Linux and Mac OS operating systems. It is important to consider alternative tools and frameworks that can help in creating cross-platform applications. There are various solutions, such as Avalonia and Electron, that provide the ability to develop interfaces compatible with multiple operating systems. These tools allow developers to expand their capabilities and create applications accessible to a wider audience of users across different platforms.

Third-party frameworks can significantly simplify the development process. They offer ready-made solutions and tools that allow you to create web applications faster and more efficiently. Using such frameworks helps developers focus on business logic rather than routine tasks. Thanks to extensive libraries and communities supporting these tools, developers can easily find solutions to emerging questions and problems. Choosing the right framework can be a key factor in the success of a project.

Which framework should I choose?

I found two well-known frameworks, both of which use the Skia graphics library.

I tested both options, and the second one seemed more convenient to me. It includes a markup language, supports the MVVM architecture, provides quick setup, and an easy transition from WPF. That's why I settled on this solution.

How to get started with AvaloniaUI

I plan to develop the application on Linux Ubuntu, but this instruction will be useful for all .NET Core users. While creating applications is possible in Visual Studio with the extension installed, this is not available on Linux. Therefore, I will be using the terminal for development.

To get started, clone this repository to your computer.

This archive contains templates for developing applications using AvaloniaUI. If you're not familiar with Git, you can simply download the archive contents and unzip it to any convenient location on your computer. After that, open the console and run the following command:

This will set up templates for developing the application. To verify that the templates were successfully added, run the following command:

You will receive a full list of all installed templates. This list should include templates such as Avalonia Window, Avalonia .NET Core MVVM App, Avalonia UserControl, and Avalonia .NET Core App. If all the necessary templates are present, you can continue working.

Open the console and navigate to the folder where you plan to create the new project. Enter the following command:

We plan to develop the application using the MVVM pattern. Most of the AvaloniaUI documentation is focused on this approach, which simplifies the development process and increases work efficiency. Using MVVM will ensure clear organization of the code and improve the testability of the application.

Now you can start developing the application.

Creating a calculator on AvaloniaUI

As a result of the process, the following folders will be created.

  • Assets - here you can upload various program components such as icons, images, sounds, etc.
  • Models - this folder is intended for classes that will act as a model.
  • ViewModels - here are the classes acting as intermediaries between the view and the model.
  • Views - all windows will be located here.

First of all, let's open the Program.cs file, which is located in the root directory of the project. This file contains the main entry point to the application and plays a key role in its launch. It defines the settings and configurations necessary for the correct operation of the application. Pay attention to the code structure and dependencies used, as they can significantly impact the functionality of the entire project.

The AppMain() method is responsible for creating the application's main window (MainWindow) with a DataContext, which is used for data binding. After setting the DataContext, the window is launched, providing the interface and user interaction. This process is essential for the proper operation of the application, as it allows for efficient data management and its display in the user interface.

In this method, you can define your own application initialization logic. For example, you can create a model instance and pass it to the MainWindowViewModel class constructor. However, before doing this, you must define a constructor that will accept the appropriate argument.

Our application has a simple structure, which allows us to place all the necessary logic in the MainWindowViewModel.cs file. This file will contain the key properties and methods that provide the application's functionality. This approach simplifies code maintenance and development, improving its readability and usability.

First, you need to include the ReactiveUI namespace, which is used in AvaloniaUI to implement the MVVM pattern. This will allow you to effectively manage data and interactions between the view and model. Including ReactiveUI will provide access to powerful tools for creating responsive user interfaces and will also simplify the data binding process. Use ReactiveUI in your AvaloniaUI projects to improve productivity and code quality.

To create a window interface, you must use XAML markup in the MainWindow.xaml file. In this file, you can define the structure and interface elements, such as buttons, text fields, labels, and other controls. For example, the window structure might include a main container that will house all controls.

To begin, you can add a root element, such as a Grid or StackPanel, which will serve as a container for other elements. Then, you should define the sizes and positions of the elements to ensure the interface is easy to use. Don't forget to specify properties such as name, text, and event handlers for interactive elements.

It's also important to consider the interface's responsiveness for different screen resolutions so that users can comfortably interact with the application on any device. With XAML, you can easily implement style and visual effects that will enhance the overall user experience.

As a result, a well-formatted MainWindow.xaml file will become the foundation of your application, providing an intuitive and functional interface.

You may notice that this XAML is almost identical to that used in WPF. It contains similar components and properties, and also utilizes elements such as Grid and data binding mechanisms. However, in this case, you can specify regular methods as commands, which is not possible in WPF. This expands development options and simplifies the implementation of functionality in applications.

To compile and run the application, enter the following command in the console:

Now the application can be easily compiled for various operating systems without the need for additional actions to port to a new platform. This simplifies the development process and ensures faster adaptation of the application to various environments.

Conclusion

Ten to fifteen years ago, using a programming language originally created for developing Windows applications to write programs for Linux might have seemed unusual. However, today it has become quite common practice. Modern tools and compilers make it possible to effectively adapt these languages ​​to work in the Linux environment, which significantly expands the capabilities of developers.

To improve the SEO optimization of the text, it is important to use keywords and phrases that are relevant to the topic. You should also make the text more readable and structured.

Reading is an important aspect of personal growth and development. It helps to expand horizons, improve critical thinking skills and increase the overall level of knowledge. Regularly reading books, articles and other materials enriches our inner world and helps to better understand the reality around us.

If you're looking to improve your reading skills, consider exploring different genres and authors. This will help you discover interesting topics and develop your passions. Reading not only entertains but also inspires new ideas and achievements.

Don't forget that you can make reading more engaging by sharing your impressions with friends or joining book clubs. This will create additional motivation and make the process more interactive.

Follow new publications and book recommendations to stay up-to-date on current trends and interesting new releases. Reading is not only useful but also an enjoyable activity that will enrich your life with new knowledge and emotions.

  • How .NET works and why you need it
  • Podcasts, frontend, networking: how to join the IT community and learn for free
  • Writing a messenger in C#. Part 1. Layout