Serializing GradientBrush

你说的曾经没有我的故事 提交于 2019-12-24 12:35:20

问题


I'm trying to save a Brush object using serializing but I get error below:

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

How can I save a Brush object into a file?


回答1:


Try this...

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

Which produces the following:

<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>


来源:https://stackoverflow.com/questions/20203077/serializing-gradientbrush

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!