WebClient事件触发顺序

时间:2012-07-06 03:49:54

标签: json windows-phone-7 rest webclient

我是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'

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

类构造函数将始终在OnNavigatedTo之前调用。你应该把这个代码从构造函数移到OnNavigatedTo(或Loaded)。

我猜你在构造函数中有这个代码,因为你只希望每个页面加载一次(即当用户导航回页面时)。如果是这种情况,您可以查看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);

     }




    }