-1

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

公共部分类 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 下载跟踪信息

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

}

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

4

1 回答 1

0

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

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

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

于 2014-02-22T21:08:37.410 回答