2010-11-17 20 views
5

MVVM Işık Threading bölümünü nasıl kullanılacağı hakkında orada herhangi örnekler var mı? Normal .net iş parçacığı üzerinde MVVMLight.threading kullanmanın avantajı nedir?MVVM Işık Threading Örnek

public static class DispatcherHelper 
{ 

    public static Dispatcher UIDispatcher 
    { 
     get; 
     private set; 
    } 

    /// <summary> 
    /// Executes an action on the UI thread. If this method is called 
    /// from the UI thread, the action is executed immendiately. If the 
    /// method is called from another thread, the action will be enqueued 
    /// on the UI thread's dispatcher and executed asynchronously. 
    /// <para>For additional operations on the UI thread, you can get a 
    /// reference to the UI thread's dispatcher thanks to the property 
    /// <see cref="UIDispatcher" /></para>. 
    /// </summary> 
    /// <param name="action">The action that will be executed on the UI 
    /// thread.</param> 
    public static void CheckBeginInvokeOnUI(Action action) 
    { 
     if (UIDispatcher.CheckAccess()) 
     { 
      action(); 
     } 
     else 
     { 
      UIDispatcher.BeginInvoke(action); 
     } 
    } 

    /// <summary> 
    /// This method should be called once on the UI thread to ensure that 
    /// the <see cref="UIDispatcher" /> property is initialized. 
    /// <para>In a Silverlight application, call this method in the 
    /// Application_Startup event handler, after the MainPage is constructed.</para> 
    /// <para>In WPF, call this method on the static App() constructor.</para> 
    /// </summary> 
    public static void Initialize() 
    { 
     if (UIDispatcher != null) 
     { 
      return; 
     } 

     // for silverlight 
     UIDispatcher = Deployment.Current.Dispatcher; 

     // wpf 
     //IDispatcher = Dispatcher.CurrentDispatcher; 

    } 
} 

}

ve hepsi bu: mvvmlight tüm diş kısmı işte bu sınıf gibi

cevap

3

görünüyor. Statik Uygulama yapıcısı (wpf) ya da Application_Startup olay işleyicisi (Silverlight) 'de yorum yapmak uygun DispatcherHelper.Initialize() kullanın - ve sonra u DispatcherHelper.CheckBeginInvokeOnUI (Eylem eylem)

Selamlar

+0

Teşekkür agend kullanabilirsiniz. Normal .net iş parçacığıyla nasıl karşılaştırılır ve görünüm modellerinde kullanılabilir mi? –

+0

1. İlk u DispatcherHelper sınıf Başlat'ı çağırmadan() metodu başlatmak - ve hatırlamak böylece u ui iş parçacığı üzerinde bunu yapmak gerekir/u DispatcherHelper.CheckBeginInvokeOnUI (Eylem eylem) kullanabilir sonra – agend

+0

2. Hareket memuruyla kendi özel referans set - ui başvurusunu tutmak: u istemek yerden - normalde u kendi başınıza bunları yapmak zorunda kalacak - görünümü, model ViewModel -çok daima eylemi, normal .net parçacığı karşılaştırma hakkında 3. çağırmak için ui iş parçacığı kullanacak dağıtıcı, ui iş parçacığı üzerinde denetleyin ve sonunda dağıtıcıyı arayın.BeginInvoke (eylem) - bu sınıf ile daha kolay – agend