序列化WPF图像控件,但在运行时忽略Source属性?

时间:2019-02-13 14:47:57

标签: c# wpf serialization

我目前正在使用WPF应用程序之类的编辑器,您可以在其中选择编辑控件,然后将打开一个对话框,其中包含控件的副本。对于控件的复制,我使用了XamlReader.Parse(string)XamlWriter.Save(obj)

现在有一些(自定义)控件,而不包含将Source设置为WriteableBitmap的Image控件。位图不可序列化,因此序列化程序会引发一些异常,但这不是主要问题,因为不需要序列化,因为以后无论如何它都会被更改。

是否可以序列化 WPF控件,并告诉序列化器忽略其子级的属性,或使用可序列化的内容(例如在运行时

我已经尝试在序列化之前和之后手动设置source属性,但是由于某些未知原因,我无法更改反序列化控件的Source。

对话框代码

public void LaunchEditor()
{
    Editor dlg = new Editor ();
    dlg.Owner = Application.Current.MainWindow;
    dlg.SetTemplateString(XamlWriter.Save(this));
    if (dlg.ShowDialog() ?? false)
    {
        string s = dlg.GetTemplateString();
        CustomControlType c = (CustomControlType)XamlReader.Parse(s);
        Content = c;
    }
}

序列化代码

public void SetTemplateString(string template)
{
    Placeholder = (CustomControlType)XamlReader.Parse(template);

    /*Formatting Stuff*/
}

public string GetTemplateString()
{           
    StringBuilder sb = new StringBuilder();
    XmlWriterSettings settings = new XmlWriterSettings()
    {
        Indent = true
    };
    XmlWriter writer = XmlWriter.Create(sb,settings);
    XamlDesignerSerializationManager mgr = new XamlDesignerSerializationManager(writer);
    XamlWriter.Save(Placeholder, mgr);
    return sb.ToString();
}

包含图片的自定义控件

public partial class RollingViewControl : ContentConstrol
{
    private RollingBitmap _bmp;
    public RollingViewControl()
    {
        _bmp = new RollingBitmap(256, 256);
        InitializeComponent();
        ImageDisplay.Source = _bmp.Bitmap;
    }
}

RollingBitmap是用于操纵WriteableBitmap此类的类。

问题是图像控件的Source属性,当序列化控件时,源也会被序列化。因为它是预编译的代码,所以我无法设置任何属性,也找不到任何将XmlAttributeOverrides应用于XamlWriter的方法。

0 个答案:

没有答案