我正在尝试实现一个Winform应用程序,该应用程序在折线图上显示一组示例(从HW设备形式)。
我使用了INotifyPropertyChanged接口并将图表绑定到硬件设备的模型,但是当在硬件模型上更改样本时,似乎图表没有得到更新。
抱歉,如果这太基础了(我更多是嵌入式人员),但是似乎我缺少将INotifyPropertyChanged事件连接到数据绑定程序的部分。
这里缺少什么吗?还是应该以其他方式实施?
在WinForm类中,我编写了以下代码,以将图表绑定到HW模型的示例。当“ ADCSamples”更改时,按钮应演示大小写:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
StreamChart.Series[0].Points.DataBindY(GSWatch.ADCSamples);
}
private GSWatchModel GSWatch = new GSWatchModel();
private void button1_Click(object sender, EventArgs e)
{
uint[] muki = new uint[128];
for (int i = 0; i < 128; i++)
{
muki[i] = (uint)(i / 10);
}
GSWatch.ADCSamples = muki;
//StreamChart.Series[0].Points.DataBindY(GSWatch.ADCSamples); //The chart is only updated if this line is executed
}
private void button2_Click(object sender, EventArgs e)
{
GSWatch.StartStreamADC();
//StreamChart.Series[0].Points.DataBindY(GSWatch.ADCSamples); //The chart is only updated if this line is executed
}
}
在硬件模型中,我编写了以下代码来实现INotifyPropertyChanged功能:
public class GSWatchModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private uint[] aDCSamples = new uint[128];
public uint[] ADCSamples
{
get
{
return aDCSamples;
}
set
{
aDCSamples = value;
NotifyPropertyChanged();
}
}
public GSWatchModel()
{
CommLink = new GSCommManager();
for (int i = 0; i < 128; i++)
{
aDCSamples[i] = (uint)(i); //initial values for demo
}
}
uint muki = 0;
public void StartStreamADC()
{
GSPacket StreamRequestPacket = new GSPacket(GSPTypes.Stream);
CommLink.SendViaGSWatchLink(StreamRequestPacket);
for (int i = 0; i < 128; i++)
{
aDCSamples[i] = (uint)i / 10; //steps for demonstration
}
NotifyPropertyChanged();
muki += 100;
}
}
StartStreamADC
在绑定之前移动...参见下文:
private void Form1_Load(object sender, EventArgs e)
{
GSWatchModel GSWatch = new GSWatchModel();
GSWatch.StartStreamADC();
StreamChart.Series[0].Points.DataBindY(GSWatch.ADCSamples);
}
结果:
为了得到通知,请执行以下操作:
private void Form1_Load(object sender, EventArgs e)
{
GSWatch = new GSWatchModel();
GSWatch.StartStreamADC();
StreamChart.Series[0].Points.DataBindY(GSWatch.ADCSamples);
GSWatch.PropertyChanged += GSWatch_PropertyChanged;
}
private void GSWatch_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
StreamChart.Series[0].Points.DataBindY(GSWatch.ADCSamples);
}
另外,请更改ADCSamples
为此:
public List<uint> ADCSamples = new List<uint>();
它将为您节省很多头痛。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句