Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
889 views
in Technique[技术] by (71.8m points)

wpf - How can i serialize xaml "Brush"?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is a good discussion of how to serialize WPF:

http://statestreetgang.net/post/2008/06/XAML-Serialization-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));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...