Created
July 4, 2013 07:08
-
-
Save jakejscott/5925535 to your computer and use it in GitHub Desktop.
Xamarin async
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using Android.App; | |
using Android.Content; | |
using Android.Runtime; | |
using Android.Views; | |
using Android.Widget; | |
using Android.OS; | |
namespace YouMeThemAndroid | |
{ | |
[Activity (Label = "YouMeThemAndroid", MainLauncher = true)] | |
public class MainActivity : Activity | |
{ | |
protected override void OnCreate (Bundle bundle) | |
{ | |
base.OnCreate (bundle); | |
SetContentView (Resource.Layout.Main); | |
Button button = FindViewById<Button> (Resource.Id.myButton); | |
button.Click += async delegate { | |
var result = await Download(); | |
Console.WriteLine (result); | |
button.Text = result; | |
}; | |
} | |
public async Task<string> Download() | |
{ | |
HttpClient client = new HttpClient (); | |
var result = await client.GetStringAsync("http://ip.jsontest.com"); | |
return result; | |
} | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using MonoTouch.Foundation; | |
using MonoTouch.UIKit; | |
namespace YouMeThem | |
{ | |
public partial class YouMeThemViewController : UIViewController | |
{ | |
public YouMeThemViewController () : base ("YouMeThemViewController", null) | |
{ | |
} | |
public override void ViewDidLoad () | |
{ | |
base.ViewDidLoad (); | |
this.View.BackgroundColor = UIColor.Black; | |
UIButton button = new UIButton (new RectangleF (10, 10, 100f, 100f)) { | |
BackgroundColor = UIColor.Red | |
}; | |
button.SetTitle ("Click Me", UIControlState.Normal); | |
button.TouchUpInside += async delegate { | |
var result = await Download(); | |
Console.WriteLine (result); | |
button.SetTitle(result, UIControlState.Normal); | |
}; | |
this.Add (button); | |
} | |
public async Task<string> Download() | |
{ | |
HttpClient client = new HttpClient (); | |
var result = await client.GetStringAsync("http://ip.jsontest.com"); | |
return result; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment