2016-11-01 123 views
1

我们有一个适用于Windows IoT Core的UWP,我们需要保存一些设置,但即使应用程序已停止或物联网设备重新启动,这些设置也必须保留。如何保存应用程序设置?

我拥有的代码如下,它在应用程序打开时工作得很好,如果我在XAML页面之间切换,但在应用程序停止时不起作用,就像变量从不存在一样。

static class Global 
{ 
    public static Windows.Storage.ApplicationDataContainer localSettings { get; set; } 
    public static Windows.Storage.StorageFolder localFolder { get; set; } 
} 

private void Btn_Inciar_Config_Click(object sender, RoutedEventArgs e) 
{ 
    if (TxtDeviceKey.Text != String.Empty || TxtDeviceName.Text != String.Empty || Txt_Humedad_Config.Text != String.Empty || Txt_Intervalo_Config.Text != String.Empty || Txt_Temperatura_Ambiente_Config.Text != String.Empty || Txt_Temperaura_Config.Text != String.Empty) 
    { 
     Windows.Storage.ApplicationDataCompositeValue composite = 
     new Windows.Storage.ApplicationDataCompositeValue(); 
     composite["GlobalDeviceKey"] = TxtDeviceKey.Text; 
     composite["GlobalDeviceName"] = TxtDeviceName.Text; 
     composite["GlobalTemperature"] = Txt_Temperaura_Config.Text; 
     composite["GlobalHumidity"] = Txt_Humedad_Config.Text; 
     composite["GlobalTemperatureRoom"] = Txt_Temperatura_Ambiente_Config.Text; 
     composite["GlobalInterval"] = Txt_Intervalo_Config.Text; 

     localSettings.Values["ConfigDevice"] = composite; 
     Lbl_Error.Text = ""; 
     Frame.Navigate(typeof(MainPage)); 
    } 
    else 
    { 
     Lbl_Error.Text = "Ingrese todos los campos de configuracion"; 
    } 
} 

回答

3

如果你想保存你的设置在本地,你可以将它们存储为单个项目或作为ApplicationDataCompositeValue(保留所有的值作为单一实体)像你这样。只需将复合物(或单个物品)放入ApplicationData.Current.LocalSettings容器中即可。在一小段代码下面,您可以简单地将粘贴复制到一个空白的应用程序中,并附加到2个按钮来尝试。

private void SaveClicked(object sender, RoutedEventArgs e) 
{ 
    Windows.Storage.ApplicationDataCompositeValue composite = 
     new Windows.Storage.ApplicationDataCompositeValue(); 
    composite["GlobalDeviceKey"] = "Key"; 
    composite["GlobalDeviceName"] = "Name"; 
    ApplicationData.Current.LocalSettings.Values["ConfigDevice"] = composite; 
} 

private void LoadClicked(object sender, RoutedEventArgs e) 
{ 
    Windows.Storage.ApplicationDataCompositeValue composite = 
     (ApplicationDataCompositeValue) ApplicationData.Current.LocalSettings.Values["ConfigDevice"]; 
    var key = (string)composite["GlobalDeviceKey"]; 
} 
相关问题