This Quickstart walks you through implementing the Settings contract by using XAML and the ApplicationSettings class.
Prerequisites
Read the Guidelines for app settings.
Introduction
The following example shows how to define a setting for a Windows Store app. You create a settings Flyout control that's hosted in a Popup control. The settings Flyout is associated with a SettingsCommand that's added to an ApplicationCommands collection.
Create the project
- Open Microsoft Visual Studio 2012. On the File menu, select New Project. The New Project dialog box appears.
- In the Templates pane, open Visual C# and click Windows Store.
- From the center pane, select Blank App (XAML).
- In the Name text box, type "SettingsExample".
- Click OK. The SettingsExample solution is created.
Define the Settings Flyout
Add a new UserControl named SimpleSettingsNarrow to the project. Open SimpleSettingsNarrow.xaml and replace the automatically generated XAML with the following XAML.
<UserControl x:Class="SettingsExample.SimpleSettingsNarrow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:SettingsExample" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="346"> <UserControl.Resources> <Style x:Key="SettingsBackButtonStyle" TargetType="Button"> <Setter Property="MinWidth" Value="0"/> <Setter Property="FontFamily" Value="Segoe UI Symbol"/> <Setter Property="FontWeight" Value="Normal"/> <Setter Property="FontSize" Value="26.66667"/> <Setter Property="AutomationProperties.AutomationId" Value="BackButton"/> <Setter Property="AutomationProperties.Name" Value="Back"/> <Setter Property="AutomationProperties.ItemType" Value="Navigation Button"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Grid x:Name="RootGrid" Width="30" Height="30"> <Grid Margin="-6,-6,0,0"> <TextBlock x:Name="BackgroundGlyph" Text="" Foreground="{StaticResource BackButtonBackgroundBrush}"/> <TextBlock x:Name="NormalGlyph" Text="{StaticResource BackButtonSnappedGlyph}" Foreground="{StaticResource BackButtonGlyphBrush}"/> <TextBlock x:Name="ArrowGlyph" Text="" Foreground="{StaticResource BackButtonPressedGlyphBrush}" Opacity="0"/> </Grid> <Rectangle x:Name="FocusVisualWhite" IsHitTestVisible="False" Stroke="{StaticResource FocusVisualWhiteStrokeBrush}" StrokeEndLineCap="Square" StrokeDashArray="1,1" Opacity="0" StrokeDashOffset="1.5"/> <Rectangle x:Name="FocusVisualBlack" IsHitTestVisible="False" Stroke="{StaticResource FocusVisualBlackStrokeBrush}" StrokeEndLineCap="Square" StrokeDashArray="1,1" Opacity="0" StrokeDashOffset="0.5"/> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Normal" /> <VisualState x:Name="PointerOver"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGlyph" Storyboard.TargetProperty="Foreground"> <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource BackButtonHoverBackgroundBrush}"/> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="NormalGlyph" Storyboard.TargetProperty="Foreground"> <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource BackButtonHoverGlyphBrush}"/> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState x:Name="Pressed"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGlyph" Storyboard.TargetProperty="Foreground"> <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource BackButtonGlyphBrush}"/> </ObjectAnimationUsingKeyFrames> <DoubleAnimation Storyboard.TargetName="ArrowGlyph" Storyboard.TargetProperty="Opacity" To="1" Duration="0"/> <DoubleAnimation Storyboard.TargetName="NormalGlyph" Storyboard.TargetProperty="Opacity" To="0" Duration="0"/> </Storyboard> </VisualState> <VisualState x:Name="Disabled"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="Visibility"> <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> </VisualStateGroup> <VisualStateGroup x:Name="FocusStates"> <VisualState x:Name="Focused"> <Storyboard> <DoubleAnimation Storyboard.TargetName="FocusVisualWhite" Storyboard.TargetProperty="Opacity" To="1" Duration="0"/> <DoubleAnimation Storyboard.TargetName="FocusVisualBlack" Storyboard.TargetProperty="Opacity" To="1" Duration="0"/> </Storyboard> </VisualState> <VisualState x:Name="Unfocused" /> <VisualState x:Name="PointerFocused" /> </VisualStateGroup> </VisualStateManager.VisualStateGroups> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </UserControl.Resources> <Border BorderBrush="Black" BorderThickness="1,0,0,0"> <Grid Background="White" VerticalAlignment="Stretch"> <!-- Root grid definition --> <Grid.RowDefinitions> <RowDefinition Height="80" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <!-- Header area for panel --> <Grid Background="Orange" Grid.Row="0"> <Grid Margin="40,32,17,13"> <Grid.Transitions> <TransitionCollection> <EntranceThemeTransition FromHorizontalOffset="50" /> </TransitionCollection> </Grid.Transitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="30" /> <ColumnDefinition Width="*" /> <ColumnDefinition /> </Grid.ColumnDefinitions> <Button Click="MySettingsBackClicked" Margin="0,3,0,0" Grid.Column="0" Style="{StaticResource SettingsBackButtonStyle}" HorizontalAlignment="Left" /> <TextBlock Margin="10,0,0,0" Grid.Column="1" FontFamily="Segoe UI" FontWeight="SemiLight" FontSize="24.6667" Text="Sound Options" HorizontalAlignment="Left" /> <Image Source="Assets/SmallLogo.png" HorizontalAlignment="Right" Grid.Column="2" Margin="0,0,6,0" /> </Grid> </Grid> <!-- Settings Panel Content --> <Grid Grid.Row="1" Margin="40,24,23,0" VerticalAlignment="Top"> <Grid.Transitions> <TransitionCollection> <EntranceThemeTransition FromHorizontalOffset="120" /> </TransitionCollection> </Grid.Transitions> <ToggleSwitch Header="Sound Effects" /> </Grid> </Grid> </Border> </UserControl>
Define the Settings Flyout code-behind
Open SimpleSettingsNarrow.cs and replace the automatically generated code with the following code.
using System; using System.Collections.Generic; using System.IO; using System.Linq; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI.ApplicationSettings; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; namespace SettingsExample { public sealed partial class SimpleSettingsNarrow : UserControl { public SimpleSettingsNarrow() { this.InitializeComponent(); } private void MySettingsBackClicked(object sender, RoutedEventArgs e) { if (this.Parent.GetType() == typeof(Popup)) { ((Popup)this.Parent).IsOpen = false; } SettingsPane.Show(); } } }
Assign the settings Flyout to the app commands collection
Open BlankPage.cs and replace the automatically generated code with the following code.
using System; using System.Collections.Generic; using System.IO; using System.Linq; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI.ApplicationSettings; using Windows.UI.ViewManagement; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media.Animation; using Windows.UI.Xaml.Navigation; // The Blank Page item template is documented at http://go.microsoft.com/fwlink/p/?linkid=234238 namespace SettingsExample { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> public sealed partial class BlankPage : Page { Rect _windowBounds; double _settingsWidth = 346; Popup _settingsPopup; public BlankPage() { this.InitializeComponent(); _windowBounds = Window.Current.Bounds; Window.Current.SizeChanged += OnWindowSizeChanged; SettingsPane.GetForCurrentView().CommandsRequested += BlankPage_CommandsRequested; } void OnWindowSizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e) { _windowBounds = Window.Current.Bounds; } void BlankPage_CommandsRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args) { SettingsCommand cmd = new SettingsCommand("sample", "Sound Options", (x) => { _settingsPopup = new Popup(); _settingsPopup.Closed += OnPopupClosed; Window.Current.Activated += OnWindowActivated; _settingsPopup.IsLightDismissEnabled = true; _settingsPopup.Width = _settingsWidth; _settingsPopup.Height = _windowBounds.Height; SimpleSettingsNarrow mypane = new SimpleSettingsNarrow(); mypane.Width = _settingsWidth; mypane.Height = _windowBounds.Height; _settingsPopup.Child = mypane; _settingsPopup.SetValue(Canvas.LeftProperty, _windowBounds.Width - _settingsWidth); _settingsPopup.SetValue(Canvas.TopProperty, 0); _settingsPopup.IsOpen = true; }); args.Request.ApplicationCommands.Add(cmd); } private void OnWindowActivated(object sender, Windows.UI.Core.WindowActivatedEventArgs e) { if (e.WindowActivationState == Windows.UI.Core.CoreWindowActivationState.Deactivated) { _settingsPopup.IsOpen = false; } } void OnPopupClosed(object sender, object e) { Window.Current.Activated -= OnWindowActivated; } /// <summary> /// Invoked when this page is about to be displayed in a Frame. /// </summary> /// <param name="e">Event data that describes how this page was reached. The Parameter /// property is typically used to configure the page.</param> protected override void OnNavigatedTo(NavigationEventArgs e) { } private void Button_Click_1(object sender, RoutedEventArgs e) { SettingsPane.Show(); } private void HandleSizeChange(object sender, RoutedEventArgs e) { RadioButton rb = sender as RadioButton; _settingsWidth = Convert.ToInt32(rb.Content); } } }
Invoke the settings Flyout
Open BlankPage.xaml and replace the automatically generated XAML with the following XAML.
<Page x:Class="SettingsExample.BlankPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:SettingsExample" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="{StaticResource ApplicationPageBackgroundBrush}"> <StackPanel Margin="50,50,0,0"> <Button Content="Show Settings" Click="Button_Click_1" /> <RadioButton GroupName="SettingsWidth" Content="346" IsChecked="True" Checked="HandleSizeChange" /> <RadioButton GroupName="SettingsWidth" Content="646" Checked="HandleSizeChange" /> </StackPanel> </Grid> </Page>
Summary
In this Quickstart, you learned to set up the Settings contract by using XAML.
Related topics
- Samples
- Application settings sample
- Reversi sample feature scenarios: settings flyout
- Quickstart: Using Windows Runtime
- Guidelines for app settings
Build date: 11/29/2012