简体   繁体   English

Xamarin.Forms Windows应用程序将XML数据加载到ListView中

[英]Xamarin.Forms Windows App load XML data into ListView

I am writing an application in Xamarin.Forms for Android, iOS, and Windows Store. 我正在Xamarin.Forms中为Android,iOS和Windows Store编写应用程序。 I want to populate a ListView with about 10,000 elements from an XML file. 我想用XML文件中的大约10,000个元素填充ListView。 The problem is when I run the code for Windows Phone or Windows Store, the application lags for about a minute before coming back alive and populating the ListView. 问题是,当我运行Windows Phone或Windows Store的代码时,应用程序滞后了大约一分钟,然后才恢复活动并填充ListView。 It populates instantly for Android and iOS. 它会立即为Android和iOS填充。 However for Windows, it lags. 但是,对于Windows,它会滞后。 Could somebody help me fix this problem? 有人可以帮我解决这个问题吗? Here is my code which does the reading of XML and populating the ListView: 这是我的代码,用于读取XML并填充ListView:

public async void LoadCharacters()
    {
        Assembly assembly = typeof(CharacterPage).GetTypeInfo().Assembly;
        string file = assembly.GetManifestResourceNames().First(x => x.Contains("Characters.xml"));
        Stream stream = assembly.GetManifestResourceStream(file);

        data = new ObservableCollection<Character>();

        await Task.Factory.StartNew(delegate {

            XDocument doc = XDocument.Load(stream);

            IEnumerable<Character> characters = from query in doc.Descendants("c")
                                                select new Character
                                                {
                                                    Simplified = query.Attribute("s").Value,
                                                    Traditional = query.Attribute("t").Value,
                                                    Pinyin = query.Attribute("p").Value,
                                                    English = query.Attribute("e").Value,
                                                    URL = query.Attribute("u").Value
                                                };
            data = characters.ToObservableCollection();
        });

        characterList.ItemsSource = data;
    }

This code works, and runs smooth in iOS and Android. 此代码有效,并且可以在iOS和Android中流畅运行。 However in Windows, it hangs for about 1 minutes. 但是,在Windows中,它挂起大约1分钟。 Please help. 请帮忙。

Thank you 谢谢

The answer is to set the height of the listview item. 答案是设置列表视图项目的高度。 Then it loads instantly. 然后立即加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM