将长列表选择器绑定到POST方法的结果

时间:2014-07-14 20:47:04

标签: c# web-services windows-phone-8 longlistselector

我正在尝试将数据发布到服务器并获取响应以将其绑定到长列表选择器。这是代码:

     public class TestData
    {
        private string _LikeNum, _CommentNum, _HyperLinkTitle, _HyperLinkNavigationLink, _BrandImage, _PostImage, _PostDate, _PostTitle, _PostDescription, _PostID;
        public string LikeNum { get { return _LikeNum; } set { _LikeNum = value; } }
        public string CommentNum { get { return _CommentNum; } set { _CommentNum = value; } }
        public string HyperLinkTitle { get { return _HyperLinkTitle; } set { _HyperLinkTitle = value; } }
        public string HyperLinkNavigationLink { get { return _HyperLinkNavigationLink; } set { _HyperLinkNavigationLink = value; } }
        public string BrandImage { get { return _BrandImage; } set { _BrandImage = value; } }
        public string PostImage { get { return _PostImage; } set { _PostImage = value; } }
        public string PostDate { get { return _PostDate; } set { _PostDate = value; } }
        public string PostTitle { get { return _PostTitle; } set { _PostTitle = value; } }
        public string PostDescription { get { return _PostDescription; } set { _PostDescription = value; } }
        public string PostID { get { return _PostID; } set { _PostID = value; } }
        public TestData(string LNum, string CNum, string HLTitle, string HLNaviagtionLink, string BImage, string PImage, string PDate, string PTitle, string PDescription, string PID)
        {
            this.LikeNum = LNum;
            this.CommentNum = CNum;
            this.HyperLinkTitle = HLTitle;
            this.HyperLinkNavigationLink = HLNaviagtionLink;
            this.BrandImage = BImage;
            this.PostImage = PImage;
            this.PostDate = PDate;
            this.PostTitle = PTitle;
            this.PostDescription = PDescription;
            this.PostID = PID;
        }
    }
    #region Lists of data
    List<string> LstBrandID = new List<string>();
    List<string> LstBrandName = new List<string>();
    List<string> LstBrandLongitude = new List<string>();
    List<string> LstBrandLatitude = new List<string>();
    List<string> LstPostID = new List<string>();
    List<string> LstPostTitle = new List<string>();
    List<string> LstPostDescription = new List<string>();
    List<string> LstPostDate = new List<string>();
    List<string> LstLikeNum = new List<string>();
    List<string> LstCommentNum = new List<string>();
    List<string> LstUserLike = new List<string>();
    List<string> LstCatName = new List<string>();
    List<string> LstUserFollow = new List<string>();
    #endregion
    ObservableCollection<TestData> DataList = new ObservableCollection<TestData>();
    string id;
    public Home()
    {
        InitializeComponent();
        id = PhoneApplicationService.Current.State["id"].ToString();
        try
        {
            GetPosts(id);
            myLLS.ItemsSource = DataList;
            for (int i = 1; i <= 10; i++)
            {
                DataList.Add(new TestData(LstLikeNum[i], LstCommentNum[i], LstBrandName[i], "SomePage.xaml", "SomeLink.com/data/" + LstBrandID[i], "SomeLink.com/data/" + LstPostID[i], LstPostDate[i], LstPostTitle[i], LstPostDescription[i], LstPostID[i]));
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
            throw;
        }

    }

    #region getting data
    void GetPosts(string UserID)
    {
        WebClient webclient = new WebClient();
        Uri uristring = new Uri("SomeLink.com");
        webclient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
        string postJsonData = string.Empty;
        postJsonData += "userId=" + UserID;
        webclient.UploadStringAsync(uristring, "POST", postJsonData);
        webclient.UploadStringCompleted += webclient_UploadStringCompleted;
    }

    void webclient_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
    {
        try
        {
            if (e.Result != null)
            {
                string response = e.Result.ToString();
                JArray a = JArray.Parse(response);
                foreach (JObject o in a.Children<JObject>())
                {
                    foreach (JProperty p in o.Properties())
                    {
                        string name = p.Name;
                        string value = p.Value.ToString();
                        if (name == "brandId")
                        {
                            LstBrandID.Add(value);
                        }
                        else if (name == "brandName")
                        {
                            LstBrandName.Add(value);
                        }
                        else if (name == "brandLongitude")
                        {
                            LstBrandLongitude.Add(value);
                        }
                        else if (name == "brandLatitude")
                        {
                            LstBrandLatitude.Add(value);
                        }
                        else if (name == "postId")
                        {
                            LstPostID.Add(value);
                        }
                        else if (name == "postTitle")
                        {
                            LstPostTitle.Add(value);
                        }
                        else if (name == "postDescription")
                        {
                            LstPostDescription.Add(value);
                        }
                        else if (name == "postDate")
                        {
                            LstPostDate.Add(value);
                        }
                        else if (name == "likeNum")
                        {
                            LstLikeNum.Add(value);
                        }
                        else if (name == "commentNum")
                        {
                            LstCommentNum.Add(value);
                        }
                        else if (name == "userLike")
                        {
                            LstUserLike.Add(value);
                        }
                        else if (name == "catName")
                        {
                            LstCatName.Add(value);
                        }
                        else if (name == "userFollow")
                        {
                            LstUserFollow.Add(value);
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

    }
    #endregion

当我运行应用程序时,我得到 超出范围的例外 获取数据不是问题。问题是从服务器获取数据以将其绑定到长列表选择器所花费的时间。那么,我怎样才能延迟绑定告诉我从服务器获取数据并用它们填充列表?

1 个答案:

答案 0 :(得分:0)

Home()

myLLS.ItemsSource = DataList;

很好。您告诉LLS观看此数据源并在插入新项目时自行更新。

for (int i = 1; i <= 10; i++)
{
    DataList.Add(new TestData(LstLikeNum[i], LstCommentNum[i], LstBrandName ...);
}

不好。您试图在数据到达之前填充数据源。这是属于请求回调的操作。您因为LstLikeNum有0项而且您正在尝试获取0 - &gt;项而超出范围异常9。

相反,这样做:

void webclient_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e) {
    ...
    int i = 0;
    foreach (JObject o in a.Children<JObject>())
    {
        foreach (JProperty p in o.Properties())
        {
        ...
        }

        var testData = new TestData(LstLikeNum[i], LstCommentNum[i], ...);
        DataList.Add(testData);
        i++;
    }
}

请注意,您不需要在回调中绑定(myLLS.ItemsSource = DataList)。这只是你做过一次的事情 - 回调只是向源头添加项目。

相关问题