Windows Phone 8.1 ApplicationDataContainer ApplicationDataCompositeValue 复合设置

network | | 访问(47)
<StackPanel>
    <Button Content="创建" Click="WriteCompositeSetting_Click"></Button>
    <Button Content="删除" Click="DeleteCompositeSetting_Click"></Button>
    <TextBlock x:Name="OutputTextBlock" TextWrapping="Wrap"></TextBlock>
</StackPanel>
public sealed partial class MainPage: Page {
    ApplicationDataContainer roamingSettings = null;
    const string settingName = "exampleCompositeSetting";
    const string settingName1 = "one";
    const string settingName2 = "two";
    public MainPage() {
        this.InitializeComponent();
        roamingSettings = ApplicationData.Current.RoamingSettings;
        DisplayOutput();
    }

    void WriteCompositeSetting_Click(Object sender, RoutedEventArgs e) {
        ApplicationDataCompositeValue composite = new ApplicationDataCompositeValue();
        composite[settingName1] = 1;
        composite[settingName2] = "world";
        roamingSettings.Values[settingName] = composite;
        DisplayOutput();
    }

    void DeleteCompositeSetting_Click(Object sender, RoutedEventArgs e) {
        roamingSettings.Values.Remove(settingName);
        DisplayOutput();
    }

    void DisplayOutput() {
        ApplicationDataCompositeValue composite = (ApplicationDataCompositeValue) roamingSettings.Values[settingName];
        String output;
        if (composite == null) {
            output = "复合设置信息为空";
        } else {
            output = String.Format("复合设置: {{{0} = {1}, {2} = \"{3}\"}}", settingName1, composite[settingName1], settingName2, composite[settingName2]);
        }
        OutputTextBlock.Text = output;
    }
}