Esempio n. 1
0
        protected override async void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.Main);

            var client = new Client("Android");

            var input = FindViewById<EditText>(Resource.Id.Input);
            var messages = FindViewById<ListView>(Resource.Id.Messages);

            var inputManager = (InputMethodManager)GetSystemService(InputMethodService);
            var adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, new List<string>());

            messages.Adapter = adapter;

            await client.Connect();

            input.EditorAction +=
              delegate
              {
                  inputManager.HideSoftInputFromWindow(input.WindowToken, HideSoftInputFlags.None);

                  if (string.IsNullOrEmpty(input.Text))
                      return;

                  client.Send(input.Text);

                  input.Text = "";
              };

            client.OnMessageReceived +=
              (sender, message) => RunOnUiThread(() =>
                adapter.Add(message));
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);

            if (!_isRunning)
            {
                _isRunning = true;
                _userName = NavigationContext.QueryString["userName"] as string;

                var client = new Client(string.Format("WinPhone: {0}", _userName));

                client.OnMessageReceived +=
                (sender, message) => Dispatcher.BeginInvoke(() =>
                {
                    _messages.Add(message);
                });

                Task.Run(async () =>
                {
                    await client.Connect();
                });

                btnSend.Click += (s, args) =>
                {
                    if (string.IsNullOrEmpty(txtMessage.Text))
                        return;

                    client.Send(txtMessage.Text);
                    txtMessage.Text = "";
                };
            }
        }