c# - 序列化 GradientBrush

标签 c# wpf serialization brush

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

Type 'System.Windows.Media.LinearGradientBrush' in Assembly 'PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable

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

最佳答案

试试这个...

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>

关于c# - 序列化 GradientBrush,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20203077/

相关文章:

java - 在android studio上反序列化xml文件

c# - Process.Start() 无法打开 exe

c# - WPF 将文本框绑定(bind)到 ViewModel

wpf - 什么是WPF浏览器应用类型项目?

wpf - 使用负边距时在控件内显示内容。

c# - Xml序列化错误: Unable to cast object of type

node.js - 在 nodejs 中使用 Apache Thrift 进行序列化-反序列化

c# - 如何知道分数中的循环小数?

c# - 将文本格式化为日期/时间格式 c#

c# - C# 中的嵌套泛型问题