htttwebresponse方法中的Messagebox.show错误

时间:2013-02-07 08:41:05

标签: c# windows-phone-8

我有以下响应代码。我收到错误,如System.Windows.ni.dll中发生类型'System.UnauthorizedAccessException'的异常但在用户codeerror中未处理,当调用Messagebox.sohw()方法显示代码时:200。 我的Json就像{“meta”:{“code”:200}}

          private void ReadWebRequestCallback(IAsyncResult ar)
          {
           HttpWebRequest myReq = (HttpWebRequest)ar.AsyncState;
            HttpWebResponse myResponse = (HttpWebResponse)myReq.EndGetResponse(ar);
uritext.Text = myResponse.ResponseUri.ToString();
            using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
            {
                string results = httpwebStreamReader.ReadToEnd();    
            }
            myResponse.Close();
           var root = JsonConvert.DeserializeObject<RootObject>(jsondata);
           String codenum = root.meta.code.ToString();
           Messagebox.sohw(codenum);
           }
   public class Meta
    {
        public int code { get; set; }
    }

    public class RootObject
    {
        public Meta meta { get; set; }
    }

1 个答案:

答案 0 :(得分:2)

问题是来自HttpWebRequest的回调是从非UI线程发生的。

以下是在UI线程上执行MessageBox.Show的操作:

Dispatcher.BeginInvoke(() => MessageBox.Show(codenum));