protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); var context = TaskScheduler.FromCurrentSynchronizationContext(); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); var send = FindViewById<Button>(Resource.Id.Send); var host = FindViewById<Button>(Resource.Id.Host); var connect = FindViewById<Button>(Resource.Id.Connect); var listView = FindViewById<ListView>(Resource.Id.dataList); var text = FindViewById<EditText>(Resource.Id.sendText); var adapter = new MessageAdapter(); listView.Adapter = adapter; host.Click += (sender, e) => { _progress = ProgressSpinner.Show(this, null, null, true, false); _service.Host(new MultiplayerGame { Name = Build.Manufacturer.Trim() + " " + Build.Model.Trim(), }) .ContinueWith(t => { if (t.IsFaulted) { ShowPopUp("Error", t.Exception.InnerExceptions.First().Message); } else { ShowPopUp("Success", "You have hosted a game."); } }, context); }; connect.Click += (sender, e) => { _progress = ProgressSpinner.Show(this, null, null, true, false); _service.FindGames() .ContinueWith(t => { if (t.IsFaulted) { ShowPopUp("Error", t.Exception.InnerExceptions.First().Message); return; } AlertDialog dialog = null; var games = t.Result.Select(g => g.Name).ToArray(); var builder = new AlertDialog.Builder(this); builder.SetTitle("Select Connection"); builder.SetSingleChoiceItems(games, -1, (s, o) => { var game = t.Result.ElementAtOrDefault(o.Which); if (game != null) { _progress = ProgressSpinner.Show(this, null, null, true, false); dialog.Dismiss(); _service.Join(game).ContinueWith(c => { if (c.IsFaulted) { ShowPopUp("Error", c.Exception.InnerExceptions.First().Message); } else { ShowPopUp("Success", "You have connected to the game."); } }, context); } }); dialog = builder.Create(); dialog.Show(); _progress.Dismiss(); }, context); }; send.Click += (sender, e) => { var message = new TestMessage { Text = text.Text, ShouldEcho = true, TimeStamp = DateTime.Now }; _service.Send("T", message).ContinueWith(t => { if (t.IsFaulted) { ShowPopUp("Error", t.Exception.InnerExceptions.First().Message); return; } }, context); }; _service.Received += (sender, e) => { var message = e.Message as TestMessage; if (message.ShouldEcho) { adapter.Logs.Add("RECEIVED: " + message.Text); adapter.NotifyDataSetChanged(); message.ShouldEcho = false; _service.Send("T", message); } else { adapter.Logs.Add("Send: " + message.Text + " Latency: " + message.RoundTripMillis); } }; }
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); var context = TaskScheduler.FromCurrentSynchronizationContext(); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); var send = FindViewById <Button>(Resource.Id.Send); var host = FindViewById <Button>(Resource.Id.Host); var connect = FindViewById <Button>(Resource.Id.Connect); var listView = FindViewById <ListView>(Resource.Id.dataList); var text = FindViewById <EditText>(Resource.Id.sendText); var adapter = new MessageAdapter(); listView.Adapter = adapter; host.Click += (sender, e) => { _progress = ProgressSpinner.Show(this, null, null, true, false); _service.Host(new MultiplayerGame { Name = Build.Manufacturer.Trim() + " " + Build.Model.Trim(), }) .ContinueWith(t => { if (t.IsFaulted) { ShowPopUp("Error", t.Exception.InnerExceptions.First().Message); } else { ShowPopUp("Success", "You have hosted a game."); } }, context); }; connect.Click += (sender, e) => { _progress = ProgressSpinner.Show(this, null, null, true, false); _service.FindGames() .ContinueWith(t => { if (t.IsFaulted) { ShowPopUp("Error", t.Exception.InnerExceptions.First().Message); return; } AlertDialog dialog = null; var games = t.Result.Select(g => g.Name).ToArray(); var builder = new AlertDialog.Builder(this); builder.SetTitle("Select Connection"); builder.SetSingleChoiceItems(games, -1, (s, o) => { var game = t.Result.ElementAtOrDefault(o.Which); if (game != null) { _progress = ProgressSpinner.Show(this, null, null, true, false); dialog.Dismiss(); _service.Join(game).ContinueWith(c => { if (c.IsFaulted) { ShowPopUp("Error", c.Exception.InnerExceptions.First().Message); } else { ShowPopUp("Success", "You have connected to the game."); } }, context); } }); dialog = builder.Create(); dialog.Show(); _progress.Dismiss(); }, context); }; send.Click += (sender, e) => { var message = new TestMessage { Text = text.Text, ShouldEcho = true, TimeStamp = DateTime.Now }; _service.Send("T", message).ContinueWith(t => { if (t.IsFaulted) { ShowPopUp("Error", t.Exception.InnerExceptions.First().Message); return; } }, context); }; _service.Received += (sender, e) => { var message = e.Message as TestMessage; if (message.ShouldEcho) { adapter.Logs.Add("RECEIVED: " + message.Text); adapter.NotifyDataSetChanged(); message.ShouldEcho = false; _service.Send("T", message); } else { adapter.Logs.Add("Send: " + message.Text + " Latency: " + message.RoundTripMillis); } }; }