1

リストボックスの状態を wp に保存しようとしています。このメソッドを使用して、テキストボックスのコンテンツを廃棄しましたが、完全に機能しましたが、文字列のリストに問題があります:

基本的に、ベータと呼ばれる文字列のリストがあり、ボタンをクリックしてリストを生成する必要があります。したがって、アプリケーションを閉じるか非アクティブ化してから再起動すると、ボタンを押さなくてもリストが表示されることを望みます

   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;
            }
   }

しかし、次の場所で問題が発生します:

phoneAppService.State.TryGetValue("MyValue", out List<myValue)>

このメソッドは 1 つの文字列で機能しますが、リストでは機能しません。

文字列のリストにはどのメソッドを使用すればよいですか?

編集 :

アプリが閉じられた、非アクティブ化された、起動された、または開かれたときに呼び出す app.xaml.cs クラスのメソッドは次のとおりです。

 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>;
            }
        }

前に述べたように、このメソッドはテキストボックスに適切に復元された文字列に対しては機能しますが、文字列のリストに対しては機能しません

4

1 に答える 1

1

それを State に追加すると、それbetaはすでに になっているように見えます。List<string>引き出すと、すでに になっているはずなので、TryGetValue 呼び出しでList<string>に変える必要はありません。List<myvalue>次のように、はるかに簡単に見えるはずです。

private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
{
    object myValue;
    if(phoneAppService.State.TryGetValue("_List", out myValue))
    {
        list.ItemsSource  = myValue as List<string>;
    }
}
于 2013-12-24T20:11:12.823 に答える