0

我在 VS2012 上测试 WPF/Silverlight。我在 MSDN 上发现以下代码应该使用 RadialGradient 方法填充一个矩形,但得到一个错误“'System.Windows.Media.GradientStop' 不包含采用 2 个参数的构造函数”。没有重载,只有一种不带参数的可用方法,但如果是这样,那么我将如何加载这些值?在我研究的任何地方,它们都与 2 个参数一起使用。

矩形填充在 XAML 中时工作正常......

(部分 XAML 代码)...

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

但在 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);
         }
    }
}

我从这里得到了代码...... http://msdn.microsoft.com/en-us/library/system.windows.media.gradientstop.color.aspx

谢谢...

4

1 回答 1

2

Silverlight 不支持 2 参数构造函数 ( GradientStop Constructor )。采用

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

反而。

于 2012-10-06T07:15:15.213 回答