简体   繁体   中英

storing and retrieving settings in c#

Dont know what the problem is wether Value is not being stored or not being retrieved from isolated settings

Page1.Xaml

Here I am storing data

    public void Stop_Click(object sender, RoutedEventArgs e)
    {
        PhoneApplicationService.Current.State["high"] = count;
    }

Here I want to retrive it!

Page2.Xaml

    private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
    {

        TP.Text = (string)PhoneApplicationService.Current.State["high"];   
    }

Store your data like this :

    public void Stop_Click(object sender, RoutedEventArgs e)
    {
        var settings = IsolatedStorageSettings.ApplicationSettings;
        if (!settings.Contains("high"))
        {
            settings.Add("high", count);
        }
        else
        {
            settings["high"] = count;
        }

        settings.Save();
    }

And then retrieve stored settings data like this :

    private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
    {
        var settings = IsolatedStorageSettings.ApplicationSettings;
        if (settings.Contains("high"))
        {
            TP.Text = settings["high"].ToString();
        }
    }

Hope this helps.

This type of saving data is only for "multitasking" purposes such as when user leaves your app but does not close it by back button. Also system can kill the app if it is in background and user opened another apps (I think the limit is 8 apps in background).

Your approach should work fine when app is not closed and then resumed by multitasking menu for example.

If you want to store data for long-term then use IsolatedStorageSettings as "Mak" answered.

You can find more info on MSDN - http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff817008%28v=vs.105%29.aspx

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