Using a Scrollview in XamarinForms

What is it ???

The short answer is ScrollView is a Layout. But it can only contain a single Visual Element in its Content property.

Why So ???

Because, the Layouts such as Stack, relative etc. are derived from Layout<T> generic class. This Layout<T> generic class is derived from the Layout class, which is a non-generic class.

ScrollView is inherited from the Non-generic Layout class. That is the same reason, why it does not support a generic way to add children. It only exhibits the Content property to accept a single view or a Layout.

ScrollView

Usage by example

Usage Example #1 : Let’s display a paragraph of scrolling text

Categories: Android, Cross Platform Mobile App, CSharp, iOS, Visual Studio, Windows Phone, Xamarin

Customizing Listview display in Xamarin

As the usual behavior of ListView to display the items in a list and it contains a single column. The ItemSource property of Listview accepts the list of data to be displayed and render the same on screen.

The code for a simple list to display the Students of a class would like as in the following pic.

ListViewInXamarinForms

Now let’s customize this ListView in a way to display the Student Name along with the Class and School they belongs to.

ListViewItem

As in the image above, the data contains a single record. So to accommodate the data in a single record, Let’s create a Student model class.

In order to customize the ListView display, we need to override the ViewCell class.

Categories: Android, Cross Platform Mobile App, iOS, Visual Studio, Windows Phone, Xamarin

What’s new in Xamarin.Forms 1.3 : Inherited App Class

With the new release of Xamarin.Forms 1.3 version, the App Class has its new Avatar.

The Older version of App Class was just a class with static methods.
OldXamarinAppClass

Unlike the older version, the new version is an inherited class from the Xamarin.Forms.Application class.
NewXamarinAppClass

Summary of improvements

Addition of new life cycle events :

We can handle separate events during different phases of app interaction life cycle.

  • onStart :- Allows us to handle the app start up. We can write code here to get executed during on app start up.
  • onSleep :- We can write code here to get executed while app in entering into the sleep/minimize mode.
  • onResume :- Code can be executed after the app regains the focus.

Self static object :
Application class uses a static instance of its own, which is named as Current. It can be accessed through out the application.

MainPage attribute :
MainPage is an attribute of the Application Class of type ContentPage. As the name suggest, we can assign the root page of the application to this attribute.

Properties :
Not to be confused, this is an attribute of the Application Class. It contains the Dictionary object. We can read/write application level global variables to it for further use in a Key and Value pair format.

Resources :
Application Class Resources attribute can store the resources for application use. This attribute is of type ResourceDictionary.We can add Implicit Styles (in XAML) and Key Value pair strings to it.

Happy coding 🙂

Categories: Android, Cross Platform Mobile App, iOS, Visual Studio, Windows Phone, Xamarin

Consuming RESTFul web Service in XamarinForms project Using HttpClient

Web service is an important part of the mobile app. I am trying to consume a RESTFul web service in Xamarin using HttpClient.

Prerequisites :
– Microsoft.Net.Http
– Newtonsoft.Json

In this example, let’s display the name of the places for a given postal code. We will use the http://api.geonames.com webservice to get the places.

Call using GET method

Now let’s create the UI having a Button and a Listview in it. Button will contain the code to fetch the web service on click event and Listview will display the result.

AppClass

We are with the UI, which is equipped with the Listview and the Button. The newButn_Clicked() method is associated with the Button click event to get the data from the Web service. Now, let’s define the code to fetch data from the WebService. Since webservice will return JSON we will decode the JSON using the Newtonsoft.Json.

Webservice class

As mentioned above, webservice will return the JSON and we have used the DeserializeObject<>() method to deserialize the JSON to a model object. Model class would look like as:

Model

The PlaceObject class will get the array of Place class after the JSON deserization process. So we have marked the places attribute as the array name returned by the web service i.e. “postalcodes”.

We can now run the code to see the data fetched from the webservice in separate platforms.

Collectively the code looks like :

Call using POST method

To call the web service in POST method, we will replace the GetAsync() method with PostAsync() method as follows

Code for the POST method will look like

Happy coding 🙂

Categories: Cross Platform Mobile App, iOS, Visual Studio, Windows Phone, Xamarin

%d bloggers like this: