0
我可以成功地發出請求並從Windows服務中獲得響應,但我無法使用響應中得到的結果更新UI,它顯示無效的交叉線程異常。無法更新Web服務請求後的Windows Phone UI
這裏是我的代碼:
public void UpdateUI()
{
check = dat.Count;
for (int ci = 0; ci < check; ci++)
{
Datum dt = new Datum();
dt = dat[ci];
Title1 = dt.title;
Price = dt.price;
Id = dt.id;
Logo = dt.logo;
Featured = dt.featured;
System.Diagnostics.Debug.WriteLine("UpdateUI");
System.Diagnostics.Debug.WriteLine(Title1.ToString());
System.Diagnostics.Debug.WriteLine(Price.ToString());
System.Diagnostics.Debug.WriteLine(Id.ToString());
System.Diagnostics.Debug.WriteLine(Logo.ToString());
System.Diagnostics.Debug.WriteLine(Featured.ToString());
Grid grd = new Grid();
grd.Name = "grd" + ci;
System.Diagnostics.Debug.WriteLine(grd.Name);
grd.Margin = new Thickness(5, 5, 5, 5);
grd.Background = new SolidColorBrush(Colors.LightGray);
Image img = new Image();
img.Width = 150;
img.Height = 120;
img.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
Uri uri = new Uri("http://www.searchi.in/classifieds/dashboard/classifieds_images/big/"+Logo, UriKind.Absolute);
ImageSource imgSource = new BitmapImage(uri);
img.Source = imgSource;
TextBlock tb = new TextBlock();
tb.MaxWidth=125;
tb.Height = 100;
tb.Text = Title1;
tb.Foreground = new SolidColorBrush(Colors.Black);
tb.FontSize = 22;
tb.VerticalAlignment = VerticalAlignment.Top;
tb.HorizontalAlignment = HorizontalAlignment.Stretch;
tb.TextAlignment = TextAlignment.Left;
TextBlock tb1 = new TextBlock();
tb1.Width = 300;
tb1.Height = 100;
tb1.Text = " ₹ "+Price;
tb1.VerticalAlignment = VerticalAlignment.Bottom;
tb1.Foreground = new SolidColorBrush(Colors.Black);
tb1.FontSize = 22;
tb1.TextAlignment = TextAlignment.Center;
grd.Children.Add(img);
grd.Children.Add(tb);
grd.Children.Add(tb1);
grid.Children.Add(grd);
}
}
和代碼的請求:
void web()
{
// Create the web request object
var url = "http://searchi.in/appservice/classifides.php";
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
// Start the request
webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest);
//asynchronousResult.AsyncWaitHandle.WaitOne();
// webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
}
void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);
System.Diagnostics.Debug.WriteLine("test");
postStream.Close();
// datacheck = 1;
cheker = true;
webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
// asynchronous = asynchronousResult;
//asynchronousResult.AsyncWaitHandle.WaitOne();
System.Diagnostics.Debug.WriteLine("2");
}
void GetResponseCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
System.Diagnostics.Debug.WriteLine("3");
HttpWebResponse response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
Response = readStream.ReadToEnd().ToString();
System.Diagnostics.Debug.WriteLine("GetResponseCallback");
System.Diagnostics.Debug.WriteLine(Response);
DataContractJsonSerializer serializer2 = new DataContractJsonSerializer(typeof(RootObject));
RootObject ro = (RootObject)serializer2.ReadObject(receiveStream);
Success = ro.success;
Sort = ro.sort;
dat = ro.data;
readStream.Close();
response.Close();
//cheker = true;
}
catch (WebException e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
}
UpdateUI();
}