我正在创建一个实时 WPF 图形绘图仪,它将在接收到数据点时绘制数据点。它利用动态数据显示库。( http://dynamicdatadisplay.codeplex.com/ )
目前,应用程序已设置为:
每当我在 WPF 应用程序中实例化的 ObservableCollection 发生更改时,都会更新图形绘图仪。
使用名为 AddDataPoint(...) 的自定义方法添加数据点,该方法修改 ObservableCollection。
应用程序在环境中按预期运行(当我按 F5 调试我的解决方案时),但这只是因为我传递了“假”数据点来测试应用程序。我利用内部 DispatchTimer/Random.Next(..) 将生成的数据点不断输入到内部实例化的 ObservableCollection;但是,我不知道如何允许外部类或外部数据源输入要绘制的“真实”数据。
总的来说,我对 WPF 和 C# 真的很陌生,所以虽然我确实在这个主题上做了很多谷歌搜索,但我找不到具体的答案(即数据绑定——在我看来,它只是为了使用在应用程序中也是如此)。在将实时数据从外部源传递到我的 WPF 应用程序时,我陷入了困境。
我尝试将 WPF 的 .exe 文件作为资源添加到将提供数据并使用以下方法启动 WPF 实例的外部类/解决方案:
"WPFAppNameSpace".MainWindow w = new "WPFAppNameSpace".MainWindow();
w.Show();
w.AddDataPoint(...);
但是,它没有用。窗口甚至没有出现!我可以让外部类(不是 WPF 应用程序)将数据传递到我的 WPF 图形应用程序吗?如果是这样,我该怎么做?我应该研究什么?
以下是我的项目中的一些代码片段:
XAML
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d3="http://research.microsoft.com/DynamicDataDisplay/1.0"
Title="MainWindow" Height="480" Width="660" Loaded="Window_Loaded" WindowState="Maximized">
<Grid>
<d3:ChartPlotter Name="plotter" Margin="12,10,12,14">
<d3:ChartPlotter.MainHorizontalAxis>
<d3:HorizontalAxis Name="xAxis"></d3:HorizontalAxis>
</d3:ChartPlotter.MainHorizontalAxis>
<d3:ChartPlotter.MainVerticalAxis>
<d3:VerticalAxis Name="yAxis"></d3:VerticalAxis>
</d3:ChartPlotter.MainVerticalAxis>
<d3:VerticalAxisTitle Content="Voltage"/>
<d3:HorizontalAxisTitle Content="Test Identifier"/>
<d3:Header TextBlock.FontSize="20" Content="Dynamically Updated Graph"/>
</d3:ChartPlotter>
</Grid>
</Window>
主窗口.xaml.cs
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
//Dummy Variables for Testing
private readonly Random rand = new Random();
private DispatcherTimer dt = new DispatcherTimer();
...
//Data Sources for Graph
private List<EnumerableDataSource<DataPoint>> enumSources;
private List<ObservableCollection<DataPoint>> observableSources;
private int dataSourceIndex = 0;
public MainWindow()
{
InitializeComponent();
}
//Automatically Called after MainWindow() Finishes. Initlialize Graph.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//Initlizes Data Sources for Graph
enumSources = new List<EnumerableDataSource<DataPoint>>();
observableSources = new List<ObservableCollection<DataPoint>>();
//Adds a new Source to the Graph (New Line on Graph)
addNewSource("Test Data " + dataSourceIndex);
//TESTING PURPOSES
dt.Interval = new TimeSpan(25000);
dt.Tick += new EventHandler(timerAction);
dt.IsEnabled = true;
dt.Start();
}
//Adds data into the observableSource and alerts the enumSource to add point into Graph
private void AsyncAppend(...) {...}
//Adds a new DataPoint onto the Graph and Specifies if it starts a new Line.
public void AddDataPoint(..., bool newLine) {...}
//Tests Function for Adding New Points/New Lines to Graph; Called by Timer
private void timerAction(object sender, EventArgs e)
{
//current count of points in a particular line
var count = observableSources[dataSourceIndex].Count;
if (count < 100)
{
//Adds Data to Current Line
AddDataPoint(...);
}
else
{
//Starts New Line and Adds Data
AddDataPoint(..., true);
}
}
//Adds a new Data Source onto the Graph (Starts a New Line)
private void addNewSource(string legendKey){...}
//DataPoint Object to Pass into the Graph
private class DataPoint
{
//X-Coord of the Point
public double xCoord { get; set; }
//Y-Coord of the Point
public double yCoord { get; set; }
//DataPoint's Label Name
public string labelName { get; set; }
//Constructor for DataPoint
public DataPoint(double x, double y, string label = "MISSNG LBL")
{
xCoord = x;
yCoord = y;
labelName = label;
}
}
}