Windows Phone 五、配置存储

基本存储形式

本地设置:ApplicationData.Current.LocalSettings

漫游设置:ApplicationData.Current.RoamingSettings

支持的数据类型:简单类型数据(不支持复合类型)

组合类型:ApplicationDataCompositeValue

1     <StackPanel>
2         <TextBox x:Name="txtKey" Header="KEY"/>
3         <TextBox x:Name="txtValue" Header="VALUE"/>
4         <Button Content="SET" Click="btnSet_Click"/>
5         <Button Content="GET" Click="btnGet_Click"/>
6     </StackPanel>
 1         //操作数据配置,必须先拿到数据容器
 2         ApplicationDataContainer container = ApplicationData.Current.RoamingSettings;//漫游设置
 3         private void btnSet_Click(object sender, RoutedEventArgs e)
 4         {
 5             //Values为数据容器
 6             container.Values.Add(txtKey.Text, txtValue.Text);//将数据放到容器当中
 7             // 不支持复合类型
 8             //container.Values.Add(txtKey.Text, new MyData { Value = txtValue.Text });
 9             //复杂数据用ApplicationDataCompositeValue
10             ApplicationDataCompositeValue values = new ApplicationDataCompositeValue();
11             values["Value"] = txtValue.Text;
12             container.Values.Add(txtKey.Text, values);
13         }
14         private void btnGet_Click(object sender, RoutedEventArgs e)
15         {
16             if (!container.Values.ContainsKey(txtKey.Text))
17             {
18                 txtValue.Text = "没有";
19                 return;
20             }
21             txtValue.Text = container.Values[txtKey.Text].ToString();
22         }
23     }
24     public class MyData
25     {
26         public string Value { get; set; }
27     }
多容器存储

创建存储容器:ApplicationData.Current.LocalSettings.CreateContainer();

参数:

Key:容器唯一标识,可以通过 Key 找到已有容器

ApplicationDataCreateDisposition:容器创建选项

Always:如果存在则使用现有的,不存在创建新的

Existing:必须是基于现有容器

容器的目的:对于数据分文别类,便于批量维护

其他容器操作:ApplicationData.Current.LocalSettings.DeleteContainer();

 1     public sealed partial class MainPage : Page
 2     {
 3         //操作数据配置,必须先拿到数据容器
 4         ApplicationDataContainer container = ApplicationData.Current.RoamingSettings;//漫游设置
 5         ApplicationDataContainer myContainer;//多容器存储
 6         public MainPage()
 7         {
 8             this.InitializeComponent();
 9             this.NavigationCacheMode = NavigationCacheMode.Required;
10         }
11         /// <summary>
12         /// 在此页将要在 Frame 中显示时进行调用。
13         /// </summary>
14         /// <param name="e">描述如何访问此页的事件数据。
15         /// 此参数通常用于配置页。</param>
16         protected override void OnNavigatedTo(NavigationEventArgs e)
17         {
18             // Existing:标识必须要基于一个现有容器基础之上;
19             myContainer = container.CreateContainer("my_container", ApplicationDataCreateDisposition.Always);
20         }
21         private void btnSet_Click(object sender, RoutedEventArgs e)
22         {
23             //Values为数据容器
24             myContainer.Values.Add(txtKey.Text, txtValue.Text);//将数据放到容器当中
25         }
26         private void btnGet_Click(object sender, RoutedEventArgs e)
27         {
28             if (!myContainer.Values.ContainsKey(txtKey.Text))
29             {
30                 txtValue.Text = "没有";
31                 return;
32             }
33             txtValue.Text = myContainer.Values[txtKey.Text].ToString();
34         }
35     }
原文地址:https://www.cnblogs.com/includeling/p/4574261.html