Saif Al Falah
Saif Al Falah

Reputation: 358

StaticResource PhoneAccentBrush as GradientStop

I am developing an application wherein I have to set one of the GradientStops as PhoneAccentColor StaticResource and I have to do this in the code (i.e. C#).

Here's what I have already tried:

GradientStop accentGS = new GradientStop();
accentGS.Color = (System.Windows.Media.Color)App.Current.Resources["PhoneAccentBrush"];
accentGS.Offset = .5;
lgbBlack.GradientStops.Add(accentGS);

Here, lgbBlack is the LinearGradientBrush to which I am adding this GradientStop.

This doesn't throw any error but when I run the application a 'System.InvalidCastException' is thrown and the application crashes.

What am I doing wrong here?

Upvotes: 0

Views: 524

Answers (1)

Olivier Payen
Olivier Payen

Reputation: 15268

The problem is that the PhoneAccentBrush resource is a SolidColorBrush, not a Color.
That code should work:

GradientStop accentGS = new GradientStop();
SolidColorBrush c = (SolidColorBrush)App.Current.Resources["PhoneAccentBrush"];
accentGS.Color = c.Color;
accentGS.Offset = .5;
lgbBlack.GradientStops.Add(accentGS);

Upvotes: 1

Related Questions