Category : Android Cross Platform Mobile App CSharp iOS Visual Studio Windows Phone Xamarin
The short answer is ScrollView is a Layout. But it can only contain a single Visual Element in its Content property.
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.
Categories: Android, Cross Platform Mobile App, CSharp, iOS, Visual Studio, Windows Phone, Xamarin
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.
Unlike the older version, the new version is an inherited class from the Xamarin.Forms.Application class.
Summary of improvements
Addition of new life cycle events :
We can handle separate events during different phases of app interaction life cycle.
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.
1 2 3 |
Application.Current |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// The root page of your application MainPage = new ContentPage { Content = new StackLayout { VerticalOptions = LayoutOptions.Center, Children = { new Label { XAlign = TextAlignment.Center, Text = "Welcome to Xamarin Forms!" } } } }; |
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.
1 2 3 |
Application.Current.Properties.Add(new KeyValuePair<string, object>("AppOwner", "Nirmal")); |
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.
1 2 3 |
Application.Current.Resources.Add("AppCaption", "My App"); |
Happy coding 🙂
Categories: Android, Cross Platform Mobile App, iOS, Visual Studio, Windows Phone, Xamarin
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.
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.
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:
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 :
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
using Newtonsoft.Json; using System; using System.Net.Http; using System.Text; using System.Threading.Tasks; using Xamarin.Forms; namespace FeedShowingApp { public class App { static ListView lstPlaces = new ListView(); public static Page GetMainPage() { Button newButn = new Button() { Text = "Connect to Service", }; newButn.Clicked += newButn_Clicked; lstPlaces.ItemTemplate = new DataTemplate(typeof(TextCell)); lstPlaces.ItemTemplate.SetBinding(TextCell.TextProperty, "placeName"); return new ContentPage { Content = new StackLayout() { Children = { newButn, lstPlaces } } }; } static async void newButn_Clicked(object sender, EventArgs e) { GeoNamesWebService geoService = new GeoNamesWebService(); Place[] places= await geoService.GetPlacesAsync(); lstPlaces.ItemsSource = places; } } public class GeoNamesWebService { public GeoNamesWebService() { } public async Task<Place[]> GetPlacesAsync() { var client = new System.Net.Http.HttpClient(); client.BaseAddress = new Uri("http://api.geonames.org/"); var response = await client.GetAsync("postalCodeLookupJSON?postalcode=751010&country=IN&username=nirmalh"); var placesJson = response.Content.ReadAsStringAsync().Result; Placeobject placeobject = new Placeobject(); if(placesJson!="") { placeobject = JsonConvert.DeserializeObject<Placeobject>(placesJson); } return placeobject.places; } } public class Placeobject { [JsonProperty("postalcodes")] public Place[] places { get; set; } } public class Place { public string placeName { get; set; } } } |
Call using POST method
To call the web service in POST method, we will replace the GetAsync() method with PostAsync() method as follows
1 2 3 4 |
StringContent str = new StringContent("postalcode=751010&country=IN&username=nirmalh", Encoding.UTF8, "application/x-www-form-urlencoded"); var response = await client.PostAsync(new Uri("http://api.geonames.org/postalCodeLookupJSON"), str); |
Code for the POST method will look like
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
using Newtonsoft.Json; using System; using System.Net.Http; using System.Text; using System.Threading.Tasks; using Xamarin.Forms; namespace FeedShowingApp { public class App { static ListView lstPlaces = new ListView(); public static Page GetMainPage() { Button newButn = new Button() { Text = "Connect to Service", }; newButn.Clicked += newButn_Clicked; lstPlaces.ItemTemplate = new DataTemplate(typeof(TextCell)); lstPlaces.ItemTemplate.SetBinding(TextCell.TextProperty, "placeName"); return new ContentPage { Content = new StackLayout() { Children = { newButn, lstPlaces } } }; } static async void newButn_Clicked(object sender, EventArgs e) { GeoNamesWebService geoService = new GeoNamesWebService(); Place[] places= await geoService.GetPlacesAsync(); lstPlaces.ItemsSource = places; } } public class GeoNamesWebService { public GeoNamesWebService() { } public async Task<Place[]> GetPlacesAsync() { var client = new System.Net.Http.HttpClient(); client.BaseAddress = new Uri("http://api.geonames.org/"); StringContent str = new StringContent("postalcode=752020&country=IN&username=nirmalh", Encoding.UTF8, "application/x-www-form-urlencoded"); var response = await client.PostAsync(new Uri("http://api.geonames.org/postalCodeLookupJSON"), str); var placesJson = response.Content.ReadAsStringAsync().Result; Placeobject placeobject = new Placeobject(); if(placesJson!="") { placeobject = JsonConvert.DeserializeObject<Placeobject>(placesJson); } return placeobject.places; } } public class Placeobject { [JsonProperty("postalcodes")] public Place[] places { get; set; } } public class Place { public string placeName { get; set; } } } |
Happy coding 🙂
Categories: Cross Platform Mobile App, iOS, Visual Studio, Windows Phone, Xamarin