如何将Web服务数据解析为windows phone 8.0 pivot app MainViewModel?

时间:2014-09-16 09:53:53

标签: c# .net windows-phone-8 httpclient

我是C#windows phone编程的新手。 我在Windows Phone 8.0中有关于数据透视模板应用的问题

如何使用HttpClient Data配置Pivot模板应用?

我有这个http请求,也可以得到回复。这工作正常..

public async static Task<List<MyData>> GetWebserviceData()
{
       HttpClient = new HttpClient();
       string response = await client.GetStringAsync("http://xxxx/xxx/xxx");

       List<MyData> data = JsonConvert.DeserializeObject<List< MyData >>(response);

       return data;
}

我想将此数据解析为pivot app - &gt; MainViewModel - &gt; LoadData()。

    public class MainViewModel : INotifyPropertyChanged
        {
            public MainViewModel()
            {
                this.Items = new ObservableCollection<ItemViewModel>();
            }

            /// <summary>
            /// A collection for ItemViewModel objects.
            /// </summary>
            public ObservableCollection<ItemViewModel> Items { get; private set; }

            private string _sampleProperty = "Sample Runtime Property Value";
            /// <summary>
            /// Sample ViewModel property; this property is used in the view to display its value using a Binding
            /// </summary>
            /// <returns></returns>
            public string SampleProperty
            {
                get
                {
                    return _sampleProperty;
                }
                set
                {
                    if (value != _sampleProperty)
                    {
                        _sampleProperty = value;
                        NotifyPropertyChanged("SampleProperty");
                    }
                }
            }

            /// <summary>
            /// Sample property that returns a localized string
            /// </summary>
            public string LocalizedSampleProperty
            {
                get
                {
                    return AppResources.SampleProperty;
                }
            }



            /// <summary>
            /// Creates and adds a few ItemViewModel objects into the Items collection.
            /// </summary>
            public void LoadData()
            {
                // This Sample data; want to replace with real data which is return from GetWebserviceData()

                this.Items.Add(new ItemViewModel() { ID ="1", LineOne = "runtime one", LineTwo = "Maecenas praesent accumsan bibendum", LineThree = "Facilisi faucibus habitant inceptos interdum lobortis nascetur pharetra placerat pulvinar sagittis senectus sociosqu" });
                this.Items.Add(new ItemViewModel() { ID = "2", LineOne = "runtime two", LineTwo = "Dictumst eleifend facilisi faucibus", LineThree = "Suscipit torquent ultrices vehicula volutpat maecenas praesent accumsan bibendum dictumst eleifend facilisi faucibus" });


            }


        }
    }

任何人都可以帮助我..谢谢.. !!

1 个答案:

答案 0 :(得分:0)

以下列方式重写LoadData函数:

public async void LoadData()
{
    var response = await GetWebserviceData();
    foreach(var item in response)
    {
     this.Items.Add(item);
    }
}