2014-02-22 16 views
-1

即使我在Windows Phone应用程序中重新启动应用程序,我如何保存列表框项目。我希望它们能够以某种方式保存在一个文件中,然后在应用程序的下一次开始时读取它们。请帮助.. 好我与代码更新:即使重新启动应用程序,我如何保存列表框项目?

公共部分类的MainPage:的PhoneApplicationPage { #地区VariableDeclaration

DispatcherTimer timer = new DispatcherTimer(); 
    WebClient client = new WebClient(); 
    WebBrowserTask Facebook = new WebBrowserTask(); 
    WebBrowserTask YouTube = new WebBrowserTask(); 
    WebBrowserTask Odnoklassniki = new WebBrowserTask(); 
    WebBrowserTask Vkontakte = new WebBrowserTask(); 
    List<ItemFormat> Items = new List<ItemFormat>(); 
    DispatcherTimer PopulateIsoFile = new DispatcherTimer(); 
    string SongBuffer; 
    int c = 1; 
    string Time; 

    #endregion 

    #region AppInit 
    // Constructor 
    public MainPage() 
    { 
     InitializeComponent(); 
     if (Microsoft.Phone.Net.NetworkInformation.DeviceNetworkInformation.IsNetworkAvailable == false) 
     { 
      MessageBox.Show("No internet connection", "Error", MessageBoxButton.OKCancel); 
     } 
     else 
     { 
      if (BackgroundAudioPlayer.Instance.PlayerState == PlayState.Playing) 
      { 
       PauseBtn.Visibility = Visibility.Visible; 
       PlayBtn.Visibility = Visibility.Collapsed; 
      } 
      else 
      { 
       BackgroundAudioPlayer.Instance.Track = new AudioTrack(new Uri("http://air-online2.hitfm.md/hitfm.mp3"), "HITFM", "Включи себя", null, null); 
       PlayBtn.Visibility = Visibility.Visible; 
       PauseBtn.Visibility = Visibility.Collapsed; 
      } 
      BackgroundAudioPlayer.Instance.PlayStateChanged += Instance_PlayStateChanged; 
      SlideView.Begin(); 
      SlideView.Completed += SlideView_Completed; 
      SlideView.AutoReverse = true; 
     } 
     timer.Interval = TimeSpan.FromSeconds(30); 
     timer.Tick += timer_Tick; 
     timer.Start(); 
     Loaded += timer_Tick; 
    } 

#地区DownloadTrackInfo

void timer_Tick(object sender, EventArgs e) 
    { 

     try 
     { 
      client.Encoding = System.Text.Encoding.UTF8; 
      client.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.Now.ToString(); 
      client.DownloadStringAsync(new Uri("http://air-online2.hitfm.md/status_hitfm.xsl")); 
      client.DownloadStringCompleted += client_DownloadStringCompleted; 
     } 
     catch (System.Net.WebException) 
     { 
     } 
    } 

    void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
    { 
     try 
     { 
      string[] raw = e.Result.Substring(166).Split('-'); 
      if (raw[0].Contains(":")) 
      { 
       Artist.Text = raw[0].Replace(":", string.Empty).Substring(0, raw[0].Length - 1); 
       Title.Text = raw[1].Substring(1); 
      } 
      else 
      { 
       Artist.Text = raw[0]; 
       Title.Text = raw[1].Substring(1); 
      } 
      TitleProgress.Visibility = Visibility.Collapsed; 
      Title.Foreground = new SolidColorBrush(Colors.White); 
      Artist.Foreground = new SolidColorBrush(Colors.White); 
      if (DateTime.Now.Minute < 10) 
      { 
       Time = "0" + DateTime.Now.Minute.ToString(); 
      } 
      else 
      { 
       Time = DateTime.Now.Minute.ToString(); 
      } 
      ItemFormat Item = new ItemFormat(e.Result.Substring(166).Replace(":", string.Empty), c, DateTime.Now.Hour.ToString() + ":" + Time); 
      if ((!(Item.Song == SongBuffer)) || (Recent.Items.Count == 0)) 
      { 
       Recent.Items.Add(Item); 
       SongBuffer = Item.Song; 
       c += 1; 
      } 
     } 
     catch (System.SystemException) 
     { 

     } 
    }  

}

public class ItemFormat 
{ 
    public string Song { get; set; } 
    public int Count { get; set; } 
    public string Time { get; set; } 
    public ItemFormat(string Song, int count, string time) 
    { 
     this.Song = Song; 
     this.Count = count; 
     this.Time = time; 
    } 
} 

}

我将这个列表框用于收音机的播放列表。但即使用户点击后退按钮或锁定屏幕,我仍然需要保存我的项目。请帮我保存我亲爱的物品。

+0

这些用于填充“列表框”的项目首先来自哪里? –

+0

我已更新代码。 – aodpi

+0

我不需要看到所有的代码,这对我来说今晚是太多了,对其他人来说也是如此。只需粘贴相关部分 - 您从哪里获取数据。 –

回答

0

有几种方法,以“会议”之间存储数据:

  • 使用在IsolatedStorage文件序列化到从化/解。
  • 使用IsolatedStorageSettings出于同样的目的,但数据量较小。
  • 使用数据库,无论是SQL CE或SQLite的

我建议你使用第一种方法,因为它是最简单的一个,你会拿得最少的错误吧。简单地将数据序列化到文件中,无论是在应用程序关闭时还是在更改时都需要。然后,您从文件(如果存在)加载启动时的数据并填写初始列表。

+0

你能展示一些例子吗?因为我尝试了第一种方法但没有成功,我删除了代码:( – aodpi

+0

嘿,托尼先生? – aodpi

+0

http://msdn.microsoft.com/en-us/library/hh821020.aspx –

相关问题