如何序列化xaml“Brush"? [英] How can i serialize xaml "Brush"?

查看:26
本文介绍了如何序列化xaml“Brush"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何或什么是序列化System.Windows.Media.Brush的最佳方法?

How can or what is best method to serialize System.Windows.Media.Brush ?

推荐答案

这里有一个关于如何序列化 WPF 的很好的讨论:

Here is a good discussion of how to serialize WPF:

http://statestreetgang.net/post/2008/06/XAML-序列化-FTW.aspx

/// <summary>
/// Serializes the specified object
/// </summary>
/// <param name="toSerialize">Object to serialize.</param>
/// <returns>The object serialized to XAML</returns>
private string Serialize(object toSerialize)
{
    XmlWriterSettings settings = new XmlWriterSettings();
    // You might want to wrap these in #if DEBUG's 
    settings.Indent = true;
    settings.NewLineOnAttributes = true;
    // this gets rid of the XML version 
    settings.ConformanceLevel = ConformanceLevel.Fragment;
    // buffer to a stringbuilder
    StringBuilder sb = new StringBuilder();
    XmlWriter writer = XmlWriter.Create(sb, settings);
    // Need moar documentation on the manager, plox MSDN
    XamlDesignerSerializationManager manager = new XamlDesignerSerializationManager(writer);
    manager.XamlWriterMode = XamlWriterMode.Expression;
    // its extremely rare for this to throw an exception
    XamlWriter.Save(toSerialize, manager);

    return sb.ToString();
}

/// <summary>
/// Deserializes an object from xaml.
/// </summary>
/// <param name="xamlText">The xaml text.</param>
/// <returns>The deserialized object</returns>
/// <exception cref="XmlException">Thrown if the serialized text is not well formed XML</exception>
/// <exception cref="XamlParseException">Thrown if unable to deserialize from xaml</exception>
private object Deserialize(string xamlText)
{
    XmlDocument doc = new XmlDocument();
    // may throw XmlException
    doc.LoadXml(xamlText);
    // may throw XamlParseException
    return XamlReader.Load(new XmlNodeReader(doc));
}

这篇关于如何序列化xaml“Brush"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆