2014-02-22 17 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 –

相關問題