Xamarin的.窗体Windows应用程序加载XML数据到ListView

本文关键字:XML 数据 ListView 加载 应用程序 窗体 Windows Xamarin | 更新日期: 2023-09-27 18:14:07

我正在用Xamarin编写一个应用程序。用于Android、iOS和Windows Store的表单。我想用来自XML文件的大约10,000个元素填充ListView。问题是,当我运行Windows Phone或Windows Store的代码时,应用程序在返回并填充ListView之前延迟了大约一分钟。它立即在Android和iOS上流行起来。然而,对于Windows,它却滞后了。有人能帮我解决这个问题吗?下面是我的代码,它读取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;
    }

这段代码可以工作,并且在iOS和Android上运行顺畅。但是在Windows中,它会挂起大约1分钟。请帮助。

谢谢

Xamarin的.窗体Windows应用程序加载XML数据到ListView

答案是设置listview项的高度。然后立即加载