|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
New Features
IntroductionAlthough just one among many requirements, the capability of a software to remember user input and reuse that information upon recurring operations is a criterion of quality, which directly influences user experience. In the wild, however, practice shows that user settings are insufficiently supported: Why can't that software remember this? Analyzing the reasons for that, the following items come to mind:
The following describes a component that shows how to easily implement user settings. The following goals were driving its development:
.NET ConfigurationSince CLR 2.0, the .NET Framework offers an extensive configuration module in
The usage scenario determines which type of setting to choose:
The matrix shows that the usage of user settings is preferable in many scenarios! Control over the user settings is gained through inheriting from the class
The 'Default Value' is defined through the property attribute The methods When performing the first
If any of these factors change, the user settings will be stored (and looked for) in a different folder. The method The .NET user settings configuration is capable of storing the values of several <?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup,
System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Itenso.Configuration.WindowSettings.MySettings1"
type="System.Configuration.ClientSettingsSection, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
allowExeDefinition="MachineToLocalUser" requirePermission="false" />
<section name="Itenso.Configuration.WindowSettings.MySettings2"
type="System.Configuration.ClientSettingsSection, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<Itenso.Configuration.WindowSettings.MySettings1>
<setting name="MySetting1" serializeAs="String">
<value>Value1</value>
</setting>
</Itenso.Configuration.WindowSettings.MySettings1>
<Itenso.Configuration.WindowSettings.MySettings2>
<setting name="MySetting2" serializeAs="String">
<value>Value2</value>
</setting>
</Itenso.Configuration.WindowSettings.MySettings2>
</userSettings>
</configuration>
The Client Settings FAQ offers a lot more interesting insights about the .NET configuration. The article Read/Write App.Config File with .NET 2.0 demonstrates the management of application settings. Extended .NET User SettingsTo achieve the desired improvements, a user setting has been separated from
The class The following illustration shows how these extended user settings influence the data flow:
The property For automating the runtime behavior, various derivations of
The class The
A Using the CodeConsole ApplicationSupport for user settings is achieved through the class // ------------------------------------------------------------------------
class Program
{
// ----------------------------------------------------------------------
public void Execute()
{
ApplicationSettings applicationSettings = new ApplicationSettings( "MySettings" );
applicationSettings.Settings.Add(
new FieldSetting( // bind setting to the class field
"StatusCode", // setting name
this, // source component
"statusCode", // field name
-1 ) ); // default value
applicationSettings.Load();
Console.Write( "Please enter a number: " );
statusCode = int.Parse( Console.ReadLine() ); // modifying the field value
applicationSettings.Save();
} // Execute
// ----------------------------------------------------------------------
// members
private int statusCode;
} // class Program
Entering the value 22 leads to the following user configuration: <?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings">
<section name="Itenso.Configuration.ApplicationSettings.MySettings"
type="System.Configuration.ClientSettingsSection, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<Itenso.Configuration.ApplicationSettings.MySettings>
<setting name="StatusCode" serializeAs="String">
<value>22</value>
</setting>
<setting name="UpgradeSettings" serializeAs="String">
<value>False</value>
</setting>
</Itenso.Configuration.ApplicationSettings.MySettings>
</userSettings>
</configuration>
The meaning of the value WinForms ApplicationThe following example shows how to implement custom application or form settings:
Control SettingsHandling user settings of // ------------------------------------------------------------------------
public class MyListBox : ListBox
{
// ----------------------------------------------------------------------
public MyListBox()
{
if ( DesignMode )
{
return;
}
ControlSettings controlSettings = new ControlSettings( this );
controlSettings.Settings.Add(
new PropertySetting( // bind setting to the property
this, // source component
"SelectedIndex" ) ); // property name
} // MyListBox
} // class MyListBox
Form SettingsUser settings of a Windows // ------------------------------------------------------------------------
public partial class MyForm : Form
{
// ----------------------------------------------------------------------
public MyForm()
{
InitializeComponent();
FormSettings formSettings = new FormSettings( this );
} // MyForm
} // class MyForm
DataGridView SettingsPosition (order) and width of // ------------------------------------------------------------------------
public partial class MyForm : Form
{
// ----------------------------------------------------------------------
public MyForm()
{
InitializeComponent();
FormSettings formSettings = new FormSettings( this );
formSettings.Settings.Add( new DataGridViewSetting( myDataGridView ) );
} // MyForm
} // class MyForm
The example Collected SettingsThe following example shows how to store all the // ------------------------------------------------------------------------
public partial class MyForm : Form
{
// ----------------------------------------------------------------------
public MyForm()
{
InitializeComponent();
FormSettings formSettings = new FormSettings( this );
formSettings.CollectingSetting +=
new SettingCollectorCancelEventHandler( FormSettingsCollectingSetting );
formSettings.SettingCollectors.Add( new PropertySettingCollector
( this, typeof( CheckBox ), "Checked" ) );
} // MyForm
// ----------------------------------------------------------------------
private void FormSettingsCollectingSetting
( object sender, SettingCollectorCancelEventArgs e )
{
if ( e.Element == this.myCheckBox ) // exclude this checkbox
{
e.Cancel = true;
}
} // FormSettingsCollectingSetting
} // class MyForm
WPF - Code BehindThe accompanying example shows how to implement custom application or
FrameworkElement SettingsHandling user settings of // ------------------------------------------------------------------------
public class MyListBox : ListBox
{
// ----------------------------------------------------------------------
public MyListBox()
{
if ( DesignerProperties.GetIsInDesignMode( this ) )
{
return;
}
FrameworkElementSettings listBoxSettings = new FrameworkElementSettings( this );
listBoxSettings.Settings.Add(
new DependencyPropertySetting( // bind setting to the dependency-property
this, // source component
SelectedIndexProperty ) ); // dependency-property name
} // MyListBox
} // class MyListBox
In contrast to the WinForm variant, WPF offers an elegant way to bind a Window SettingsThe settings of a // ------------------------------------------------------------------------
public partial class MyWindow : Window
{
// ----------------------------------------------------------------------
public MyWindow()
{
WindowSettings windowSettings = new WindowSettings( this );
} // MyWindow
} // class MyWindow
ListView SettingsPosition (order) and width of // ------------------------------------------------------------------------
public partial class MyWindow : Window
{
// ----------------------------------------------------------------------
public MyWindow()
{
WindowSettings windowSettings = new WindowSettings( this );
windowSettings.Settings.Add( new ListViewSetting( myListView ) );
} // MyWindow
} // class MyWindow
The example Collected SettingsThe following example shows how to store all the // ------------------------------------------------------------------------
public partial class MyWindow : Window
{
// ----------------------------------------------------------------------
public MyWindow()
{
WindowSettings windowSettings = new WindowSettings( this );
windowSettings.CollectingSetting +=
new SettingCollectorCancelEventHandler( WindowSettingsCollectingSetting );
windowSettings.SettingCollectors.Add( new DependencyPropertySettingCollector
( this, CheckBox.IsCheckedProperty ) );
} // MyWindow
// ----------------------------------------------------------------------
private void WindowSettingsCollectingSetting
( object sender, SettingCollectorCancelEventArgs e )
{
if ( e.Element == this.myCheckBox ) // exclude this checkbox
{
e.Cancel = true;
}
} // WindowSettingsCollectingSetting
} // class MyWindow
WPF - XAMLUser settings can be declared in XAML as follows: <Window
x:Class="Itenso.Solutions.Community.ConfigurationWindowsDemo.XamlUserSettingsWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:config="clr-namespace:Itenso.Configuration;
assembly=Itenso.Configuration.Windows"
config:WindowSettings.Settings="XamlWindowSettings">
<StackPanel>
<CheckBox
x:Name="MyOption"
Content="My Option"
config:DependencyPropertySetting.Property=
"{x:Static CheckBox.IsCheckedProperty}" />
</StackPanel>
</Window>
The attribute The attribute <Itenso.Configuration.WindowSettings.XamlWindowSettings>
<setting name="Window.Top" serializeAs="String">
<value>203</value>
</setting>
<setting name="Window.Height" serializeAs="String">
<value>200</value>
</setting>
<setting name="Window.Left" serializeAs="String">
<value>813</value>
</setting>
<setting name="Window.Width" serializeAs="String">
<value>713</value>
</setting>
<setting name="Window.WindowState" serializeAs="String">
<value>Normal</value>
</setting>
<setting name="UpgradeSettings" serializeAs="String">
<value>False</value>
</setting>
<setting name="MyOption.IsChecked" serializeAs="String">
<value>True</value>
</setting>
</Itenso.Configuration.WindowSettings.XamlWindowSettings>
ListView SettingsUsing XAML, the position (order) and width of <Window
x:Class="Itenso.Solutions.Community.ConfigurationWindowsDemo.XamlUserSettingsWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:config="clr-namespace:Itenso.Configuration;
assembly=Itenso.Configuration.Windows"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
config:WindowSettings.Settings="XamlWindowSettings">
<StackPanel>
<ListView
Name="MyListView"
ItemsSource="{Binding MyList}"
config:ListViewSetting.Settings="MyListView">
...
</ListView>
</StackPanel>
</Window>
The attribute Collected SettingsThe following example shows the usage of a <Window
x:Class="Itenso.Solutions.Community.ConfigurationWindowsDemo.XamlUserSettingsWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:config="clr-namespace:Itenso.Configuration;
assembly=Itenso.Configuration.Windows"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
config:WindowSettings.Settings="XamlWindowSettings"
config:WindowSettings.CollectedSetting="{x:Static CheckBox.IsCheckedProperty}">
<StackPanel>
<CheckBox
x:Name="MyOption"
Content="My Option"
config:WindowSettings.ExcludeElement="True" />
</StackPanel>
</Window>
The Points of Interest
History
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||