Sonntag, 29. August 2010

Silverlight: Increase performance for NotifyPropertyChanged

Last week I met Jeff Wilcox from Microsoft in my new Windows Phone 7 project for a big Telephone company in Germany. He gave us the following tip for improving performance of Silverlight applications concerning the often using NotifyPropertyChanged method. The tip is to store the event arguments in a dictionary to avoid the recreation every time.

 

   1: private static Dictionary<string, PropertyChangedEventArgs> _argumentInstances = new Dictionary<string, PropertyChangedEventArgs>(); 
   2:        
   3:        protected void NotifyPropertyChanged(String propertyName)
   4:        {
   5:            PropertyChangedEventHandler handler = PropertyChanged;
   6:  
   7:            if (null != handler)
   8:            {
   9:                PropertyChangedEventArgs args;
  10:  
  11:                if (!_argumentInstances.TryGetValue(propertyName, out args))
  12:                {
  13:                    args = new PropertyChangedEventArgs(propertyName);
  14:                    _argumentInstances[propertyName] = args;
  15:                }
  16:                
  17:                handler(this, args);
  18:            }
  19:        }

Keine Kommentare:

Kommentar veröffentlichen