【问题标题】:How can i serialize xaml "Brush"?我怎样才能序列化xaml“画笔”?
【发布时间】:2011-05-28 04:07:15
【问题描述】:

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

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    这里很好地讨论了如何序列化 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));
    }
    

    【讨论】:

    • 此答案中的链接已不存在!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 2023-01-18
    • 2012-02-03
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多