OnPlatformExtension Class

Definition

Provides a XAML markup extension that returns different values depending on the platform the app is running on.

public ref class OnPlatformExtension : Microsoft::Maui::Controls::Xaml::IMarkupExtension
[Microsoft.Maui.Controls.ContentProperty("Default")]
[Microsoft.Maui.Controls.Xaml.RequireService(new System.Type[] { typeof(Microsoft.Maui.Controls.Xaml.IProvideValueTarget), typeof(Microsoft.Maui.Controls.Xaml.IValueConverterProvider), typeof(Microsoft.Maui.Controls.Xaml.IXmlLineInfoProvider), typeof(Microsoft.Maui.Controls.Xaml.IConverterOptions) })]
public class OnPlatformExtension : Microsoft.Maui.Controls.Xaml.IMarkupExtension
[Microsoft.Maui.Controls.ContentProperty("Default")]
public class OnPlatformExtension : Microsoft.Maui.Controls.Xaml.IMarkupExtension
[<Microsoft.Maui.Controls.ContentProperty("Default")>]
[<Microsoft.Maui.Controls.Xaml.RequireService(new System.Type[] { typeof(Microsoft.Maui.Controls.Xaml.IProvideValueTarget), typeof(Microsoft.Maui.Controls.Xaml.IValueConverterProvider), typeof(Microsoft.Maui.Controls.Xaml.IXmlLineInfoProvider), typeof(Microsoft.Maui.Controls.Xaml.IConverterOptions) })>]
type OnPlatformExtension = class
    interface IMarkupExtension
[<Microsoft.Maui.Controls.ContentProperty("Default")>]
type OnPlatformExtension = class
    interface IMarkupExtension
Public Class OnPlatformExtension
Implements IMarkupExtension
Inheritance
OnPlatformExtension
Attributes
Implements

Remarks

The value is resolved by matching the Platform identifier (the DevicePlatform struct's ToString() value) against the per-platform values, so a custom backend whose platform string matches one of the named properties below (for example GTK) resolves correctly at runtime. The match is case-sensitive (ordinal, per DevicePlatform equality), so the identifier must match the property name's casing exactly — a backend reporting DevicePlatform.Create("gtk") (lowercase) does not match the GTK property and falls through to Default. Note that the compile-time optimization (SimplifyOnPlatformVisitor) currently recognizes only the -android/-ios/-macos/-maccatalyst target frameworks; every other target framework (including -windows and -tizen) and any custom backend falls back to runtime resolution.

Because a XAML markup extension maps each named argument to a CLR property, the inline {OnPlatform iOS=…, Android=…} form can only express the platforms exposed as properties here. To target an arbitrary custom platform (e.g. a backend reporting DevicePlatform.Create("Web")), use the element form, which accepts any platform string:

<OnPlatform x:TypeArguments="x:String">
    <On Platform="Web" Value="…" />
    <On Platform="iOS, Android" Value="…" />
</OnPlatform>

Constructors

Name Description
OnPlatformExtension()

Properties

Name Description
Android

Gets or sets the value to use on Android.

Converter

Gets or sets a converter to apply to the platform-specific value.

ConverterParameter

Gets or sets a parameter to pass to the converter.

Default

Gets or sets the default value to use if no platform-specific value is set.

GTK

Gets or sets the value to use on GTK.

iOS

Gets or sets the value to use on iOS.

MacCatalyst

Gets or sets the value to use on Mac Catalyst.

macOS

Gets or sets the value to use on macOS.

Tizen

Gets or sets the value to use on Tizen.

UWP
Obsolete.

Gets or sets the value to use on UWP. Use WinUI instead.

WinUI

Gets or sets the value to use on Windows (WinUI).

WPF

Gets or sets the value to use on WPF.

Methods

Name Description
ProvideValue(IServiceProvider)

Applies to