如何使用Dispatcher设置Image.Source属性?

| 我尝试使用以下建议:http://msdn.microsoft.com/zh-cn/library/ms741870.aspx(\“处理具有后台线程的阻止操作\”)。 这是我的代码:
  public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ;
        }

        private void Process()
        {
            // some code creating BitmapSource thresholdedImage

            ThreadStart start = () =>
            {
                Dispatcher.BeginInvoke(
                    System.Windows.Threading.DispatcherPriority.Normal,
                    new Action<ImageSource>(Update),
                    thresholdedImage);
            };
            Thread nt = new Thread(start);
            nt.SetApartmentState(ApartmentState.STA);
            nt.Start();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            button1.IsEnabled = false;
            button1.Content = \"Processing...\";

            Action action = new Action(Process);
            action.BeginInvoke(null, null);
        }

        private void Update(ImageSource source)
        {
            this.image1.Source = source; // ! this line throw exception

            this.button1.IsEnabled = true; // this line works
            this.button1.Content = \"Process\"; // this line works
        }
    }
在标记的行中,它引发InvalidOperationException \“,因为调用线程无法访问此对象,因为另一个线程拥有它。\”。但是,如果我删除此行,则应用程序有效。
XAML:
<!-- language: xaml -->
    <Window x:Class=\"BlackZonesRemover.MainWindow\"
            xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"
            xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"
            xmlns:my=\"clr-namespace:BlackZonesRemover\"
            Title=\"MainWindow\" Height=\"600\" Width=\"800\" Loaded=\"Window_Loaded\">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition Height=\"Auto\" />
            </Grid.RowDefinitions>
            <Border Background=\"LightBlue\">
                <Image Name=\"image1\" Stretch=\"Uniform\" />
            </Border>
            <Button Content=\"Button\" Grid.Row=\"1\" Height=\"23\" Name=\"button1\" Width=\"75\" Margin=\"10\" Click=\"button1_Click\" />
        </Grid>
    </Window>
Image.Source属性与Button.IsEnabled和Button.Content属性之间有什么区别?我能做什么?     
已邀请:
        具体建议:正在后台线程上创建“ 2”。在UI线程上创建它,或者一旦创建它就冻结它。 一般建议:区别在于
ImageSource
DependencyObject
,因此具有线程相似性。因此,您需要在与要分配给它的ѭ6相同的线程上创建
ImageSource
(即UI线程),或者一旦创建了它,就需要在need3ѭ上创建
Freeze()
,从而允许任何线程访问它。     
        问题是因为在后台线程中创建了“ 9”,并在UI线程上使用了它。 调用冻结方法可能会有所帮助。看到 从BackgroundWorker线程更新Image UI属性     

要回复问题请先登录注册