【问题标题】:Serialization of a OxyPlot Chart to XAML将 OxyPlot 图表序列化为 XAML
【发布时间】:2018-11-18 14:00:12
【问题描述】:

我为创建 XAMLLayout 生成了不同的 UI 模型(文本块、图像)的 PageBuilder。 为此,我将所有元素放在 Canvas 上并将完整的 Canvas 保存到 Xaml 文件(使用 XamlWriter.Save-Method)。

现在我需要生成图表并对其进行序列化。 使用 OxyPlot-Library,我使用此代码通过运行时生成代码。

OxyPlot.Wpf.PlotView pv = new OxyPlot.Wpf.PlotView();
OxyPlot.PlotModel pm = new OxyPlot.PlotModel();

pv.Height = 300;
pv.Width = 500;
pv.Background = Brushes.Red;
pm.Title = "Test";
pm.Series.Add(new OxyPlot.Series.FunctionSeries(Math.Cos, 0, 10, 0.1, "cos(x)"));
pv.Model = pm;

canvas.Children.Add(pv);

对于序列化,我只需调用我的 Canvas 并像这样序列化整个对象:

XamlWriter.Save(canvas);

在这个过程中我遇到了一个异常:

System.InvalidOperationException:“泛型类型”System.Collections.Generic.List`1 [OxyPlot.OxyColor]“无法序列化。”

【问题讨论】:

    标签: wpf xaml serialization charts oxyplot


    【解决方案1】:

    此时,Generic 类型在 xaml 中的支持非常有限。

    XamlWriter 无法序列化泛型列表类型的属性。您可以尝试以下解决方法来解决此问题。

    public class OxyPlot
    {
        public OxyColors OxyColor { get; set; }
    }
    
    public class OxyColors : List<OxyColor>
    {
    
    }
    
    public class OxyColor
    {
    
    }
    

    【讨论】:

    • 好的,我应该把它添加到我的主类中吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    • 2023-03-13
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    相关资源
    最近更新 更多