简体   繁体   English

创建Oxyplot PlotModel,它会随着传入的新数据自动更新

[英]Creating Oxyplot PlotModel that automatically updates with new data incoming

I've been following this excellent tutorial found here in order to learn how to create LineSeries plots using OxyPlot while following MVVC. 我一直在遵循此处找到的出色教程,以了解如何在遵循MVVC的同时使用OxyPlot创建LineSeries图。 I've also downloaded, and after some error fixing, managed to get the author's source code for the tutorial to compile and run. 我还下载了该文件,并在进行了一些错误修复后,设法获得了作者的源代码,以供本教程编译和运行。 The source code on Github can be found here . Github上的源代码可以在这里找到。

Unfortunately, while the code runs and the chart is created, the chart doesn't update itself every 5 seconds like it should. 不幸的是,当代码运行并创建图表时,图表不会像需要的那样每5秒更新一次。 This is what the chart looks like on my machine forever. 这就是我的机器上永远的图表。 It doesn't update: 它不会更新:

在此处输入图片说明

This is what it should look like, more plots should show as time increases: 这是它的样子,随着时间的增加,将显示更多的图: 在此处输入图片说明

Now, I've done some digging of my own to find out why it isn't updating. 现在,我做了一些挖掘工作,以了解为什么它没有更新。 I've added some debugging commands to find whether new points are added correctly, as below: 我添加了一些调试命令来查找是否正确添加了新点,如下所示: 在此处输入图片说明

I've checked that UpdateModel gets called every 5 seconds, and that measurements are correctly returning from GetUpdateData, and that the lineSerie.Points count is increasing before the end of the function. 我检查了是否每5秒调用一次UpdateModel,并从GetUpdateData正确返回测量值,并且在函数结束之前lineSerie.Points计数在增加。 InvalidatePlot is also called in the actual View class. 在实际的View类中也会调用InvalidatePlot。

Is there anything else I can do to solve this problem? 有什么我可以解决的问题吗? Thanks 谢谢

I figured it out. 我想到了。 According to this tutorial here (Create The View section), I thought I had to put: 根据此处的本教程 (“创建视图”部分),我认为我必须把:

<Window.DataContext>
    <local:MainViewModel/>
</Window.DataContext>

in my XAML file. 在我的XAML文件中。 But according to the tutorial project, you don't need it: 但是根据教程项目,您不需要它:

<Window x:Class="OxyPlotDemo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:oxy="http://oxyplot.org/wpf"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <oxy:PlotView x:Name="Plot1" Model="{Binding PlotModel}" Margin="10" Grid.Row="1">
    </oxy:PlotView>
</Grid>

Removing the local.MainViewModel line fixed the issue. 删除local.MainViewModel行解决了该问题。 I think it was using the wrong PlotModel, instead of the correct one created in the View class 我认为它使用了错误的PlotModel,而不是在View类中创建的正确模型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM