0

Somewhat related to this topic here: Async XML Reading in Windows Phone 7

I'm developing a Windows Phone app, and I have a search function in my Search.xaml.cs file. It is called by clicking a button, it creates a search query and calls DownloadStringInBackground with it

    private void SearchQuery(object sender, EventArgs e)
    {
        string temp = "http://api.search.live.net/xml.aspx?Appid=myappid&query=randomqueryhere&sources=web";
        DownloadStringInBackground(temp);
    }

    public static void DownloadStringInBackground(string address)
    {
        WebClient client = new WebClient();
        Uri uri = new Uri(address);

        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCallback);
        client.DownloadStringAsync(uri);
    }

    private static void DownloadStringCallback(Object sender, DownloadStringCompletedEventArgs e)
    {
        // Fancy manipulation logic here

        finalResult = words;
    }

finalResult has been stored as

public static string[] finalResult;

in the Search class. My question is, where can I put the Navigate command (NavigationService.Navigate(new Uri("/Result.xaml", UriKind.Relative));)? I tried doing it in the callback, but I get a nullobject exception due to the static keyword. How can I ensure that finalResult has been populated, and that I can navigate to Result.xaml and reference the data in finalResult on that page. Alternately, how can I pass Words or finalResult to Result.xaml?

Thanks for looking :)

Community
  • 1
  • 1
Freakishly
  • 1,533
  • 5
  • 32
  • 61
  • So it turns out I was going about my problem the wrong way. The correct approach is here:http://www.jaylee.org/post/2010/06/22/WP7Dev-Using-the-WebClient-with-Reactive-Extensions-for-Effective-Asynchronous-Downloads.aspx I'm going to mark Mick's answer as the right one because that is the answer to my original question. Thanks everyone. – Freakishly Dec 16 '10 at 09:27
  • Best solution IMO: http://stackoverflow.com/questions/4701566/passing-a-complex-object-to-a-page-while-navigating-in-a-wp7-silverlight-applica/15536127#15536127 – radsdau Mar 20 '13 at 22:44

2 Answers2

3

There is a walkthrough here on passing values between pages.

How to: Perform Page Navigation on Windows Phone

Migisha
  • 415
  • 4
  • 10
Mick N
  • 14,892
  • 2
  • 35
  • 41
  • Could you describe clearly where (and on what object reference) the null object exception is occuring? – Mick N Dec 16 '10 at 06:45
  • In DownloadStringCallback, what is the value of finalResult immediately prior to Navigate() if you place a call to Navigate immediately after it. Are you able to break on the Navigate, find non null contents and execute further without error. If yes, and without the breakpoint it fails, could there be some timing problem related to code not shown (words). – Mick N Dec 16 '10 at 06:49
0

If you don't make the callback function static you can do this:

Dispatcher.BeginInvoke(() => NavigationService.Navigate(new Uri("/Result.xaml", UriKind.Relative)));

If the callback function must be static you can use:

Deployment.Current.Dispatcher.BeginInvoke();
Matt Lacey
  • 65,560
  • 11
  • 91
  • 143
  • If I remove the 'static' keyword from the callback function (DownloadStringCallback), I get an error saying: An object reference is required for the non-static field, method or property DownloadStringCallback. – Freakishly Dec 16 '10 at 05:34