Windows Phone 8.1 ApplicationDataContainer 容器

network | | 访问(62)
<StackPanel>
    <Button Content="创建Container" Click="CreateContainer_Click"></Button>
    <Button Content="添加信息" Click="WriteSetting_Click"></Button>
    <Button Content="删除信息" Click="DeleteSetting_Click"></Button>
    <Button Content="删除Container" Click="DeleteContainer_Click"></Button>
    <TextBlock x:Name="OutputTextBlock" TextWrapping="Wrap"></TextBlock>
</StackPanel>
public sealed partial class MainPage: Page {
    ApplicationDataContainer localSettings = null;
    const string containerName = "exampleContainer";
    const string settingName = "exampleSetting";
    public MainPage() {
        this.InitializeComponent();
        localSettings = ApplicationData.Current.LocalSettings;
        DisplayOutput();
    }

    void CreateContainer_Click(Object sender, RoutedEventArgs e) {
        ApplicationDataContainer container = localSettings.CreateContainer(containerName, ApplicationDataCreateDisposition.Always);
        DisplayOutput();
    }

    void DeleteContainer_Click(Object sender, RoutedEventArgs e) {
        localSettings.DeleteContainer(containerName);
        DisplayOutput();
    }

    void WriteSetting_Click(Object sender, RoutedEventArgs e) {
        if (localSettings.Containers.ContainsKey(containerName)) {
            localSettings.Containers[containerName].Values[settingName] = "Hello World";
        }
        DisplayOutput();
    }

    void DeleteSetting_Click(Object sender, RoutedEventArgs e) {
        if (localSettings.Containers.ContainsKey(containerName)) {
            localSettings.Containers[containerName].Values.Remove(settingName);
        }
        DisplayOutput();
    }

    void DisplayOutput() {
        bool hasContainer = localSettings.Containers.ContainsKey(containerName);
        bool hasSetting = hasContainer ? localSettings.Containers[containerName].Values.ContainsKey(settingName) : false;
        String output = String.Format("Container Exists: {0}\n" + "Setting Exists: {1}", hasContainer ? "true": "false", hasSetting ? "true": "false");
        OutputTextBlock.Text = output;
    }

    protected override void OnNavigatedTo(NavigationEventArgs e) {}
}