gpt4 book ai didi

wpf - 如何序列化 xaml "Brush"?

转载 作者:行者123 更新时间:2023-12-04 18:52:14 25 4
gpt4 key购买 nike

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

最佳答案

这里有一个关于如何序列化 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));
}

关于wpf - 如何序列化 xaml "Brush"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4488476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com