我正在VS2012上测试WPF/Silverlight。我在MSDN上发现了下面的代码,这些代码应该用RadialGradient方法填充一个矩形,但得到了一个错误"'System.Windows.Media.GradientStop‘不包含接受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
谢谢..。
发布于 2012-10-06 15:15:15
Silverlight不支持双参数构造函数(GradientStop Constructor)。使用
GradientStop stop = new GradientStop();
stop.Color = Colors.Yellow;
stop.Offset = 0.0;
myBrush.GradientStops.Add(stop);而不是。
https://stackoverflow.com/questions/12756602
复制相似问题