Platform Settings
The PlatformSettings service provides access to platform-specific settings and information. Some of these settings can change at runtime if a user modifies their OS preferences, so your application can respond dynamically.
You access PlatformSettings through the GetPlatformSettings extension method on any Visual. For more details on accessing platform services, visit the TopLevel page.
var platformSettings = myControl.GetPlatformSettings();
Methods
GetTapSize(PointerType type)
Returns the size of the rectangle around a pointer-down location within which a pointer-up must occur to register a tap gesture. The value is in device-independent pixels.
Size GetTapSize(PointerType type);
GetDoubleTapSize(PointerType type)
Returns the size of the rectangle around a pointer-down location within which a pointer-up must occur to register a double-tap gesture. The value is in device-independent pixels.
Size GetDoubleTapSize(PointerType type);
GetDoubleTapTime(PointerType type)
Returns the maximum time allowed between the first and second tap of a double-tap gesture.
TimeSpan GetDoubleTapTime(PointerType type);
GetColorValues()
Returns the current system color values, including whether dark mode is active and what accent color the user has chosen.
PlatformColorValues GetColorValues();
While the built-in FluentTheme supports automatic switching between accent colors, you can use this method to apply custom logic based on OS color settings.
Properties
HoldWaitDuration
Gets the duration between a pointer press and when the Holding event fires.
TimeSpan HoldWaitDuration { get; }
HotkeyConfiguration
Gets the platform-specific hotkey configuration for your Avalonia application. This property returns a PlatformHotkeyConfiguration object that contains the key gestures for common operations such as copy, paste, cut, select all, and undo.
PlatformHotkeyConfiguration HotkeyConfiguration { get; }
HotkeyConfiguration is especially useful when your application needs to handle well-known gestures like Copy, Paste, or Cut in a platform-aware way.
The following example shows how you can check whether a key event matches the platform's Copy gesture:
protected override void OnKeyDown(KeyEventArgs e)
{
var hotkeys = this.GetPlatformSettings()?.HotkeyConfiguration;
if (hotkeys is not null && hotkeys.Copy.Any(g => g.Matches(e)))
{
// Handle Copy hotkey.
}
}
Events
ColorValuesChanged
Raised when the system color values change. This includes changes to dark mode and accent colors. You can subscribe to this event to update your application's appearance in real time.
event EventHandler<PlatformColorValues>? ColorValuesChanged;
The following example subscribes to color changes and logs the new theme variant:
var platformSettings = myControl.GetPlatformSettings();
if (platformSettings is not null)
{
platformSettings.ColorValuesChanged += (sender, values) =>
{
Debug.WriteLine($"Theme variant: {values.ThemeVariant}");
};
}
See also
- TopLevel: Accessing platform services from controls.
- Pointer events: Working with pointer input and gestures.
- Gestures: Handling tap and other gesture events.