Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

保存Windows Phone应用程序的首选项

From Wiki
Jump to: navigation, search
WP Metro Icon File.png
SignpostIcon XAML 40.png
SignpostIcon WP7 70px.png
Article Metadata

兼容于
文章
翻译:
最后由 hamishwillee 在 17 Jul 2013 编辑

这篇文章展示了如何在Windows Phone 7设备上保存和加载应用程序首选项。

Contents

简介

在大多数的应用程序中,保存用户首选项是常见的任务。普通的情况是-就像团购网那样保存顾客群体,对游戏中的最高分进行存档,保存顾客在游戏中首选的游戏声音片段。这篇文章对用户的首选项进行存档的流程进行讨论。

IsolatedStorageSettings类涉及到我们的操作,可以用来保存和加载用户的这些首选项。这个类帮助我们保存键值对。备重启来IsolatedStorageSettings中的数据不会丢失。那些设置是保存在应用程序的独立存储空间的,因此和其他应用程序是相对独立的。所以,我们可以说,它们是安全的。

示例应用

在下面,我们创建了一个简单的应用程序来保存用户在游戏中的偏爱。游戏音乐,定时模式和难易程度,这三个要素是一个用户对游戏的设定。由于游戏音乐和定时模式基本上是布尔值,所以在应用程序的UI,我们使用ToggleSwitch。ToggleSwitch并不是伴随着Windows Phone SDK一起的,因此要从网站上下载和安装。在这里有详细的描述。这里有丰富的有关的Windows Phone控件的资源。让我们在 XAML的设置页面创建用户界面。

<phone:PhoneApplicationPage 
x:Class="Pref.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:toolbox="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
 
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
 
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="MY GAME" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="Settings" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
 
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
 
<toolbox:ToggleSwitch Name="gameMusic" Header="Game Music"></toolbox:ToggleSwitch>
<toolbox:ToggleSwitch Name="timed" Header="Timed Mode" Margin="6,119,6,371"></toolbox:ToggleSwitch>
 
<Slider Height="84" HorizontalAlignment="Left" Margin="12,310,0,0" Name="slider1" VerticalAlignment="Top" Width="418" Value="1" Minimum="1" />
<TextBlock Height="30" HorizontalAlignment="Left" Margin="12,274,0,0" Name="Difficulty" Text="Difficulty Level" VerticalAlignment="Top" Width="135" />
</Grid>
</Grid>
 
<!--Sample code showing usage of ApplicationBar-->
<!--<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="MenuItem 1"/>
<shell:ApplicationBarMenuItem Text="MenuItem 2"/>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>-->
 
</phone:PhoneApplicationPage>

上面的代码创建的UI界面样式如下图所示. Application UI

后台代码

UI上有一个保存和加载按钮,在用户浏览页面时进行保存和加载比较简单。因此在MainPage.xaml.cs类中,我们重写了OnNavigatedTo()OnNavigatedFrom()方法。

namespace Pref
{
public partial class MainPage : PhoneApplicationPage
{
private IsolatedStorageSettings settings;
// Constructor
public MainPage()
{
InitializeComponent();
settings = IsolatedStorageSettings.ApplicationSettings;
}
 
protected override void OnNavigatedTo(NavigationEventArgs e)
{
System.Diagnostics.Debug.WriteLine("into the app");
try
{
System.Diagnostics.Debug.WriteLine("Retrieving values");
gameMusic.IsChecked = (bool)settings["gamemusic"];
timed.IsChecked = (bool)settings["timed"];
slider1.Value = ( Int16)settings["diff"];
 
}
catch(KeyNotFoundException ex)
{
System.Diagnostics.Debug.WriteLine("First Time using the app");
settings.Add("timed", false);
settings.Add("gamemusic", false);
settings.Add("diff", 1);
settings.Save();
 
}
 
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
System.Diagnostics.Debug.WriteLine("Exiting, so save now");
settings["timed"] = timed.IsChecked;
settings["diff"] = (Int16) slider1.Value;
settings["gamemusic"] = gameMusic.IsChecked;
settings.Save();
}
}
}

你可以看到,我可以以键值对的形式来访问IsolatedStorageSettings中的值。记住在检索和保存文件时要先重置值。同时也需要调用Save()方法来保存或重置这些值。如果一个键值在IsolatedStorageSettings是不存在的,那么它可能会引发KeyNotFoundException事件。

总结

这篇文章描述了在应用开发中的一个基本任务-保存和加载用户的首选项。这可以扩展到许其它地方-如对于游戏中最高分的存储。

This page was last modified on 17 July 2013, at 10:51.
133 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×