Avalonia learning practice (2)--cross-platform support and release

Avalonia focuses on cross-platform, and claims that a set of code supports Windows, macOS, Linux, iOS, Android operating systems. Its foundation is a series of libraries based on .NET Standard 2.0, that is, as long as the platform can support .NET Standard 2.0, Avalonia can run developed applications. However, the ideal is full and the reality is very skinny. It does not mean that cross-platform Avalonia has not achieved it, but that there will inevitably be some minor problems in the process.


1. Supported platforms

Operating platform Version
Windows Windows8 and above (Window7 can also be used, but no problem is not guaranteed)
MacOS MacOS High Serra 10.13 and above (I have never used Apple and have no development needs, so I will not study in detail)
Linux Debian 9, Ubuntu 16.5, Fedora 30 and above (mainly various distributions)

The cross-platform mobile terminal, that is, the support of iOS and Android will be discussed later.
By the way, let's learn about the situation of using the Linux kernel domestic operating system:

operating system R&D unit Remark
galaxy unicorn Tianjin Kirin Information Technology Co., Ltd.
Winning Kirin Zhongbiao Software Technology Co., Ltd. Puhua
Unicom UOS Tongxin Software Technology Co., Ltd.
Zhongke Fangde Zhongke Fangde Software Co., Ltd.
Ubuntu Kylin China CCN Joint Laboratory Based on Ubuntu distribution

At present, there are a lot of contact with these models. Other domestic operating systems include StartOS (formerly ylmf OS), SPG Sipu, Gongchuang Linux, Deepin Linux (Deepin), Red Flag Linux, ZTE NewStart (NewStart), Huawei Hongmeng (HarmonyOS), etc. Among them, the party, government, and military departments of the Kirin series and the Chinese prefix are often used, and there will be a dedicated version. The operating system version is generally good, and the desktop version is not as good. However, it can support .NET Core cross-platform, and it should be possible to run Avalonia applications; The desktop version of Xinxin UOS is relatively comfortable to use in China, especially when you are used to the smooth switching of Windows (Tongxin UOS V20 Home Edition is used in the test environment); Huawei Hongmeng is coming in very quickly, and its positioning is not as good as the previous ones. The same, focusing on mobile and IoT; others have not been used, so no evaluation.

2. Cross-platform realization

The logic of Avalonia's cross-platform implementation can be understood through the code of the application entry Program.cs.

public static AppBuilder BuildAvaloniaApp()
    => AppBuilder.Configure<App>()
        .UsePlatformDetect()
        .LogToTrace()
        .UseReactiveUI();

This is the code to build the application in the MVVM mode. In the normal mode, there is no line of UseReactiveUI().
The focus is on the method UsePlactformDetect(), which is to run platform detection. The method is implemented as follows:

public static TAppBuilder UsePlatformDetect<TAppBuilder>(this TAppBuilder builder)
    where TAppBuilder : AppBuilderBase<TAppBuilder>, new()
{
    
    
    var os = builder.RuntimePlatform.GetRuntimeInfo().OperatingSystem;

    // We don't have the ability to load every assembly right now, so we are
    // stuck with manual configuration  here
    // Helpers are extracted to separate methods to take the advantage of the fact
    // that CLR doesn't try to load dependencies before referencing method is jitted
    // Additionally, by having a hard reference to each assembly,
    // we verify that the assemblies are in the final .deps.json file
    //  so .NET Core knows where to load the assemblies from,.
    if (os == OperatingSystemType.WinNT)
    {
    
    
        LoadWin32(builder);
        LoadSkia(builder);
    }
    else if(os==OperatingSystemType.OSX)
    {
    
    
        LoadAvaloniaNative(builder);
        LoadSkia(builder);
    }
    else
    {
    
    
        LoadX11(builder);
        LoadSkia(builder);
    }
    return builder;
}

static void LoadAvaloniaNative<TAppBuilder>(TAppBuilder builder)
    where TAppBuilder : AppBuilderBase<TAppBuilder>, new()
     => builder.UseAvaloniaNative();
static void LoadWin32<TAppBuilder>(TAppBuilder builder)
    where TAppBuilder : AppBuilderBase<TAppBuilder>, new()
     => builder.UseWin32();

static void LoadX11<TAppBuilder>(TAppBuilder builder)
    where TAppBuilder : AppBuilderBase<TAppBuilder>, new()
     => builder.UseX11();

static void LoadSkia<TAppBuilder>(TAppBuilder builder)
    where TAppBuilder : AppBuilderBase<TAppBuilder>, new()
     => builder.UseSkia();

The logic is also easy to understand, that is, when the application starts, the current operating system type is obtained through the operating environment information, and different UI rendering libraries are loaded according to the operating system type. When the operating system type is WinNT (Windows), load the libraries of Win32 and Skia; when the operating system type is OSX (MacOS), load the libraries of AvaloniaNative and Skia; for other types, that is, Linux, load the libraries of X11 and Skia.

3. Publish to different platforms

Commonly used IDEs for developing Avalonia applications include VisualStudio and JetBrains Rider. Taking VisualStudio, the "No. 1 IDE in the universe" as an example, the key configurations for publishing to different platforms are as follows:
insert image description here

  • Configuration: Mainly distinguish between Debug and Release
  • Target Framework: Any that supports .NET Standard 2.0 is fine.
  • Deployment mode: Select "Standalone" to run directly without additional installation of the runtime environment; select "Dependent" and also need to install the runtime environment in advance
  • Target runtime: need to be selected according to the type of operating system and hardware architecture (mainly CPU architecture)
  • Generate a single file: Check "Generate a single file", all DLL class libraries will be packaged into an executable file when publishing

Publish to the test environment (unixin UOS, AMD processor, so choose linux-x64) as follows:
insert image description here
the running effect is as follows:

insert image description here

Attachment. Domestic CPU instruction set route

Domestic CPU Instruction Set
Godson MIPS
sea ​​light x86
Zhaoxin x86
Soar arm
Kunpeng arm
Shenwei Alpha

Among them, Godson is a completely independent instruction set, and it has just updated its support for .Net Core some time ago; x86 is mainly ecologically good. Traditional desktop processors Intel and AMD are both x86 architectures, and it is more convenient to do compatibility and adaptation; arm used to There are more mobile terminals, and now the desktop terminal is gradually catching up.


In the previous article, the operating environment was still .NET 5.0. As a result, I saw that the official document had stopped supporting it a few days ago. Then I uninstalled VS2019 and replaced it with VS2022. I also planned to see how the MAUI released in .NET 7.0 is. Recently, the .NET version has been updated really fast, and I feel that I can't keep up with it, so I have to keep working hard.
insert image description here

Guess you like

Origin blog.csdn.net/lordwish/article/details/124767653