Welcome to MSDN Blogs Sign in | Join | Help

It's been a while since my last post, so I figured I'd write something I just recently had to work on: user settings.

In many client applications, you quickly run into the need to store application or user settings. There are a few options on how to do that, some of which are more complex than others, and I'll provide links at the end on a other options I found. In this post, I'll describe one of the simpler solutions, which takes advantage of existing functionality in Visual Studio, makes use of data-binding in Xaml, and also allows you to retrieve or set settings through code.

Visual Studio has design-time support for application and user settings, described quite well in the MSDN documentation. The VS designer creates as Settings class and automatically generates properties based on the setting names. Moreover, the settings will work in a ClickOnce application. As for the limitations mentioned in the article, I would add that design-time implies you need to know the user settings ahead of time. Not usually a big problem, but it can be for more complex scenarios. For example, if your application supports plug-ins that have custom settings, you wouldn't necessarily know this ahead of time.

Assuming you’ve already created settings for your application, how could you go about making use of them in a WPF application? In order to access the properties in Xaml, you could add an entry in the application's resource dictionary:

<Application x:Class="SampleApp.App"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:properties="clr-namespace:SampleApp.Properties"
   Exit="OnExit"
   StartupUri="MainWindow.xaml">
     <Application.Resources>
         <properties:Settings x:Key="Settings" />
     </Application.Resources>
</Application>

If you had added a setting named Username as a string type, you could bind a TextBox in Xaml this way:

<TextBox Text="{Binding Source={StaticResource Settings}, Path=Default.Username}" />

As you enter text in the TextBox, the two-way binding automatically updates the Username setting, and similarly at startup, the TextBox will be populated with the most recently saved Username value.

Finally, in order to ensure your settings are properly saved, you could implement the OnExit handler declared above, or alternatively you could put the settings in a dedicated window and save at the appropriate time.

private void OnExit(object sender, ExitEventArgs e)
{
    Properties.Settings.Default.Save();

}

To access the current property value through code:

Properties.Settings.Default[“Username”]

Now, for the additional links I promised. This article offers a more complex and complete solution. Although it’s relatively heavy-weight, it adds support for specific WPF controls, such as saving column positions in a ListView.

One of the questions we frequently get when meeting with customers is whether or not to adopt WPF over WinForms, Win32 or some other technology. As much as I'd love to have the answer always be "of course!", the reality is that it depends on a number of factors that need to be taken in consideration.

This technical article by Peter Faraday and Brad Becker outlines well some of the benefits and tradeoffs. In addition, I would consider the following:

  • Length of Project: Although we've tried to make WPF easy to use, as with any new technology there is a learning curve, which can be problematic if the schedule doesn't leave much room for it or if you can't afford to train developers to use WPF.
  • Project Lifespan: Are you planning on using this project, and its associated technology, for an indefinite period of time? Or is the project near it's end of life, only requiring an incremental improvement?
  • Goals of Project: Are you planning on providing an improved user experience for your application, or are you planning on extending existing functionality provided by your custom control? You could decide to use WPF for the application chrome, yet continue using the existing technology for custom components that would be too expensive to re-write.
  • Scope of WPF Use: Using some of the interop features of WPF, it's possible to make use of other technologies in conjunction with WPF. For example, you can use WinForms in WPF (WinFormsHost), WPF in WinForms (ElementHost), Win32 in WPF (HWndHost) or WPF in Win32 (HwndSource).
  • 3rd Party Components: The application might depend on components that can't be updated, in which case it might not be as easy to transition to WPF.

In addition, a few additional points have come up:

  • High DPI: WPF applications are DPI aware, meaning WPF is resolution independent and will scale if DPI settings are changed. This blog entry describes this in good detail.
  • System Fonts: This is particularly important for companies that rely on system fonts to provide a consistent experience, as well as companies that have accessibility requirements such as High Contrast. WPF does use system font settings for the default theme, that is unless you override these settings by using a custom style/skin/theme that does not make use of system fonts.
  • Accessibility: WPF has support for the latest UI automation technology (UIA), which works well with the screen reader and magnifier included with the OS. Although UIA does have backwards compatibility with its predecessor (MSAA), many 3rd party screen readers and magnifiers are still MSAA based and don't directly support UIA at this particular time, but some have indicated they are working on adding UIA support going forward.
  • Globalization/Localization: This is possible using WPF. Rob Relyea posted this blog entry on the topic.

If you're passionate about WPF, or even if you're just curious to learn more about WPF, we have a few software engineering positions opened on the test team.

Here's a link to a post by Ivo Manolov, Test Manager for WPF, with a list of positions currently opened: Jobs on the WPF Team

The third major update of WPF has just been released as a Beta, as part of the .Net Framework 3.5 Service Pack 1 Beta. Here's a few links to blogs that have more detail:

Scott’s Blog [VP]

http://weblogs.asp.net/scottgu/archive/2008/05/12/visual-studio-2008-and-net-framework-3-5-service-pack-1-beta.aspx

Soma’s Blog [VP]

http://blogs.msdn.com/somasegar/

Tim Sneath’s Blog [Dev Evangelist]

http://blogs.msdn.com/tims

Greg Schecter’s Blog [Architect]

http://blogs.msdn.com/greg_schechter/archive/2008/05/12/a-series-on-gpu-based-effects-for-wpf.aspx

Jossef Goldberg's Blog [PM] (more detailed breakdown of features per area)

http://blogs.msdn.com/jgoldb/archive/2008/05/15/what-s-new-for-performance-in-wpf-in-net-3-5-sp1.aspx

Vincent Sibal's Blog [SDET]

http://blogs.msdn.com/vinsibal/archive/tags/WPF+3.5+SP1/default.aspx

Channel 9 WPF Intro on SP1

http://channel9.msdn.com

Welcome to my blog!

In the coming weeks, I will be posting a series of posts based on my experiences doing WPF application development. For the time being, the focus will be on client applications, although that may change as I take on new projects.

Over the years, I have had the chance to work on a number of interesting and challenging problems. My title at MS is Software Design Engineer in Test (SDET), and I spent a couple of years working on tools used internally, many of which are still actively used today. Unfortunately, although most of these projects used .Net technologies (Winforms, C#), very few made use of WPF. If only because WPF is a great platform, and partly due to the fact I'm a member of the WPF team, I thought it was important to start focusing on projects that used WPF.

So why yet another blog? As part of my learning experience, I have been spending quite a bit of time reading samples on MSDN, as well as searching the Internet and Forums for answers to my various questions. I found Bea Stollnitz's blog (formerly Bea Costa) to be extremely useful in answering many of my data binding questions, and although I won't pretend mine will be quite as polished and helpful, I figured it couldn't hurt to aspire to it :)

 
Page view tracker