Using UncommonField <T> in WPF

I just confused WindowsBase.dll >> System.Windows.UncommonField<T> and I thought about using this class ... For example. it is used in the Button class:

 public class Button : ButtonBase { private static readonly UncommonField<KeyboardFocusChangedEventHandler> FocusChangedEventHandlerField = new UncommonField<KeyboardFocusChangedEventHandler>(); } 

So what is the use of this “wrapper”?

+7
c # wpf
source share
1 answer

Short answer

Used to reduce memory usage.

Longer answer

Start with dependency properties. In each DependencyObject you can define a large number of dependency properties. Whether it is a "local" DependencyProperty , for example TextBox.Text or attached, for example Grid.Row , most of them are never installed and only save the default value. To prevent each DependencyObject instance from occupying kilobytes of memory, storing the value for each defined dependency property, only values ​​other than the default values ​​are stored inside the instance.

Now meet the inner class UncommonField<T> . You can view it as a lightweight DependencyProperty without notice of metadata, enforcement or property changes. However, it uses the same mechanism as the actual DependencyProperty with respect to its value: it must be different from the default value that must be stored inside DependencyObject . Since the KeyboardFocusChanged event is rarely used (explaining an unusual adjective), it makes sense to save a little memory here.

But you're probably not Microsoft creating a framework that needs to take into account thousands of DependencyObject instances and optimize them for multiple bytes. Just replace static UncommonField<T> someField instance of T someField .

+7
source share

All Articles