0

I'm testing WPF/Silverlight on VS2012. I've found on MSDN the following code that should fill a rectangle with the RadialGradient method, but get an error "'System.Windows.Media.GradientStop' does not contain a constructor that takes 2 arguments". There are no overloads and only one available method that takes no parameters, but if so, then how would I load the values? Everywhere I research they're used with 2 parameters.

The rectangle fill works fine when it's in XAML...

(partial XAML code)...

                <Rectangle.Fill>
                    <RadialGradientBrush>
                        <GradientStop Color="Black" Offset="0.063"/>
                        <GradientStop Color="White" Offset="1"/>
                    </RadialGradientBrush>
                </Rectangle.Fill>

but gives an error in C#...

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using Microsoft.Expression.Interactivity;

namespace Test
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
        // Required to initialize variables
        InitializeComponent();
        }

        public void drawBars(int numBars)
        {
            Rectangle rectangle;
            double offsetX=0;
            double width=0;
            for (int i = 0; i < numBars; i++)
            {
                RadialGradientBrush myBrush = new RadialGradientBrush();
                myBrush.GradientOrigin = new Point(0.75, 0.25);
                myBrush.GradientStops.Add(new GradientStop(Colors.Yellow, 0.0)); //error
                myBrush.GradientStops.Add(new GradientStop(Colors.Orange, 0.5)); //error
                myBrush.GradientStops.Add(new GradientStop(Colors.Red, 1.0)); //error
                rectangle.Fill = myBrush;
            }
         }

         private void Button_Click_2(object sender, RoutedEventArgs e)
         {
                drawBars(10);
         }
    }
}

I got the code from here... http://msdn.microsoft.com/en-us/library/system.windows.media.gradientstop.color.aspx

Thanks...

hagensoft
  • 1,497
  • 13
  • 13
  • Please be aware of the fact that Silverlight is different from WPF. It is not even a simple subset of WPF. See [here](http://stackoverflow.com/q/629927/1136211) for some details. – Clemens Oct 06 '12 at 07:25

1 Answers1

2

Silverlight doesn't support the 2 arguments constructor (GradientStop Constructor). Use

GradientStop stop = new GradientStop();
stop.Color = Colors.Yellow;
stop.Offset = 0.0;
myBrush.GradientStops.Add(stop);

instead.

LPL
  • 16,827
  • 6
  • 51
  • 95
  • 2
    Or shorter `myBrush.GradientStops.Add(new GradientStop { Color = Colors.Yellow, Offset = 0.0 });` – Clemens Oct 06 '12 at 07:18
  • If you say so. :) I wasn't sure if this is valid for Silverlight too. Thanks! – LPL Oct 06 '12 at 07:18