序列化GradientBrush

时间:2013-11-25 20:53:09

标签: c# wpf serialization brush

我尝试使用序列化保存Brush对象,但我收到以下错误:

  

键入' System.Windows.Media.LinearGradientBrush'在Assembly&PresentationCore中,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35'未标记为可序列化

如何将Brush对象保存到文件中?

1 个答案:

答案 0 :(得分:4)

试试这个......

var brush = new LinearGradientBrush(new GradientStopCollection(
    new GradientStop[] { new GradientStop(Colors.Blue, 2.0), new GradientStop(Colors.Red, 3.0) }));

using (var outfile = File.CreateText("Brush.xaml"))
{
    XamlWriter.Save(brush, outfile);
}

产生以下内容:

<LinearGradientBrush xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <LinearGradientBrush.GradientStops>
        <GradientStop Color="#FF0000FF" Offset="2" />
        <GradientStop Color="#FFFF0000" Offset="3" />
    </LinearGradientBrush.GradientStops>
</LinearGradientBrush>