aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/csharp/HelloworldXamarin/Droid/MainActivity.cs
blob: 2c158e9d7ccd1bb52f47cb038da5781f053c4af2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using Android.App;
using Android.Widget;
using Android.OS;
using Grpc.Core;
using Helloworld;

namespace HelloworldXamarin.Droid
{
    [Activity(Label = "HelloworldXamarin", MainLauncher = true, Icon = "@mipmap/icon")]
    public class MainActivity : Activity
    {
        //int count = 1;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button>(Resource.Id.myButton);

            button.Click += delegate { SayHello(button); };
        }

            private void SayHello(Button button)
            {

              // use loopback on host machine: https://developer.android.com/studio/run/emulator-networking
              Channel channel = new Channel("10.0.2.2:50051", ChannelCredentials.Insecure);

              var client = new Greeter.GreeterClient(channel);
              string user = "Xamarin";

              var reply = client.SayHello(new HelloRequest { Name = user });

              button.Text = "Greeting: " + reply.Message;

              channel.ShutdownAsync().Wait();
            }
    }

    
}