0

我有一个 Silverlight 应用程序,它只是在图表上绘制点。它绘制的点来自 sql 查询。

silverlight 程序必须自行运行查询并提取相关数据。

我如何实现这种功能?

谢谢!

4

1 回答 1

1

您没有说每秒/分钟需要绘制多少次数据?是否只是绘制一次。如果是这样,那么当您的应用程序首次加载时编写一个异步调用并让调用查询 sql 并在回调中返回结果..

如果程序需要以设定的时间间隔返回数据,那么您将需要一个调度程序计时器或类似的东西..

好的像这样..

public class MyClass : INotifyPropertyChanged
{
    public MyClass()
    {
        DispatcherTimer timer = new DispatcherTimer();
        timer.Tick += OnTimerTick;
        timer.Interval = TimeSpan.FromSeconds(300);
    }

private void OnTimerTick(object sender, EventArgs e)
    {
        var result = await UpdateGraphPoints();
        MyGraphPoints = this.PopulateTheGraph(result);
    }

    private async Task<List<MyGraphPoint>> UpdateGraphPoints()
    {
        var oper = await YourDatabaseQueryMethod();
        return oper;
    }

    private ObservableCollection<MyGraphPoint> PopulateTheGraph(object result)
    {

    }

    private ObservableCollection<MyGraphPoint> myGraphPoints;

    public ObservableCollection<MyGraphPoint> MyGraphPoints
    {
        get { return this.myGraphPoints; }
        set
        {
            myGraphPoints = value;
            OnPropertyChanged("MyGraphPoints");
        }
    }

private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}

public class MyGraphPoint : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private int xValue;

    public int XValue
    {
        get { return xValue; }
        set
        {
            this.xValue = value;
            this.OnPropertyChanged("XValue");
        }
    }

    private int yValue;

    public int YValue
    {
        get { return yValue; }
        set
        {
            this.yValue = value;
            this.OnPropertyChanged("YValue");
        }
    }

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

然后在您的 xaml 中 - 将 MyGraphPoints 可观察集合绑定到您的图形控件。

于 2012-12-08T18:02:52.053 回答