【问题标题】:Serializing GradientBrush序列化渐变画笔
【发布时间】:2013-12-10 18:28:21
【问题描述】:

我正在尝试使用序列化保存 Brush 对象,但出现以下错误:

在程序集“PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”中的类型“System.Windows.Media.LinearGradientBrush”未标记为可序列化

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

【问题讨论】:

  • “将画笔对象保存到对象”是什么意思?请说明您要完成的工作,而不是您要完成的工作。
  • 与其将画笔对象保存到文件中,不如直接保存其值?
  • 我想为我的应用保存皮肤设置。
  • 我无法保存值,因为它们有很多。

标签: c# wpf serialization brush


【解决方案1】:

试试这个...

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>

【讨论】: