简体   繁体   中英

Unable to add view in callback method & getting error "UnauthorizedAccessException" in windows phone 7

I am new to Windows Phone 7 & C#. I am facing problem for navigation to new screen. My requirement is i need to Check login credentials to server & if OK then move on to next screen. But i am getting error as "UnauthorizedAccessException" - "Invalid cross-thread access". .

Find Code below

 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));
        }
    }

Although the OP has fixed his issue, this MSDN article explains the rationale behind this in detail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM