0

我是 WP7 应用程序开发的新手,在将参数传递给网站上的 API 时遇到问题。

据我了解,在 WP7 上打开页面时首先触发 onNavigatedTo(),但是当我尝试获取参数时,首先触发 webClient_DownloadStringCompleted()。

public partial class Ranks : PhoneApplicationPage
{
    private WebClient webClient;
    private string pageType;
    private string pagePosition;
    public Ranks()
    {
        InitializeComponent();
        this.webClient = new WebClient();
        string header_auth = "application/json";
        this.webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
        this.webClient.Headers[HttpRequestHeader.Authorization] = header_auth;

        Uri serviceUri = new Uri(@"http://www.example.com/api/API.php?type=" + pageType + "&position=" + pagePosition);
        this.webClient.DownloadStringAsync(serviceUri);
    }

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
    { 
        string type, position;
        if (NavigationContext.QueryString.TryGetValue("type", out type))
        {
            pageType = type;
        }
        if (NavigationContext.QueryString.TryGetValue("pos", out position))
        {
            pagePosition = position;
        }

    }


    void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        string myJsonString = e.Result;
        List<PlayerDetails> dataSource = new List<PlayerDetails>();

        //load into memory stream
        using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(myJsonString)))
        {
            //parse into jsonser
            var ser = new DataContractJsonSerializer(typeof(PlayerDetails[]));
            PlayerDetails[] obj = (PlayerDetails[])ser.ReadObject(ms);

            foreach (PlayerDetails plyr in obj)
            {   

                dataSource.Add(plyr);

            }

            playerList.ItemsSource = dataSource;

        }


    }

每当构建 URI 字符串时,它都会缺少参数“pageType”和“pagePosition”

任何帮助将不胜感激!

4

1 回答 1

0

类构造函数总是会在之前被调用OnNavigatedTo。您应该将该代码从构造函数中移到OnNavigatedTo(或加载)中。

我猜您在构造函数中有该代码,因为您只希望它在每次页面加载时发生一次(即当用户导航回页面时不发生)。如果是这种情况,您可以检查NavigationMode.

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
    { 
        if (e.NavigationMode == NavigationMode.New)
     {
string type, position;
        if (NavigationContext.QueryString.TryGetValue("type", out type))
        {
            pageType = type;
        }
        if (NavigationContext.QueryString.TryGetValue("pos", out position))
        {
            pagePosition = position;
        }

       this.webClient = new WebClient();
        string header_auth = "application/json";
        this.webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
        this.webClient.Headers[HttpRequestHeader.Authorization] = header_auth;

        Uri serviceUri = new Uri(@"http://www.example.com/api/API.php?type=" + pageType + "&position=" + pagePosition);
        this.webClient.DownloadStringAsync(serviceUri);

     }




    }
于 2012-07-06T05:21:22.790 回答