windows phone 7 - loading a big xml file

纵然是瞬间 提交于 2019-12-04 10:20:35

To improve performance you can do the following:

  • Make sure the loading is moved off the ui thread
  • Break the file into multiple smaller ones and load them one at a time and updating the list as you add each one.
  • If possible, reformat the data (before adding to the app) so it only includes the absolute minimum data used by the app.
  • Convert the file to a different format (try JSON) which can be parsed faster.
  • (If using 7.1) put the data in a database and deploy that in the XAP.
  • Reconsider the need to have so much data. Large/long lists are hard to browse on a device with a small screen.

You could try putting that code into a background thread to do its stuff while you can get on with other stuff in your main thread.

delcare "private BackgroundWorker backgroundWorker;" in the MainPage partial class

Put this code in your MainPage() constuctor (or MainPage_loaded)

// Initiaze background worker do display favourites

        if (backgroundWorker == null)
        {
            backgroundWorker = new BackgroundWorker();
            backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
        }
        backgroundWorker.RunWorkerAsync();

and then have your code in here:

    void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
       XDocument loadedData = XDocument.Load("BigFile.xml");
        var data = from query in loadedData.Descendants("w")
                   orderby (string)query.Element("e")
                   select new myClass
                   {
                       First = (string)query.Element("a"),
                       Second = (string)query.Element("e")
                   };
    ListBox.ItemsSource = data.ToList<myClass>();

    }

Hopefully this should do the trick.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!