无法在回调方法中添加视图,并在Windows Phone 7中收到错误“UnauthorizedAccessExcept
本文关键字:Phone UnauthorizedAccessExcept 错误 Windows 回调 方法 添加 视图 并在 | 更新日期: 2023-09-27 18:30:25
我是Windows Phone 7和C#的新手。我遇到导航到新屏幕的问题。我的要求是我需要检查服务器的登录凭据,如果确定,则转到下一个屏幕。但是我收到错误为"未经授权的访问异常" - "无效的跨线程访问"。.
在下面查找代码
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));
}
}
尽管 OP 已修复他的问题,但这篇 MSDN 文章详细解释了这背后的基本原理。