Xamarin.Native Tutorial: Consuming WebService: Setup

Alright, let's consume a Webservice Next.

We will consume a sample RESTful webservice (made using ASP.NET WebAPI) from rabbit.miaw.xyz/api.

Setup

Install Newtonsoft.Json

First, open NuGet package manager. Install the package Newtonsoft.Json.

Create a Method to Get Data

Open Repository.cs.

Add this method:

 public async System.Threading.Tasks.Task<IEnumerable<Invoice>> GetInvoicesFromWebserviceAsync()
        {
            IEnumerable<Invoice> invoices = new List<Invoice>();

            using (HttpClient client = new HttpClient())
            {
                //client.BaseAddress = new Uri("http://rabbit.miaw.xyz/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage response = await client.GetAsync("http://rabbit.miaw.xyz/api/invoices");
                if (response.IsSuccessStatusCode)
                {
                    var result = await response.Content.ReadAsStringAsync();
                    invoices = JsonConvert.DeserializeObject<IEnumerable<Invoice>>(result);
                }
            }

            return invoices;
        }

...And reference the namespaces as necessary.

Alright, now we have setup our code, let's actually try actually consuming the webservice and show it the data to our users next.

UWP: In progress

Android: http://miaw.xyz/b/post/2017/09/20/xamarin-native-tutorial-consuming-webservice-android

iOS: In progress

 

 

 

 

Changing the Status Bar Colour in Android in Xamarin

If you're using AppCompatActivity and want to change the status bar colour to any colour you want, first you'll need to add this line inside your styles.xml:

<!-- Status Bar Colour -->
<item name="android:statusBarColor">@color/primary</item>

...And then, inside your Activity inside the OnCreate function, add this line:

Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);

 Congrats! Now your Status bar should have changed colour to @color/primary. Change the @color/primary to whatever colour you want.

RecyclerView Animation

https://proandroiddev.com/enter-animation-using-recyclerview-and-layoutanimation-part-1-list-75a874a5d213

Using Glide to load image into ImageView

Now, if you load images from external URLs into your ImageViews inside a RecyclerView (usually inside the OnBindViewHolder method), chances are your RecyclerView will be pretty sluggish upon first load.

This is simply because everything is fetched from the URL and loaded before the RecyclerView UI is updated.

So, let's use Glide, which not only solves that problem, but also automatically caches all images that you load inside an Android app!

Reference: 

https://github.com/beraybentesen/glide-xamarin-android

https://www.nuget.org/packages/Glide.Xamarin/3.8.0