How can i serialize xaml “Brush”?

前端 未结 1 821
借酒劲吻你
借酒劲吻你 2020-12-10 18:23

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

1条回答
  •  长情又很酷
    2020-12-10 18:45

    Here is a good discussion of how to serialize WPF:

    http://statestreetgang.net/post/2008/06/XAML-Serialization-FTW.aspx


    /// 
    /// Serializes the specified object
    /// 
    /// Object to serialize.
    /// The object serialized to XAML
    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();
    }
    
    /// 
    /// Deserializes an object from xaml.
    /// 
    /// The xaml text.
    /// The deserialized object
    /// Thrown if the serialized text is not well formed XML
    /// Thrown if unable to deserialize from xaml
    private object Deserialize(string xamlText)
    {
        XmlDocument doc = new XmlDocument();
        // may throw XmlException
        doc.LoadXml(xamlText);
        // may throw XamlParseException
        return XamlReader.Load(new XmlNodeReader(doc));
    }
    

    0 讨论(0)
提交回复
热议问题