How to load ThemeResourceKind.ControlForegroundBrushOutlineAccent.ToResourceKey()

Themes, Shared, and Core Libraries for Avalonia Forum

Posted 6 months ago by Michael Janulaitis - Corner Bowl Software
Avatar

I am trying to create a SolidColorBrush for a custom Control that uses the ThemeResourceKind.ControlForegroundBrushOutlineAccent.ToResourceKey() C# resource key, however I am unable to load the resource with the following code:

var resource = Application.Current?.FindResource(ThemeResourceKind.ControlBackgroundBrushSolidAccent.ToResourceKey());

Does anyone know the syntax to load these resources?

Comments (2)

Answer - Posted 6 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

These resources can vary based on the current theme (light or dark), so you'll want to be sure you're passing the appropriate theme when resolving the resource.  In our Sample Browser, we load those resources like this:

var keyName = ThemeResourceKind.ControlBackgroundBrushSolidAccent.ToResourceKey();
if (Application.Current?.TryGetResource(keyName, ThemeVariant.Default, out var resource) == true) {
  // ...
}

Depending on where you call that, you'd want to use the appropriate control's actual theme variant or perhaps fallback to "Application.Current?.ActualThemeVariant".


Actipro Software Support

Posted 6 months ago by Michael Janulaitis - Corner Bowl Software
Avatar

OK that's working now.  Thanks.

[Modified 6 months ago]

Add Comment

Please log in to a validated account to post comments.