The internet has so much to offer on any topic and there’s
so much to learn; but where to start? I realized many years ago, when I first
began to be intrigued by the Windows
Presentation Foundation (WPF), that short write-ups help you gain incremental
knowledge, which when combined can offer solutions to the bigger picture. This
article offers a list of tips that I learned around a year ago, while
scavenging for simple solutions to big problems.
- Use the Visibility.Collapsed Enumeration vs. Visibility.Hidden
The Collapsed value ensures that the element does not
participate in the layout and gives it a height and width of zero. The latter causes
the element to continue to participate in layout. -
Reduce CPU Consumption for WPF Animations
As you know, WPF draws animations at 60 frames per
second. You can reduce this to a lower optimal rate, resulting in less CPU usage. Use Timeline.DesiredFrameRateProperty to change the default value; set it to a
lower value like 15, and then change it according to the smoothness that you
desire.Timeline.DesiredFrameRateProperty.OverrideMetadata( typeof(Timeline), new FrameworkPropertyMetadata { DefaultValue = 15 } );
-
Default the Culture
of the Application to the Culture of Your Client’s MachineWPF elements expose a language property that can be
used to assign languages as per any culture. When this property is applied to a
Window, it applies to all the elements that it contains. Look at the sample
code that applies the language to the current culture. The Startup function in
the App class is an ideal place to apply this change across the application.protected override void OnStartup(StartupEventArgs e) { FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(System.Globalization.CultureInfo.CurrentCulture.IetfLanguageTag))); base.OnStartup(e); }
-
Use StaticResource vs. DynamicResources
StaticResources are evaluated only
once and only during the object. Dynamic resources cause evaluation each time
they are requested by the control, and hence make the application perform
slower. Although this is a deferred runtime lookup, it makes sense if the
resource is not going to change -
Use WPF Performance Suite to Profile Your WPF Applications
WPF Performance Suite
is available in the Windows SDK and consists of few important profiling tools
that allow you to analyze the applications and provide information on the
optimizations that can be applied. You can download it from the Microsoft’s
web site.The Perforator and Visual Profiler profiling tools
give you a lot of information and also indicate potential bottlenecks in the
application. This suite is part of the Microsoft Windows SDK for Windows 7 and
.NET Framework 3.5 SP1. -
Avoid Designer Errors
with the Help of DesignerProperties.GetIsInDesignMode MethodMost often designers break with the “Could not create
an instance” error. Although this is due to some unhandled exception occurring
in the constructor of the control, it may not require running the code in the
design time.public SampleControlConstructor() { InitializeComponent(); if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) { //place your code here. } }
-
Use
IsMouseDirectlyOver Event to Determine the Exact Mouse Movement.The IsMouseOver event would respond to the mouse
movement within a control or its children.Use the IsMouseDirectlyOver event to determine if the
mouse movement is on the control and not its containers. This is useful to
build specific triggers that change elements on mouse movement.
I
hope these help you save some precious programming time. I will be back very
soon with some more tricks. Enjoy!