2011-06-13 26 views
5

Ben kullanacağım bu recomendations çalıştı: http://msdn.microsoft.com/en-us/library/ms741870.aspx ("Bir Arkaplan Konu ile Engelleme Operasyonu Handling").Nasıl Dispatcher ile Image.Source özelliğini ayarlamak için?

Bu benim kodudur: "sebebiyle farklı bir iş parçacığı sahibi olan çünkü bu nesneyi erişemez çağıran evreyi"

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 
     } 
    } 

o InvalidOperationException atar işaretli satırda. Ama bu satırı kaldırırsam uygulama çalışır.

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 özelliği ile Button.IsEnabled ve Button.Content özellikleri arasındaki fark nedir? Ne yapabilirim?

cevap

10

Özel öneri:thresholdImage arka plan iş parçacığı üzerinde oluşturuluyor. Bunu UI iş parçacığı üzerinde oluşturun veya oluşturulduktan sonra dondurun.

Genel öneri: fark ImageSource bir DependencyObject yüzden iplik afinitesi vardır olmasıdır. Bu nedenle, (yani. UI iş parçacığı) atamadan ediyoruz hangi Image aynı iş parçacığı üzerinde ImageSource oluşturmanız gerekir, yoksa bu şekilde erişmek için herhangi bir iş parçacığı izin oluşturduktan sonra size ImageSourceFreeze() gerekir o. thresholdedImage arka plan iş parçacığı oluşturulan ve UI iş parçacığı üzerinde kullanıldığı için

+0

teşekkür ederiz. Şimdi çalışıyor ve şimdi nokta aldım –

İlgili konular