2011-10-14 15 views

答えて

5

それがインテリセンスには表示されませんが、あなたが使用できるように、これは少し奇妙です:

var dispatcher = myDispatcherObject.Dispatcher; 
if (dispatcher.CheckAccess()) { /* ... */ } 

すべてのUIコンポーネントが継承したようDispatcherObjectから、これはあなたの特定の問題を解決するはずですが、UIスレッドに固有のものではありません。どのディスパッチャでも使用できます。

3

頭に浮かん可能な解決策がある:

if (Dispatcher.Thread.Equals(Thread.CurrentThread)) 
{ 
    Action(); 
} 
else 
{ 
    Dispatcher.Invoke(Action); 
} 
0

Windowsストアアプリを構築している場合、上記の例は機能しません。以下は、の場合の例です。もちろん、必要に応じて変更してください。

/// <summary> 
/// Updates the UI after the albums have been retrieved. This prevents the annoying delay when receiving the albums list. 
/// </summary> 
/// <param name="albums"></param> 
public void UpdateUiAfterAlbumsRetrieved(System.Collections.ObjectModel.ObservableCollection<PhotoAlbum> albums) 
{ 
    if (!Dispatcher.HasThreadAccess) 
    { 
     Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() => 
     { 
      ddlAlbums.DataContext = albums; 
      ddlAlbums.IsEnabled = true; 
      tbxProgress.Text = String.Empty; 
      ProgressBar.IsIndeterminate = false; 
      ProgressBar.Visibility = Windows.UI.Xaml.Visibility.Collapsed; 
     }); 
    } 
    else 
    { 
     ddlAlbums.DataContext = albums; 
     ddlAlbums.IsEnabled = true; 
     tbxProgress.Text = String.Empty; 
     ProgressBar.IsIndeterminate = false; 
    } 
} 
関連する問題