[英]How to save application settings?
We have a UWP for Windows IoT Core, we need to save some settings, but these settings have to live even if the application is stopped or the IoT device restarted. 我们有一个用于Windows IoT核心的UWP,我们需要保存一些设置,但即使应用程序停止或IoT设备重新启动,这些设置也必须存在。
The code I have is the following, it works perfectly fine when the app is open, and if I switch between XAML pages, but doesn't work when the application is stopped, it's like the variable never existed. 我的代码如下,当应用程序打开时,它完全正常,如果我在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";
}
}
If you want to store your settings locally you can store them as single items or as a ApplicationDataCompositeValue
(to keep all values as a single entity) like you did. 如果要在本地存储设置,可以将它们存储为单个项目或作为ApplicationDataCompositeValue
(将所有值保存为单个实体),就像您一样。 Just put the composite (or single items) in the ApplicationData.Current.LocalSettings
container. 只需将复合(或单个项)放在ApplicationData.Current.LocalSettings
容器中。 Below a small piece of code that you can simply copy paste in an empty app and attach to 2 buttons to try out. 在一小段代码下面,您可以简单地在空应用程序中复制粘贴并附加到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"];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.