MainWindow
クラスで指定されたリアルタイムチャートを表示するクラスがありますDataChart
。アプリを実行すると、DataChart
クラスのコンストラクターで新しいスレッドを開始するため、チャートは新しいデータの追加と更新を開始します。MainWindow
しかし、アプリの起動後ではなく、クラスで定義されたボタンをクリックした後にチャートの更新を開始する必要があります。しかし、同じ Thred を から開始するとMainWindow
、チャートは更新されず、PropertyChangedEventHandler
null になります。
でMainWindow
:
private void connectBtn_Click(object sender, RoutedEventArgs e)
{
DataChart chart = new DataChart();
Thread thread = new Thread(chart.AddPoints);
thread.Start();
}
でDataChart
:
public class DataChart : INotifyPropertyChanged
{
public DataChart()
{
DataPlot = new PlotModel();
DataPlot.Series.Add(new LineSeries
{
Title = "1",
Points = new List<IDataPoint>()
});
m_userInterfaceDispatcher = Dispatcher.CurrentDispatcher;
//WHEN I START THREAD HERE IT WORKS AND PROPERTYCHANGED IS NOT NULL
//var thread = new Thread(AddPoints);
//thread.Start();
}
public void AddPoints()
{
var addPoints = true;
while (addPoints)
{
try
{
m_userInterfaceDispatcher.Invoke(() =>
{
(DataPlot.Series[0] as LineSeries).Points.Add(new DataPoint(xvalue,yvalue));
if (PropertyChanged != null) //=NULL WHEN CALLING FROM MainWindow
{
DataPlot.InvalidatePlot(true);
}
});
}
catch (TaskCanceledException)
{
addPoints = false;
}
}
}
public PlotModel DataPlot
{
get;
set;
}
public event PropertyChangedEventHandler PropertyChanged;
private Dispatcher m_userInterfaceDispatcher;
}
チャートが更新されない問題はそれだと思いますが、PropertyChanged=null
解決方法がわかりません。OxyPlot
役に立ったら使っています。
MainWindow.xaml
:
<oxy:Plot Model="{Binding DataPlot}" Margin="10,10,10,10" Grid.Row="1" Grid.Column="1"/>