Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 调用线程必须是STA,因为许多UI组件都需要它_Wpf_Visifire - Fatal编程技术网

Wpf 调用线程必须是STA,因为许多UI组件都需要它

Wpf 调用线程必须是STA,因为许多UI组件都需要它,wpf,visifire,Wpf,Visifire,我在网站上找到了关于这个问题的所有答案,代码更改为以下格式不再是一个错误,但是代理没有在a中执行语句,怎么了?有人能帮我吗?我的程序使用多媒体定时器每2秒绘制一个曲线点,用Visifire绘制曲线 Thread Messagethread = new Thread(new ThreadStart(delegate() { DispatcherOperation DispacherOP = Dispatcher.CurrentDispatcher.BeginInvo

我在网站上找到了关于这个问题的所有答案,代码更改为以下格式不再是一个错误,但是代理没有在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();
threadmessagethread=newthread(newthreadstart(delegate)(){
DispatcherOperation DispatcherOpp=Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Normal,
新操作(委托(){
添加(新元组(“A”+seed.NextDouble(),1.5+seed.Next(10));
})); 
}));
Messagethread.SetApartmentState(ApartmentState.STA);
Messagethread.Start();
我的ViewModel页面:

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");
        }

    }
}`
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows.Input;
使用System.Windows;
使用System.Collections.ObjectModel;
使用东锆米长岩;
使用System.Windows.Threading;
使用系统线程;
命名空间WpfVisifire
{
公共类图表视图模型
{
静态定时器1;
静态调度器定时器2;
私有静态只读随机种子=新随机();
公共可观测收集图表数据
{
得到;
私人设置;
}
公共图表视图模型()
{
StopDataCommand=newrelaycommand((p)=>stop());
ChangeVisiChartDataCommand=新的RelayCommand((p)=>changeData());
ChartData=新的ObservableCollection();
timer1=新的MmTimer();
}
公共ICommand StopDataCommand
{
得到;
私人设置;
}
公共ICommand ChangeVisiChartDataCommand
{
得到;
私人设置;
}
私有void changeData()
{
timer1.Mode=MmTimerMode.Periodic;
计时器1.间隔=2000;
timer1.Tick+=新事件处理程序(timer1\u Tick);
timer1.Start();
}
无效计时器1_刻度(对象发送方,事件参数e)
{
/*Dispatcher.CurrentDispatcher.BeginInvoke(
调度员优先权,正常,
新行动(
代表()
{
添加(新元组(“A”+seed.NextDouble(),1.5+seed.Next(10));
}));*/
Thread Messagethread=new Thread(new ThreadStart(delegate()){
DispatcherOperation DispatcherOpp=Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Normal,
新操作(委托(){
添加(新元组(“A”+seed.NextDouble(),1.5+seed.Next(10));
})); 
}));
Messagethread.SetApartmentState(ApartmentState.STA);
Messagethread.Start();
}
私人停车场()
{
timer1.Stop();
timer1.Dispose();
//.Show(“jeighier”);
}
}
}`
MainWindow.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>

尝试在记号处理程序中执行绘图过程。如果未在主线程上执行tick处理程序,请在局部变量中保存一个Dispatcher实例,并在中初始化它

public ChartViewModel()
像这样:

myDispatcher=Dispatcher.CurrentDispatcher

然后在计时器刻度处理程序中使用myDispatcher.Invoke,尝试在刻度处理程序中执行绘图过程。如果未在主线程上执行tick处理程序,请在局部变量中保存一个Dispatcher实例,并在中初始化它

public ChartViewModel()
像这样:

myDispatcher=Dispatcher.CurrentDispatcher

然后使用计时器刻度处理程序中的myDispatcher.Invoke,尝试更简单的方法

timer1.Tick += (o,e) => Dispatcher.BeginInvoke((Action)(() => 
     ChartData.Add(Tuple.Create("A" + seed.NextDouble(), 1.5 + seed.Next(10)))); 

试试简单一点的

timer1.Tick += (o,e) => Dispatcher.BeginInvoke((Action)(() => 
     ChartData.Add(Tuple.Create("A" + seed.NextDouble(), 1.5 + seed.Next(10)))); 

对不起,你能澄清问题是什么吗?对不起,你能澄清问题是什么吗?谢谢你的回复!Dispatcher myDispatcher=Dispatcher.CurrentDispatcher;是的,谢谢你的回复!Dispatcher myDispatcher=Dispatcher.CurrentDispatcher;没错。