Pergunta

I have a "setting page" which will save a value from the user for further use. If the user set this value then this value will be used into main page. In main page first it check if the user set or change the previous saved value or not.If it is changed then it uses the current value otherwise it will use the previous settled value. I used Application.Current.Resources. But i found that if my app restart again the value is lost. What i need is that from the setting page a value named noOfWordsForLearning(for example) will be saved so that anytime it can be accessed from anypage.If the value is changed anytime the other page can get the changed value. I tried like this. In setting page:

 Application.Current.Resources.Add("savedNoOfWords",noOfWordsForLearning);

In main page:

 if (Application.Current.Resources.Contains("savedNoOfWords"))
        {
            getSavedValueFromSetting = (int)Application.Current.Resources["savedNoOfWords"];
            MessageBox.Show("no of saved words is " + getSavedValueFromSetting);
        }
        else
            MessageBox.Show("no of default words is "+getSavedValueFromSetting);

How can i do that??

Foi útil?

Solução

I dont know if there any performance issue or other concern, but if you want to avoid reading & writing to IsolatedStorage too often, this is what i will do:

//In App.xaml.cs
//Point no 1
private void Application_Launching(object sender, LaunchingEventArgs e)
{
    object noOfWordsForLearning;
    IsolatedStorageSettings.ApplicationSettings.TryGetValue("savedNoOfWords", out noOfWordsForLearning);
    if (noOfWordsForLearning != null)
    {
         Application.Current.Resources["savedNoOfWords"] = noOfWordsForLearning;
    }
}

//Point no 2
private void Application_Closing(object sender, ClosingEventArgs e)
{
    IsolatedStorageSettings.ApplicationSettings["savedNoOfWords"] = noOfWordsForLearning;
}
  1. Load values from IsoStorage to Application.Current.Resources when application launched
  2. Store setting values from Application.Current.Resources to IsoStorage just before application closed
  3. During application run, only read and write settings to Application.Current.Resources

Another advantage for you following these steps is.. your existing codes (which only read and save to Application.Current.Resources) will remain unchanged. You just need to add codes to do point 1 & 2. I hope this help, cheers.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top