如何在 Windows Phone 8 Mvvm 中显示包含 XML 数据的图表

本文关键字:XML 包含 数据 显示 Windows Phone Mvvm | 更新日期: 2023-09-27 17:56:16

我正在制作一个应用程序,该应用程序必须从在线XML中按日期显示货币汇率。我正在使用麻雀图。当使用示例数据对其进行测试时,图表仅在 ChartViewModel 函数中调用 GenerateDatas() 方法时才显示数据,但在读取 asinc XML 数据或使用 ICommand 调用函数时,它不会显示或刷新。

     List<CurrencyOfDate> list = new List<CurrencyOfDate>();
            foreach (XElement c in xmlData.Elements(ns + "Cube").Elements(ns + "Cube"))
                list.Add(new CurrencyOfDate()
                {
                    Date = c.Attribute("time").Value,
                    CurrencyTypes = (from k in xmlData.Elements(ns + "Cube").Elements(ns + "Cube").Elements(ns + "Cube")
                                     select new CurrencyType()
                                     {
                                         Name = k.Attribute("currency").Value,
                                         Value = k.Attribute("rate").Value
                                     }).ToList()
                });
            FeatList = list;
            if (FeatList != null)
            {
                foreach (var date in FeatList)
                {
                    var x = DateTime.Parse(date.Date);
                    PickedDate.Add(x);
                }
                for (int i = 0; i < FeatList.Count; i++)
                {
                    CurList = FeatList[i].CurrencyTypes.ToList();
                }
            }
            MakeChartData();
}
private void MakeChartData()
    {
        try
        {
            IsDataLoading = true;
            for (int i = 0; i < CurList.Count; i++)
            {
                var selectedCurrencyRates =
                    (from c in CurList where c.Name == "GBP" select c).ToList();
                foreach (var selRate in selectedCurrencyRates)
                {
                    double parsedFromValue = Double.Parse(selRate.Value);
                        CalculatedRate.Add(parsedFromValue);
                }
            }
            GenerateDatas();
        }
        catch (Exception e)
        {
            IsDataLoading = false;
        }
        Refresh();
    }
    private void GenerateDatas()
    {
        Refresh();
        try
        {
            IsDataLoading = true;

            this.Collection.Add(new Model(PickedDate[0], CalculatedRate[0]));
            this.Collection.Add(new Model(PickedDate[10], CalculatedRate[10]));
            this.Collection.Add(new Model(PickedDate[20], CalculatedRate[20]));
            this.Collection.Add(new Model(PickedDate[30], CalculatedRate[30]));

            IsDataLoading = false;
        }
        catch (Exception ex)
        {
            IsDataLoading = false;
        }
    }

和 xaml:

     <chart:SparrowChart x:Name="Chart1" Height="409" Margin="0,0,22,0">
            <chart:SparrowChart.Legend>
                <chart:Legend Header="EUR/GBP" Height="55" Margin="155,0,172,4" Width="107" />
            </chart:SparrowChart.Legend>
            <chart:SparrowChart.DataContext>
                <viewModels:ChartViewModel/>
            </chart:SparrowChart.DataContext>
            <chart:SparrowChart.XAxis>
                <chart:LinearXAxis/>
            </chart:SparrowChart.XAxis>
            <chart:SparrowChart.YAxis>
                <chart:LinearYAxis/>
            </chart:SparrowChart.YAxis>
            <chart:LineSeries PointsSource="{Binding Collection}" XPath="X" YPath="Y" IsRefresh="True"/>
        </chart:SparrowChart>

如何在 Windows Phone 8 Mvvm 中显示包含 XML 数据的图表

问题解决了。此代码有效。问题是集合是列表类型,但它应该是可观察集合。