2

我在网站上找到了所有关于那个问题的答案,将代码更改为以下格式不再是错误,但是委托没有执行a里面的语句,这是怎么回事?任何人都可以帮助我吗?我的程序每 2 秒使用多媒体计时器绘制一个曲线点,用 Visifire 绘制曲线

Thread Messagethread = new Thread(new ThreadStart(delegate() {
                DispatcherOperation DispacherOP = Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Normal, 
                    new Action(delegate() {
                    ChartData.Add(new Tuple<string, double>("A" + seed.NextDouble(), 1.5 + seed.Next(10)));
                })); 
            }));
            Messagethread.SetApartmentState(ApartmentState.STA);
            Messagethread.Start();

我的视图模型页面:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows;
using System.Collections.ObjectModel;
using Dongzr.MidiLite;
using System.Windows.Threading;
using System.Threading;

namespace WpfVisifire
{
    public class ChartViewModel
    {
        static MmTimer timer1;
        static DispatcherTimer timer2;

        private static readonly Random seed = new Random();
        public ObservableCollection<Tuple<string, double>> ChartData
        {
            get;
            private set;
        }

        public ChartViewModel()
        {
            StopDataCommand = new RelayCommand((p) => stop());
            ChangeVisiChartDataCommand = new RelayCommand((p) => changeData());
            ChartData = new ObservableCollection<Tuple<string, double>>();
            timer1 = new MmTimer();
        }
        public ICommand StopDataCommand
        {
            get;
            private set;
        }
        public ICommand ChangeVisiChartDataCommand
        {
            get;
            private set;
        }
        private void changeData()
        {
            timer1.Mode = MmTimerMode.Periodic;
            timer1.Interval = 2000;
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Start();           
        }

        void timer1_Tick(object sender, EventArgs e)
        {
           /*Dispatcher.CurrentDispatcher.BeginInvoke(
                 DispatcherPriority.Normal,
                 new Action(
                     delegate()
                     {
                         ChartData.Add(new Tuple<string, double>("A" + seed.NextDouble(), 1.5 + seed.Next(10)));
                     }));*/
            Thread Messagethread = new Thread(new ThreadStart(delegate() {
                DispatcherOperation DispacherOP = Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Normal, 
                    new Action(delegate() {
                    ChartData.Add(new Tuple<string, double>("A" + seed.NextDouble(), 1.5 + seed.Next(10)));
                })); 
            }));
            Messagethread.SetApartmentState(ApartmentState.STA);
            Messagethread.Start(); 
        }

        private void stop()
        {
            timer1.Stop();
            timer1.Dispose();
            //.Show("jeighier");
        }

    }
}`

主窗口.xaml

<Window x:Class="WpfVisifire.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vCharts="clr-namespace:Visifire.Charts;assembly=WPFVisifire.Charts"
        xmlns:vm="clr-namespace:WpfVisifire"
        Title="MainWindow" Height="600" Width="525">
    <Window.Resources>
        <ResourceDictionary>
            <vm:ChartViewModel x:Key="chartViewModel" />
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <StackPanel DataContext="{Binding Source={StaticResource chartViewModel}}">
            <WrapPanel Orientation="Horizontal">
                <Button Content="Start" Height="28" Name="Add" Margin="5" Width="125" Command="{Binding Path=ChangeVisiChartDataCommand}"/>
                <Button Margin="5" Height="28" Width="125" Content="Stop" Command="{Binding Path=StopDataCommand}" />
            </WrapPanel>

            <vCharts:Chart Watermark="False" Theme="Theme1" Width="480" Height="479" x:Name="MyChart"
                            AnimationEnabled="True" AnimatedUpdate="True">
                <vCharts:Chart.Titles>
                    <vCharts:Title Text="This is a chart" FontSize="12" />
                    <vCharts:Title Text="This is another chart" FontSize="10" HorizontalAlignment="Right" />
                </vCharts:Chart.Titles>
                <vCharts:Chart.AxesX>
                    <vCharts:Axis Title="horizontal title" />
                </vCharts:Chart.AxesX>
                <vCharts:Chart.AxesY>
                    <vCharts:Axis Title="vertical title" />
                </vCharts:Chart.AxesY>
                <vCharts:Chart.Series>
                    <vCharts:DataSeries x:Name="dataSeries" RenderAs="Line"  DataSource="{Binding Path=ChartData}">
                        <vCharts:DataSeries.DataMappings>
                            <vCharts:DataMapping MemberName="AxisXLabel" Path="Left" />
                            <vCharts:DataMapping MemberName="YValue" Path="Right" />
                        </vCharts:DataSeries.DataMappings>
                    </vCharts:DataSeries>
                </vCharts:Chart.Series>
            </vCharts:Chart>
        </StackPanel>
    </Grid>
</Window>
4

2 回答 2

0

尝试一些更简单的东西

timer1.Tick += (o,e) => Dispatcher.BeginInvoke((Action)(() => 
     ChartData.Add(Tuple.Create("A" + seed.NextDouble(), 1.5 + seed.Next(10)))); 
于 2011-12-07T13:23:22.373 回答
0

尝试在您的 Tick 处理程序中执行绘图过程。如果在主线程上未执行刻度处理程序,则将 Dispatcher 实例保存在局部变量中并在

public ChartViewModel()

像这样 :

myDispatcher=Dispatcher.CurrentDispatcher

然后在计时器滴答处理程序中使用 myDispatcher.Invoke

于 2011-12-07T13:08:34.290 回答