How do I navigate from one xaml page to another, a

2019-03-04 05:05发布

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 :)

2条回答
叼着烟拽天下
2楼-- · 2019-03-04 05:32

There is a walkthrough here on passing values between pages.

How to: Perform Page Navigation on Windows Phone

查看更多
放我归山
3楼-- · 2019-03-04 05:49

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();
查看更多
登录 后发表回答