Pregunta

I want to change skin color, so did this:

<Application.Resources>
    <Color x:Key="AppColor">#FFFFFF</Color>
</Application.Resources>

and then used it across the app for example:

<...... BackgroundColor="{StaticResource AppColor}" ... />

it works fine. but when I try to change it I face some problems.

Application.Current.Resources["AppColor"] = Color.FromArgb(255, 0, 255, 255);

I get a not implemented error. when I try remove it and add a new one:

Application.Current.Resources.Remove("AppColor");
var color = Color.FromArgb(255, 0, 255, 255);
Application.Current.Resources.Add("AppColor", color);

I get a xaml error, just whereever I've used that static value.

How can I use a color and change it?

¿Fue útil?

Solución

One possible solution is to use SolidColorBrush in resources

Example define SolidColorBrush, with x:Name="AppBrush" in App.xaml resources

<SolidColorBrush x:Key="AppBrush" Color="#FF7DC959"/>

If your binding Target is Color then it will be like this

<....BackgroundColor="{Binding Color,Source={StaticResource AppBrush}}">

And if your binding target is SolidColorBrush it will be like this

<....BackgroundColor="{StaticResource AppBrush}">

Then you can change Color property in code

SolidColorBrush brush = (SolidColorBrush)App.Current.Resources["AppBrush"];
brush.Color = Colors.DarkGray;

Otros consejos

Using a DynamicResource instead of a StaticResource should fix this:

<...... BackgroundColor="{DynamicResource AppColor}" />
Application.Current.Resources["AppColor"] = Color.FromArgb(255, 0, 255, 255);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top