In this project, you will build more onto our in-class WebServiceAndDatabaseDemo_PCL project that is using our local sample web service to get information about U.S. states.
To make it easier to switch between the various examples we're building into this app, please create a new ContentPage (non-XAML) called MenuPage and use this code:
using System;
using Xamarin.Forms;
namespace WebServiceAndDatabaseDemo_PCL
{
public class MenuPage : ContentPage
{
Button simpleStatesPageButton = null;
Button statesListPageButton = null;
Button singleStatePageButton = null;
public MenuPage ()
{
this.Title = "Menu";
this.simpleStatesPageButton = new Button () {
Text = "Simple"
};
this.statesListPageButton = new Button () {
Text = "States List"
};
this.singleStatePageButton = new Button () {
Text = "Single State"
};
this.Content = new StackLayout {
Children = {
this.simpleStatesPageButton,
this.statesListPageButton,
this.singleStatePageButton
}
};
this.simpleStatesPageButton.Clicked += OnSimpleStatesPageButtonClicked;
this.statesListPageButton.Clicked += OnStatesListPageButtonClicked;
this.singleStatePageButton.Clicked += OnSingleStatePageButtonClicked;
}
private async void OnSimpleStatesPageButtonClicked (object sender, EventArgs e)
{
await this.Navigation.PushAsync (new SimpleStatesPage (), true);
}
private async void OnStatesListPageButtonClicked (object sender, EventArgs e)
{
await this.Navigation.PushAsync (new StatesListPage (), true);
}
private async void OnSingleStatePageButtonClicked (object sender, EventArgs e)
{
await this.DisplayAlert ("TODO", "Navigate to SingleStatePage", "OK");
}
}
}
Use this page wrapped in a NavigationPage as the main content for the app in the constructor of the App class:
public App ()
{
// The root page of your application
this.MainPage = new NavigationPage(new MenuPage());
}
Also, set the Title property to "Simple" on the SimpleStatesPage and to "States List" on the StatesListPage. This will help the user to know which page is currently being viewed. You can do this in code or in the XAML for the pages.
Add a new page called StateDetailsPage to show the details for a state when the item is selected from the ListView on the StatesListPage.
Add a new page called SingleStatePage where the user can pick a particular state to get just that state's data.
/// <summary>
/// Converts the raw response to a StateViewModel instance.
/// </summary>
/// <returns>A StateViewModel instance.</returns>
/// <param name="data">The raw XML string.</param>
private StateViewModel ConvertRawResponseToStateViewModel(string data)
{
StateViewModel state = null;
if (!string.IsNullOrEmpty(data))
{
try
{
// Read the raw string into an XElement
XElement xml = XElement.Parse (data);
// Instantiate a viewmodel instance
state = new StateViewModel();
// Use LINQ to XML to get the data from the XML
state.Name = xml.Element("Name") != null ? xml.Element("Name").Value : string.Empty;
state.Abbreviation = xml.Element("Abbreviation") != null ? xml.Element("Abbreviation").Value : string.Empty;
state.Capital = xml.Element("Capital") != null ? xml.Element("Capital").Value : string.Empty;
// Web service has a typo in the DateOfStatehood element name; need to match it
state.DateOfStatehood = xml.Element("DateOfStathood") != null ? xml.Element("DateOfStathood").Value : string.Empty;
}
catch
{
// If there's an error, set the viewmodel to null
state = null;
}
}
return state;
}