0

Here is what I am trying to do. In my WP7 app, I am loading a page that has two StackPanels. StackPanel1 is "Collapsed" and StackPanel2 is "Visible". On load of the page, I am kicking off an HttpWebRequest and then processing the BeginGetResponse asynchronously. At this point I just want to swap the Visibility of the two StackPanels. However, since the BeginGetResponse is run Asynchronously, I am no longer in the UI thread and cannot manipulate these StackPanel controls. If I try to reference them, of course, I get "An object reference is required for the non-static field, method, or property 'blah.StackPanel1'"

This all makes sense and I get why.

Here are some things I have tried:

  1. Delegates, but any way I sliced it, I needed a static reference to my controls. fail.
  2. I tried to create a static reference to my page class and then use that to reference my controls in the BeginGetResponse. This compiled, but I got a UnauthorizedAccessException 'invalid cross-thread access.' at run-time when I tried to reference the controls.
  3. Searching and searching and searching.
  4. Using Deployment.Current.Dispatcher.BeginInvoke to run on the UI thread.

How can I statically reference these controls?

OR is there a better way to do what I'm doing?

EDIT:

Here is my HttpWebRequest

if (NetworkInterface.GetIsNetworkAvailable())
        {
            HttpWebRequest httpWebRequest = HttpWebRequest.CreateHttp("http://urlThatWorks.com");
            httpWebRequest.Method = "GET";
            httpWebRequest.BeginGetResponse((asyncresult) =>
                //do processing of my return here

                //then here is the problem
                StackPanel1.Visibility = System.Windows.Visibility.Visible;
                StackPanel2.Visibility = System.Windows.Visibility.Collapsed;

           }, httpWebRequest);

        }

ANOTHER EDIT:

And here is how I tried with Deployment.Current.Dispatcher.BeginInvoke

  httpWebRequest.BeginGetResponse((asyncresult) =>
      //do processing of my return here

      Deployment.Current.Dispatcher.BeginInvoke(() =>
          {
              StackPanel1.Visibility = System.Windows.Visibility.Visible;
              StackPanel2.Visibility = System.Windows.Visibility.Collapsed;
          });
  }, httpWebRequest);
davehale23
  • 4,374
  • 2
  • 27
  • 40

1 Answers1

1

You don't really want a static reference, you want a thread-safe way of accessing them.
You can execute it on the UI thread by:

Deployment.Current.Dispatcher.BeginInvoke(()=> SomeMethod);

or

Deployment.Current.Dispatcher.BeginInvoke(()=> {  // code });
Robaticus
  • 22,857
  • 5
  • 54
  • 63
  • I found this a few minutes ago actually. http://stackoverflow.com/questions/4679324/what-is-the-use-of-deployment-current-dispatcher-begininvoke And I tried to get it to work, but could not. I still get "an object reference is required for non-static field . . . " I'll edit my question to include this. – davehale23 Mar 09 '12 at 19:23
  • Is your async process defined within your page, or is it external (e.g. separate class, viewmodel, etc.)? – Robaticus Mar 09 '12 at 19:58
  • Well, I created a new page with ONLY the webrequest and the show/hide in it and it works. So, somehow something else that I'm doing on the page is messing me up. But the method that I'm using is yours. Thanks for the help, I'll have to dig into the rest of the page this weekend. – davehale23 Mar 09 '12 at 22:53