if I do a converter like this
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is null)
return "Pink";
else
return "Red";
}
It works properly, but if i want to return a "{StaticResource Primary}" or "StaticResource Primary" instead of Pink ou red it doesn't work
My xaml is like this
<Image Source="{FontImageSource FontFamily=MaterialRegular, Glyph={x:Static m:MaterialRegular.Folder}, Color={Binding Action, Converter={StaticResource ConvertColorFolder}}}" />`
Do you have any idea to do that ?
In MAUI some things have changed. Just know you can't directly access Application level Resources it throws exceptions and this is what I use to avoid that:
public static class Extensions
{
public static T GetResource<T>(this ResourceDictionary dictionary, string key)
{
if (dictionary.TryGetValue(key, out var value) && value is T resource)
return resource;
else
return default;
}
}
Now you need to get the correct Color in your Converter using this extension method (don't forget to add the correct namespace):
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
var primaryColor = Application.Current.Resources.GetResource<Color>("PrimaryColor");
if (value is null)
return primaryColor;
else
return Colors.Red;
}
Note that this is just an example, and will need changes based on your needs.