Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3217452
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:24:46+00:00 2026-05-17T15:24:46+00:00

I have been experimenting with WP7 apps today and have hit a bit of

  • 0

I have been experimenting with WP7 apps today and have hit a bit of a wall.
I like to have seperation between the UI and the main app code but Ive hit a wall.

I have succesfully implemented a webclient request and gotten a result, but because the call is async I dont know how to pass this backup to the UI level. I cannot seem to hack in a wait for response to complete or anything.
I must be doing something wrong.

(this is the xbox360Voice library that I have for download on my website: http://www.jamesstuddart.co.uk/Projects/ASP.Net/Xbox_Feeds/ which I am porting to WP7 as a test)

here is the backend code snippet:

    internal const string BaseUrlFormat = "http://www.360voice.com/api/gamertag-profile.asp?tag={0}";
    internal static string ResponseXml { get; set; }
    internal static WebClient Client = new WebClient();

    public static XboxGamer? GetGamer(string gamerTag)
    {
        var url = string.Format(BaseUrlFormat, gamerTag);

        var response = GetResponse(url, null, null);

        return SerializeResponse(response);
    }

    internal static XboxGamer? SerializeResponse(string response)
    {
        if (string.IsNullOrEmpty(response))
        {
            return null;
        }

        var tempGamer = new XboxGamer();
        var gamer = (XboxGamer)SerializationMethods.Deserialize(tempGamer, response);

        return gamer;
    }

    internal static string GetResponse(string url, string userName, string password)
    {


            if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
            {
                Client.Credentials = new NetworkCredential(userName, password);
            }

            try
            {
                Client.DownloadStringCompleted += ClientDownloadStringCompleted;
                Client.DownloadStringAsync(new Uri(url));

                return ResponseXml;
            }
            catch (Exception ex)
            {
                return null;
            }
        }



    internal static void ClientDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            ResponseXml = e.Result;
        }
    }

and this is the front end code:

public void GetGamerDetails()
{
    var xboxManager = XboxFactory.GetXboxManager("DarkV1p3r");
    var xboxGamer = xboxManager.GetGamer();

    if (xboxGamer.HasValue)
    {
        var profile = xboxGamer.Value.Profile[0];
        imgAvatar.Source = new BitmapImage(new Uri(profile.ProfilePictureMiniUrl));
        txtUserName.Text = profile.GamerTag;
        txtGamerScore.Text = int.Parse(profile.GamerScore).ToString("G 0,000");
        txtZone.Text = profile.PlayerZone;
    }
    else
    {
        txtUserName.Text = "Failed to load data";
    }
}

Now I understand I need to place something in ClientDownloadStringCompleted but I am unsure what.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:24:46+00:00Added an answer on May 17, 2026 at 3:24 pm

    The problem you have is that as soon as an asynchronous operation is introduced in to the code path the entire code path needs to become asynchronous.

    • Because GetResponse calls DownloadStringAsync it must become asynchronous, it can’t return a string, it can only do that on a callback
    • Because GetGamer calls GetResponse which is now asynchronous it can’t return a XboxGamer, it can only do that on a callback
    • Because GetGamerDetails calls GetGamer which is now asynchronous it can’t continue with its code following the call, it can only do that after it has received a call back from GetGamer.
    • Because GetGamerDetails is now asynchronous anything call it must also acknowledge this behaviour.
    • …. this continues all the way up to the top of the chain where a user event will have occured.

    Here is some air code that knocks some asynchronicity in to the code.

    public static void GetGamer(string gamerTag, Action<XboxGamer?> completed) 
    { 
        var url = string.Format(BaseUrlFormat, gamerTag); 
    
        var response = GetResponse(url, null, null, (response) =>
        {
            completed(SerializeResponse(response));
        }); 
    } 
    
    
    internal static string GetResponse(string url, string userName, string password, Action<string> completed)      
    {      
    
       WebClient client = new WebClient();
       if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))      
       {      
           client.Credentials = new NetworkCredential(userName, password);      
       }      
    
       try      
       {      
            client.DownloadStringCompleted += (s, args) =>
            {
               // Messy error handling needed here, out of scope
               completed(args.Result);
            };
            client.DownloadStringAsync(new Uri(url));        
       }      
       catch     
       {      
          completed(null);      
       }      
    }      
    
    
    public void GetGamerDetails()              
    {              
        var xboxManager = XboxFactory.GetXboxManager("DarkV1p3r");              
        xboxManager.GetGamer( (xboxGamer) =>              
        {
             // Need to move to the main UI thread.
             Dispatcher.BeginInvoke(new Action<XboxGamer?>(DisplayGamerDetails), xboxGamer);
        });
    
    } 
    
    void DisplayGamerDetails(XboxGamer? xboxGamer)
    {
        if (xboxGamer.HasValue)              
        {              
            var profile = xboxGamer.Value.Profile[0];              
            imgAvatar.Source = new BitmapImage(new Uri(profile.ProfilePictureMiniUrl));              
            txtUserName.Text = profile.GamerTag;              
            txtGamerScore.Text = int.Parse(profile.GamerScore).ToString("G 0,000");              
            txtZone.Text = profile.PlayerZone;              
        }              
        else              
        {              
            txtUserName.Text = "Failed to load data";              
        }         
    }
    

    As you can see async programming can get realy messy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been experimenting with a lot of web development apps like Drupal, Moodle,
I've been experimenting with the SimpleServiceLocator , and I like it quite a bit,
I've been experimenting with Lucene on Google's App Engine and have run across index
I have been experimenting with woopra.com A web analytics tool. Which requires a piece
I have been experimenting with WPF and rendering strict XAML markup in a web
I have been experimenting with Lambda expressions in Oxygene. Very simple recursive lambda expression
What is the best compiler to experiment with C++0x features? I have been experimenting
We have been experimenting with using data visualisation techniques inspired by Edward Tufte to
I have been experimenting with using UUIDs as database keys. I want to take
I have been experimenting with functional programming and I still dont understand the concept.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.