Newtonsoft.Json.JsonSerializationException:无法找到用于 System.Windows.Ink.Stroke 类型的构造函数

问题描述 投票:0回答:1

我正在尝试从 WPF InkCanvas 中保存笔划和子项。我设法保存文件,但现在无法加载它。它抛出这个异常:

Newtonsoft.Json.JsonSerializationException H结果=0x80131500 消息=无法找到用于 System.Windows.Ink.Stroke 类型的构造函数。类应该具有一个默认构造函数、一个带参数的构造函数或一个标有 JsonConstructor 属性的构造函数。路径“笔触[0].DrawingAttributes”,第 4 行,位置 26。 来源=Newtonsoft.Json

保存/加载类的代码:

class SaveLoad
{
    public static void Save(Board board) {
        Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
        dlg.FileName = "Document";
        dlg.DefaultExt = ".txt";
        dlg.Filter = "Text documents (.txt)|*.txt";
        Nullable<bool> result = dlg.ShowDialog();
        if (result == true)
        {
            string outpath = dlg.FileName;
            string jsonData = JsonConvert.SerializeObject(board, Newtonsoft.Json.Formatting.Indented);
            var myFile = File.Create(outpath);
            myFile.Close();
            File.WriteAllText(@"" + outpath, jsonData);
        }
    }

    public static Board Load()
    {
        Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
        dlg.FileName = "Document";
        dlg.DefaultExt = ".txt";
        dlg.Filter = "Text documents (.txt)|*.txt";
        Nullable<bool> result = dlg.ShowDialog();
        if (result == true)
        {
            string outpath = dlg.FileName;
            string jsonData = File.ReadAllText(outpath);
            return JsonConvert.DeserializeObject<Board>(jsonData);
        }
        else { return null; }
    }
}

internal class Board
{
    public StrokeCollection strokes;
    public UIElementCollection elements;
    public UIElementCollection elements_serialized = new UIElementCollection(new UIElement(),new FrameworkElement());

    public Board(StrokeCollection strokes, UIElementCollection elements) { this.strokes = strokes; this.elements = elements; }

    public void Serialize() {
        foreach (UIElement element in this.elements) {
            elements_serialized.Add(Clone(element));
        }
    }

    public Board getSerialized() {
        return new Board(strokes,elements_serialized);
    }

    public static T Clone<T>(T element)
    {
        string xaml = XamlWriter.Save(element);
        using (StringReader stringReader = new StringReader(xaml))
        using (XmlReader xmlReader = XmlReader.Create(stringReader))
            return (T)XamlReader.Load(xmlReader);
    }
}

保存代码:

Board board = new Board(mainInk.Strokes, mainInk.Children);
board.Serialize();
SaveLoad.Save(board.getSerialized());

加载代码:

 Board board = SaveLoad.Load();
 if (board == null) {
     mainInk.Strokes = board.strokes;
     mainInk.Children.Clear();
     foreach (UIElement element in board.elements)
     {
         mainInk.Children.Add(Board.Clone(element));
     }
 }
c# wpf json.net inkcanvas
1个回答
0
投票

实现一个自定义转换器来处理 Stroke 对象的序列化和反序列化。因为这个类没有构造函数参数 less。

public class StrokeConverter : JsonConverter
{
//overrides here
}
© www.soinside.com 2019 - 2024. All rights reserved.