简体   繁体   中英

Return string from Web API DownloadCompleteAsync

I am developing an Windows Phone Application and I am stuck at a part. My project is in c#/xaml - VS2013.

Problem : I have a listpicker (Name - UserPicker) which is list of all user's names. Now I Want to get the UserID from the database for that UserName. I have implemented Web Api and I am using Json for deserialization. But I am not able to return the String from DownloadCompleted event.

Code:

string usid = "";

        selecteduser = (string)UserPicker.SelectedItem;
        string uri = "http://localhost:1361/api/user";
        WebClient client = new WebClient();
        client.Headers["Accept"] = "application/json";
        client.DownloadStringAsync(new Uri(uri));
        //client.DownloadStringCompleted += client_DownloadStringCompleted;
        client.DownloadStringCompleted += (s1, e1) =>
        {
            //var data = JsonConvert.DeserializeObject<Chore[]>(e1.Result.ToString());
            //MessageBox.Show(data.ToString());
            var user = JsonConvert.DeserializeObject<User[]>(e1.Result.ToString());
            foreach (User u in user)
            {
                if (u.UName == selecteduser)
                {
                    usid = u.UserID;

                }
                //result.Add(c);

                return usid;
            }
            //return usid
        };

I want to return the UserID of the selected user. But Its Giving me following errors.

Since 'System.Net.DownloadStringCompletedEventHandler' returns void, a return keyword must not be followed by an object expression

Cannot convert lambda expression to delegate type 'System.Net.DownloadStringCompletedEventHandler' because some of the return types in the block are not implicitly convertible to the delegate return type

If you check source code of DownloadStringCompletedEventHandler you will see that it is implemented like that:

public delegate void DownloadStringCompletedEventHandler(
   object sender, DownloadStringCompletedEventArgs e);

That means that you can't return any data from it. You probably have some method that does something with selected user id. You will need to call this method from event handler. So if this method is named HandleSelectedUserId , then code might look like that:

client.DownloadStringCompleted += (sender, e) =>
{
    string selectedUserId = null;
    var users = JsonConvert.DeserializeObject<User[]>(e.Result.ToString());
    foreach (User user in users)
    {
        if (user.UName == selecteduser)
        {
            selectedUserId = user.UserID;
            break;
        }
    }

    HandleSelectedUserId(selectedUserId);
};
client.DownloadStringAsync(new Uri("http://some.url"));

It's also a good idea to add event handler for DownloadStringCompleted event before you call DownloadStringAsync method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM