简体   繁体   中英

Storing Variables in UWP C#

I've been writing Universal Windows Apps for over a month now, and I wanted to make sure that I was storing variables I want to use throughout the entire app in the appropriate place. What I have been doing is placing variables in App.xaml.cs and creating an App variable that references the current app as follows

private App obj = (App)App.Current;

I am then able to grab the variables that I sotre in App.xaml.cs and use it when needed. Is this a correct way of storing information or is there a better way of storing these variables?

Per your description, I think what you need is ApplicationData.LocalSettings , take a look at Store and retrieve settings and other app data .

Code will be like the following

Create a setting

Windows.Storage.ApplicationDataCompositeValue composite = 
    new Windows.Storage.ApplicationDataCompositeValue();
composite["intVal"] = 1;
composite["strVal"] = "string";

localSettings.Values["exampleCompositeSetting"] = composite;

Retrieve a setting

Windows.Storage.ApplicationDataCompositeValue composite = 
   (Windows.Storage.ApplicationDataCompositeValue)localSettings.Values["exampleCompositeSetting"];

if (composite == null)
{
   // No data
}
else
{
   // Access data in composite["intVal"] and composite["strVal"]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM