无法在回调方法中添加视图&在Windows Phone 7中收到错误“UnauthorizedAccessException”

时间:2012-04-09 13:08:44

标签: c# windows-phone-7 windows-phone-7.1

我是Windows Phone 7& S的新手。 C#。我正面临导航到新屏幕的问题。我的要求是我需要检查登录凭证到服务器&如果确定,则转到下一个屏幕。但我收到错误“UnauthorizedAccessException” - “无效的跨线程访问”。 。

查找以下代码

 private void button1_Click(object sender, RoutedEventArgs e)
    {     
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(uri));
        request.ContentType = "application/x-www-form-urlencoded";
        request.Method = "POST";
        request.BeginGetResponse(ResponseCallback, request);
    }   

  private void ResponseCallback(IAsyncResult asynchronousResult) 
    {
        var request = (HttpWebRequest)asynchronousResult.AsyncState;
        var response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);

        if (response.StatusCode  == HttpStatusCode.OK)
        {
            this.NavigationService.Navigate(new Uri("/tabview.xaml", UriKind.Relative));
        }
    }

1 个答案:

答案 0 :(得分:1)

尽管OP修复了他的问题,但this MSDN文章详细解释了其背后的基本原理。