即使我重新启动应用程序,我怎么能保存列表框项目?

时间:2014-02-22 20:30:23

标签: c# windows-phone-7 wp7test

即使我在Windows手机应用程序中重新启动应用程序,我如何保存列表框项目。我想让它们以某种方式保存在文件中,然后在应用程序的下一次启动时读取它们。请帮忙.. 好的,我正在用代码更新:

公共部分类MainPage:PhoneApplicationPage     {         #region 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;
    }

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

}

我将此列表框用于收音机的某种播放列表。但即使用户单击后退按钮或处于锁定屏幕下,我也需要保存我的项目。请帮我保存我亲爱的物品。

1 个答案:

答案 0 :(得分:0)

有几种方法可以在“会话”之间存储数据:

  • 使用IsolatedStorage中的文件序列化/反序列化。
  • 使用IsolatedStorageSettings用于相同目的,但数据量较少。
  • 使用数据库,SQL CE或sqlite

我建议您使用第一种方法,因为它是最简单的方法,您将获得最少的错误。只需在应用程序关闭或更改时将数据序列化到文件中。然后,您可以在文件启动时加载数据(如果存在)并填写初始列表。