Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用XML文件中的数据填充GridView_C#_Xaml_Windows 8 - Fatal编程技术网

C# 用XML文件中的数据填充GridView

C# 用XML文件中的数据填充GridView,c#,xaml,windows-8,C#,Xaml,Windows 8,因此,在Windows 8平板电脑应用程序中,我有一个具有以下属性的GridView: <Grid> <GridView ItemsSource="{Binding Source={StaticResource manager}, Path=TestStrings}" /> </Grid> 链接到另一个类中的属性TestString的 public List<string> TestStrings {

因此,在Windows 8平板电脑应用程序中,我有一个具有以下属性的GridView:

    <Grid>
        <GridView ItemsSource="{Binding Source={StaticResource manager}, Path=TestStrings}" />
    </Grid>

链接到另一个类中的属性TestString的

public List<string> TestStrings
    {
        get
        {
            List<Location> locations = getLocations(); 

            List<string> testStrings = new List<string>();

            for (int i = 0; i < locationList.Count; i++)
            {
                testStrings.Add(locationList[i].Name);
            }

            return testStrings;
        }
    }

    public async Task<List<Location>> getLocations()
    {
        return await xmlParser.getLocations();
    }
公共列表测试字符串
{
得到
{
列表位置=getLocations();
List testStrings=new List();
对于(int i=0;i
如果我只是用值填充一个字符串列表并返回它,GridView就会显示这些值,没有问题。然而,问题是我需要调用一个异步方法。我的大部分数据将来自XML文件。要访问XML文件,我必须从存储器中取出该文件,据我所知,这需要我等待。以下是该方法:

public async Task<List<Location>> getLocations()
    {
        StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
        StorageFile file = await storageFolder.GetFileAsync("main.xml");
        XmlDocument xmlDoc= await XmlDocument.LoadFromFileAsync(file);
        XDocument xml = XDocument.Parse(xmlDoc.GetXml());

        List<Location> locationList =
            (from _location in xml.Element("apps").Elements("app").Elements("locations").Elements("location")
             select new Location
             {
                 Name = _location.Element("name").Value,
             }).ToList();

        return locationList;
    }
公共异步任务getLocations() { StorageFolder StorageFolder=ApplicationData.Current.LocalFolder; StorageFile文件=等待storageFolder.GetFileAsync(“main.xml”); XmlDocument xmlDoc=wait XmlDocument.LoadFromFileAsync(文件); XDocument xml=XDocument.Parse(xmlDoc.GetXml()); 列表位置列表= (来自xml.Element(“apps”).Elements(“app”).Elements(“位置”).Elements(“位置”)中的_位置) 选择新位置 { Name=_location.Element(“Name”).Value, }).ToList(); 返回位置列表; }
如您所见,我等待了两次,这迫使我将其设置为异步方法,这意味着所有调用它的方法都必须是异步的。但是,XAML中的绑定属性要求我访问一个属性,该属性不能是异步的


我觉得我错过了什么。我最近从安卓系统开始在Windows8上编程,所以很多对我来说都是新的。当然,将数据从文件显示到UI是一项常见的任务。处理此问题的最佳方法是什么?

尝试将列表的类型从
列表更改为
可观察的集合
,看看这是否解决了问题。您对异步行为的观察非常准确:对列表的修改将而不是触发一个通知,通知绑定引擎有任何更改,并且更改将在未知时间后发生。该通知将在您使用时发生