I am trying to save the state of a listbox on wp. I used this this method to tombstone the content of a textbox and it worked perfectly but I am having problems for a list of strings :
Basically I have a list of strings called beta and I have to click on a button to generate the list. So I would like that if I close or deactivate my application and then relaunch it, the list shows up without pressing the button
List<string> beta;
private void b_Click_1(object sender, RoutedEventArgs e)
{
List<string> beta = new List<string>{
"string","string","string",
"string","string","string",
"string", };
list.ItemsSource = beta;
phoneAppService.State["_List"] = beta;
}
private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
{
object myValue;
if(phoneAppService.State.TryGetValue("_List", out List<myValue)> ){
list.ItemsSource = myValue;
}
}
but a problem occurs at :
phoneAppService.State.TryGetValue("MyValue", out List<myValue)>
Although this method works with one string it does not with a list.
Which method should I use for a list of strings?
EDIT :
Here are my methods in my app.xaml.cs class that I call when the app is closed, deactivated, launched or opened :
private void SaveState() {
PhoneApplicationService phoneAppService = PhoneApplicationService.Current;
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings["MyValue"] = phoneAppService.State["MyValue"];
if(settings.Contains("_List")){
settings["_List"] = phoneAppService.State["_List"];
}
}
private void LoadState() {
PhoneApplicationService phoneAppService = PhoneApplicationService.Current;
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
string myValue = "";
if(settings.TryGetValue<string>("MyValue", out myValue )){
phoneAppService.State["MyValue"] = myValue;
}
List<string> myValues;
if (settings.TryGetValue<List<string>>("_List", out myValues))
{
phoneAppService.State["_List"] = myValues as List<string>;
}
}
As I said earlier this method works for a string which is properly restored in a textbox but not for a list of string
It appears that
beta
is already aList<string>
when you add it to State. When you pull it out, it should already be aList<string>
, so you don't need to turn it into aList<myvalue>
in the TryGetValue call. It should look something much more straightforward, like: