我对 LiveCharts 和 XamlReader(序列化)有疑问。
MyUserControlWithLiveChart (XAML):
<UserControl>
<Grid>
<liveCharts:LineChart LineSmoothness="0">
<liveCharts:LineChart.Series>
<liveCharts:LineSeries Values="1, 5, 8" />
<liveCharts:LineSeries Values="3, 6, 2" />
</liveCharts:LineChart.Series>
</liveCharts:LineChart>
</Grid>
</UserControl>
流文档(XAML):
<FlowDocument>
<BlockUIContainer x:Name="ReportContainer">
<!-- I add my UserControl with LiveChart here in runtime. -->
</BlockUIContainer>
</FlowDocument>
创建 MyUserControlWithLiveChart 并将其作为子项添加到 BlockUIContainer (C#) 中:
_mainWindow.Dispatcher.Invoke(new Action(delegate()
{
var chartControl = new MyUserControlWithLiveChart();
ReportContainer.Child = chartControl;
}));
当我的 FlowDocument 准备就绪时,我将其序列化到后台线程 (C#) 中的 StreamMemory 中:
_stream = new MemoryStream();
_mainWindow.Dispatcher.BeginInvoke(new Action(delegate ()
{
XamlWriter.Save(_myFlowDocument, _stream); // #1 ERROR HERE!
_stream.Position = 0;
}));
在我在主 UIThread 中反序列化我的 FlowDocument 并尝试在 FlowDocumentScrollViewer (C#) 中显示它之后:
_mainWindow.Dispatcher.BeginInvoke(new Action(delegate ()
{
_myFlowDocument = (FlowDocument)XamlReader.Load(_stream); // #2 ERROR HERE!
_stream.Close();
_mainWindow.MyFlowDocumentScrollViewer.Document = _myFlowDocument;
}));
但是当我尝试使用 LiveChart 控件对我的 FlowDocument 进行去实化时,我遇到了异常。
1 错误:
Cannot serialize a generic type
"System.Collections.Generic.List`1[LiveCharts.Axis]"
2 错误:
XamlParserEception occurred.
Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll
Additional information: Root element is missing.