2010-11-17 11 views

उत्तर

3

लग रहा है mvvmlight में सभी सूत्रण भाग की तरह इस वर्ग है:

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; 

    } 
} 

}

और इतना ही है। का प्रयोग करें DispatcherHelper.Initialize() स्थिर अनुप्रयोग निर्माता (WPF) या Application_Startup ईवेंट हैंडलर (Silverlight) में टिप्पणी के अनुसार - और फिर यू DispatcherHelper.CheckBeginInvokeOnUI (कार्रवाई कार्रवाई)

सादर

+0

धन्यवाद agend उपयोग कर सकते हैं। सामान्य .NET थ्रेडिंग से इसकी तुलना कैसे की जाती है और इसका उपयोग व्यूमोडेल में किया जा सकता है? –

+0

1. सबसे पहले आप डिस्पैचरहेल्पर क्लास कॉलिंग प्रारंभ करें() विधि आरंभ करें (और आपको इसे यूई थ्रेड पर करना होगा ताकि यह प्रेषक – agend

+0

पर अपना निजी संदर्भ याद रख सके। 2. इसके बाद आप DispatcherHelper.CheckBeginInvokeOnUI (एक्शन एक्शन) का उपयोग कर सकते हैं - कहीं भी आप चाहते हैं - देखें, मॉडल, व्यूमोडेल -ट हमेशा आपकी क्रिया 3. को सामान्य .NET थ्रेडिंग तुलना के बारे में आमंत्रित करने के लिए ui थ्रेड का उपयोग करेगा - आम तौर पर आपको इन चीजों को अपने आप करना होगा: ui के संदर्भ में रहें प्रेषक, चेक यू यू थ्रेड पर हैं, और अंत में प्रेषक को कॉल करें। बेगिन इनवोक (एक्शन) - इस कक्षा के साथ यह आसान है – agend

संबंधित मुद्दे