Platform-Specific XAML
OnPlatform Markup Extension
Overview
The OnPlatform markup extension in Avalonia allows developers to specify different values for a property based on the operating system on which the application is running. This is particularly useful for creating cross-platform applications that need to adapt their UI or behavior according to the platform.
Basic usage in markup extension syntax
You can specify values for each platform and a default value that will be used if no specific platform match is found.
<TextBlock Text="{OnPlatform Default='Unknown', Windows='Im Windows', macOS='Im macOS', Linux='Im Linux'}"/>
Alternatively, you can use constructor syntax to define the default value directly, skipping Default
keyword. Platform-specific properties still needs to be defined.
<TextBlock Text="{OnPlatform 'Hello World', Android='Im Android'}"/>
You can use this markup extension with any other type, not only strings:
<Border Height="{OnPlatform 10, Windows=50.5}"/>