访问不同页面中的隔离存储数据

本文关键字:隔离 存储 数据 访问 | 更新日期: 2023-09-27 17:54:44

我创建了一个IsolatedStorageSettings来存储播放器的数据我在SetProfile中创建了它。xaml

在MainPage中。在xaml中,有一个TextBlock包含文本"Hello "

我想这样做:

TextBlock1.Text = "Hello " +(THE NAME OF THE PLAYER);

这就是我需要访问隔离存储的地方我该怎么做呢?

SetProfile.xaml

 IsolatedStorageSettings Profile = IsolatedStorageSettings.ApplicationSettings;
private void create_Click(object sender, RoutedEventArgs e)
       {
                Player player = new Player(); // Player is a class 
                player.FirstName = FirstName.Text;
                player.LastName = LastName.Text;
                player.Age = Convert.ToInt32(Age.Text);
                player.Rank = 1;
                player.RankDescreption = "Beginner";
                if (Profile.Contains("profile"))
                {
                    Profile.Remove("profile");
                    Profile.Add("profile", player);
                    Profile.Save();
                }
                else
                {
                    Profile.Add("profile", player);
                    Profile.Save();
                }
                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
            }

  private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
        {
           if (IsolatedStorageSettings.ApplicationSettings.Contains("profile"))
               NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

MainPage.xaml

private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
    {
        HelloName.Text = "Hello " + (WHAT I NEED) ;
    }

访问不同页面中的隔离存储数据

您可以这样读取设置值:

private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
    {
    if (IsolatedStorageSettings.ApplicationSettings.Contains("profile"))
       {
         Player player =(Player)IsolatedStorageSettings.ApplicationSettings["profile"];         
        HelloName.Text ="Hello"+ player.FirstName;
       }      
    }

点击本文档了解更多设置