Skip to content

Instantly share code, notes, and snippets.

@fabianwilliams-zz
Created August 11, 2015 18:35
Show Gist options
  • Save fabianwilliams-zz/3eae5ea16c0e67cb9e25 to your computer and use it in GitHub Desktop.
Save fabianwilliams-zz/3eae5ea16c0e67cb9e25 to your computer and use it in GitHub Desktop.
Steps to Accomplish Xamarin.Forms with Azure
Steps with Explanatory Notes
Section 1 - Create or Attach to a Windows Azure Mobile Service URI
1. Log into https://manage.windowsazure.com
2. Click on the option to create an Azure Mobile Service (WAMS) if the one you want to use does not exist otherwise just click on the one you will be using in this process. By default you get a ToDo Items Entity and Controller
3. Optional – Adjust your Data Objects(Entitys Add, Edit, etc)
4. Optional – Adjust your Controllers (CRUD-Q Capabilities)
5. Publish your WAMS URI back to Azure
6. Record your URI endpoint and Client ID
Section 2 – Create a Xamarin.Forms Project (Shared or PCL, PCL is the default option)
1. Decision Point –
a. If you create your Xamarin.Forms Project in Visual Studio (Windows OS), then you will get 3 Platforms to build against. [Windows Phone, iOS, Android]
b. If you create your Xamarin.Forms Project in Xamarin Studio (Mac OS), then you get 2 Platforms to build against. [iOS and Android]
2. Create your Xamarin.Forms Project
3. Update your Packages in all your Project within the Solution
4. Add the following Packages to your individual Projects
a. Azure Mobile Services (there are dependencies that come with it)
b. Azure Mobile Services SQLiteStore (there are overlapping and new dependencies here as well)
5. You will get 3 Errors based on conflicting References
a. Error CS1703: An assembly `System.IO' with the same identity has already been imported. Consider removing one of the references (CS1703) (meetup_prep1.iOS)
b. Error CS1703: An assembly `System.Runtime' with the same identity has already been imported. Consider removing one of the references (CS1703) (meetup_prep1.iOS)
c. Error CS1703: An assembly `System.Threading.Tasks' with the same identity has already been imported. Consider removing one of the references (CS1703) (meetup_prep1.iOS)
6. Click Edit References in your Project and then .Net Assembly and remove the offending assemblies. If you type “System.” It is usually the 1st 3 that come up in a search
Section 3 – Create your Data Services for Access to the Underlying WAMS Data
1. Create a Class File in your PCL or Shared Project called “BeerService.cs”
a. Add References to the Following Assembly
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.MobileServices;
using Microsoft.WindowsAzure.MobileServices.SQLiteStore;
using Microsoft.WindowsAzure.MobileServices.Sync;
2. You can remove the default Constructor or leave it there
3. Create a private method for the mobile services client
private MobileServiceClient MobileService = new MobileServiceClient(
"https://[service name].azure-mobile.net/",
"[service key]"
);
4. Create an Entity/Model that represents the Beer from the WAMS solution this time also add a string for Id. This was taken care of in the WAMS solution by ServiceTable Operations on the EF
public string Id { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public double Abv { get; set; }
public double Ibu { get; set; }
public string Taste { get; set; }
public string ProName { get; set; }
public string Smu {get; set;}
5. Create a List Instance of Type Beer to hold the values being returned from the Data Service
public List<Beer> BeerItem { get; private set;}
6. Optional - Create a Mobile Services SyncTable to persist information to the SQL Client
private IMobileServiceSyncTable<Beer> beerTable;
7. Create an Asyc Task Initialize Method to make the initial Connection to the WAMS Data Store
public async Task InitializeAsync()
{
var store = new MobileServiceSQLiteStore("localdata.db");
store.DefineTable<Beer> ();
await this.MobileService.SyncContext.InitializeAsync(store);
beerTable = this.MobileService.GetSyncTable<Beer>();
}
8. Create an Async Task to do Syncing on Demand
public async Task SyncAsync()
{
// Comment this back in if you want auth
//if (!await IsAuthenticated())
// return;
await InitializeAsync();
await this.MobileService.SyncContext.PushAsync();
var query = beerTable.CreateQuery()
//.Where(mb => mb.OnTapYN == true)
;
await beerTable.PullAsync("Beers", query);
}
9. Create an Async Task Method to get back all Valid Beers
public async Task<List<Beer>> GetAllValidBeers()
{
try {
await SyncAsync();
BeerItem = await beerTable
.Where (ot => ot.Abv > 0).ToListAsync ();
} catch (MobileServiceInvalidOperationException e) {
Console.Error.WriteLine (@"ERROR {0}", e.Message);
return null;
}
return BeerItem;
}
Section 4 – House Keeping Duties in iOS Project
10. Add Reference to Xamarin.Forms
11. Initialize the Mobile Service and SQL Lite Store inside the FinishLaunching Method
Microsoft.WindowsAzure.MobileServices.CurrentPlatform.Init();
SQLitePCL.CurrentPlatform.Init();
Section 5 – Create your Home Screen that will Display a ListView of the Items
1. Create if you have not already a page called HomePage.cs
a. Add the below Using Statements
using Xamarin.Forms;
using System.Threading.Tasks;
using System.Collections.Generic;
b. Inhertit from ContentPage
2. Create an Instance of your Beer Service, wire up a List for Beers and create a ListView for the Screen
BeerService bs = new BeerService ();
public List<Beer> BeerItem { get; private set;}
ListView lv = new ListView ();
3. Create a Method to OverRide the OnAppearing so we can make sure we are connected to the WAMS and then Wire Up the ListView for Content in the Refresh Async Method
protected async override void OnAppearing()
{
base.OnAppearing ();
await this.RefreshAsync ();
}
public async Task RefreshAsync()
{
BeerItem = await bs.GetAllValidBeers();
lv.ItemsSource = BeerItem;
var cell = new DataTemplate (typeof(TextCell));
cell.SetBinding(TextCell.TextProperty,"Name");
lv.ItemTemplate = cell;
//lv.ItemTemplate = new DataTemplate (typeof(BeerCell));
}
4. Finally Create a StackLayout with the Child being the ListView
this.Title = "All my JailBreak Beers";
this.Content = new StackLayout {
Padding = new Thickness(0, Device.OnPlatform(20,0,0), 0,0),
Orientation = StackOrientation.Vertical,
Children = {
lv
}
};
5. TIME TO TEST. YES. TIME TO TEST. TEST ON ALL DEVICE PROJECTS
SECTION 6 – Turn this into a Tabbed Application to make a Add Beer Page
6. Create a page called Master Page that will be a Tabbed Page
7. Create a page called AddBeer that will serve to add new Beers
8. Change the SharedProject Start Page to point to Master Page now
9. TEST TEST TEST on all platforms
SECTION 7 – Add Method to Beer Service to Add Beer
10. Add method
public async Task InsertBeerAsync (Beer bevvy)
{
try
{
await SyncAsync();
await beerTable.InsertAsync(bevvy);
await SyncAsync();
}
catch (MobileServiceInvalidOperationException e)
{
//soemethign good here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment