【问题标题】:WPF UI ThreadingWPF UI 线程
【发布时间】:2013-03-16 19:10:44
【问题描述】:

我正在尝试更新图表并在那里绘制一些散点。为此,我使用BackgroundWorker 类。它确实有效。但是我注意到,一旦我向我的 Point 类添加颜色并想要显示不同颜色的点,它就会崩溃。为什么?有什么想法吗?

    public class ChartData
    {
        private readonly Brush Red = new SolidColorBrush(Colors.Red);
        private readonly Brush Orange = new SolidColorBrush(Colors.Orange);
        private readonly Brush Green = new SolidColorBrush(Colors.Green);

        public ChartData(double x, double y)
        {
            this.XValue = x;
            this.YValue = y;
        }

        public double XValue { get; set; }
        public double YValue { get; set; }

        public Brush Brush{ get; set;}
    }

    <telerik:ScatterPointSeries XValueBinding="XValue" 
                            YValueBinding="YValue" 
                            ItemsSource="{Binding Data}" >
        <telerik:ScatterPointSeries.PointTemplate>
            <DataTemplate>
                <Ellipse Width="10"
             Height="10"
             Fill="{Binding DataItem.Brush}"/>
            </DataTemplate>
        </telerik:ScatterPointSeries.PointTemplate>
    </telerik:ScatterPointSeries>

例外:Must create DependencySource on same Thread as the DependencyObject.

【问题讨论】:

  • 它是如何崩溃的?发生什么了?它会抛出异常吗?如果是,是什么?
  • 是的,它抛出异常Must create DependencySource on same Thread as the DependencyObject.
  • hmmm,您是否尝试过使用调度程序在 GUI 线程上设置点数据的画笔?

标签: wpf multithreading mvvm telerik backgroundworker


【解决方案1】:

正如消息本身所述,您不能从另一个线程修改DependencySource(add,create,delete)。您可以从同一个线程修改它,在您的情况下是 UI 线程。

你可以把modifying dependency source on UI thread dispatcher的代码作为一个出路

App.Current.Dispatcher.Invoke((Action)delegate()
            {
                // Code here for updating Dependency Source.
            });

【讨论】:

  • 感谢您的回答!不幸的是,相同的结果或应用程序只是被冻结了。这可能是因为 MVVM 的问题吗?谢谢!
  • 其实是我的错。以适当的方式做到了。效果很好!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-17
  • 2014-08-24
  • 2011-02-24
  • 2014-03-23
相关资源
最近更新 更多