How To Add Azure Mobile App To An UWP App

In this article, you will go step by step to create Azure Mobile App using Azure Portal and Insert data from Universal Windows Platform App. Mobile Apps come under Azure App Service. Azure App Service is a fully managed Platform as a Service (PaaS) offering for professional developers that brings a rich set of capabilities to web, mobile, and integration scenarios. Mobile Apps offer a highly scalable, globally available mobile application development platform for Enterprise Developers and System Integrators that brings a rich set of capabilities to mobile developers. Mobile App features:

  • Build native and cross platform apps

    • Building native Windows, iOS, Android apps
    • Cross platform Xamarin or Cordova

  • Connect to your enterprise systems – With the help of this feature, you can add corporate sign on option to connect with on-premises or cloud resources. Azure Active Directory is available for identity providers for enterprise authentication, also, social providers, such as Facebook, Twitter and Microsoft account.

  • Build offline-ready apps with data sync – Develop app using this feature to save data offline as well as sync data with the cloud to get the latest data or records in the background when the connectivity is available. This service allows you to easily integrate with other NoSQL and SQL data providers, including Azure Table Storage, MongoDB, DocumentDB and SaaS API providers like Office 365 and Salesforce.com

  • Push Notifications to millions in seconds - Engage your customers with instant push notifications on any device, personalized to their needs, sent when the time is right.

Prerequisites

  • Microsoft Azure Subscription (MSDN subscribers or sign up for one month free trial )
  • Visual Studio 2015 with UWP SDK – For Download Click here
  • .NET Azure SDK for Visual Studio 2015 – For Download Click here

You will learn,

  • How to Create Azure Mobile App
  • How to Create Database & Server
  • How to Develop UWP App
  • Add Mobile App to Existing UWP App

Create Azure Mobile App

Step 1:
Navigate to Azure Portal & Sign in with Azure Credentials.

Step 2: Click on New button -> Web + Mobile -> Mobile App

New

Step 3: Enter Mobile App Name, Choose Azure Subscription, Create new or use existing Resource Group.

Mobile App
Click on App Service Plan/Location -> Create New.

Create New

Enter App Service Plan name, Location, Pricing tier.

App Service Plan
App Service Plan
Step 4: Mobile App is created successfully. Click on “Easy tables” inside Mobile section. Click on Easy Table Configuration option.

Configuration

Configuration

Step 5: First, select “Connect to database“ option. Add new data connection by clicking on “Add” button.

Add

Add

Step 6: Choose Type of Database: SQL Database or Storage. Click on SQL Database & create a new database.

Choose Type

Step 7: Enter Database name, Pricing Tier, Target Server, Collation. No server created as of now; so please select Target server & create a new Server. Enter Server name, admin login, password, location.

Database

Step 8: Select last option of database “Connection string” option. Just check if all the data filled is correct or not.

Connection string

Wait for few seconds. The database & server are being created.

database

Step 9: Database & Server has been created in Easy Tables. Now, check on “I acknowledge that this will overwrite all site contents” & click on “Initialize App” button.

Initialize App
Step 10: Click on Add button to create table in database.

Add

Step 11: Enter table name & all permissions: Insert, Update, Delete, Read, Undelete.

table
For this example, we are considering “Allow anonymous access”.

Step 12: Click on Table Name.

Table Name

Next step is to create columns or fields in table, so click on “Manage Schema”.

Manage Schema
Step 13: Add columns,

  • Column name : firstname , Type : String
  • Column name : lastname , Type : String
  • Column name : city , Type : String

     Add a columns

Create UWP App

Step 14: Start Visual Studio 2015 & Create New Project.

New Project

Step 15: First, select Universal templates & then select Blank App (Universal Windows).
Blank App

Note: if Windows Universal Platform SDK is not installed, then this options will not be available. So, please check when the Visual Studio installation is going on.

Select Target Version of UWP Project

Version

Step 16: To deploy or run a UWP App, it’s required to enable the Developer Mode. So, please follow the below steps to enable the same.

Mode

Mode

Mode

Now, the UWP app is ready to add Azure Mobile app.

UWP app

Step 17: Open MainPage.xaml file & add the below code for app designing or UI part.

Open MainPage.xaml file

  1. <StackPanel Margin="12">  
  2.     <TextBlock Text="Azure Mobile App - UWP App" FontSize="22" Margin="12" />  
  3.     <TextBox x:Name="txtFirstName" PlaceholderText="first name" />  
  4.     <TextBox x:Name="txtLastName" PlaceholderText="last name" Margin="0,20,0,0" />  
  5.     <TextBox x:Name="txtCity" PlaceholderText="city" Margin="0,20,0,0" />  
  6.     <Button x:Name="btnSave" Content="Save" Tapped="btnSave_Tapped" Margin="0,20,0,0" HorizontalAlignment="Stretch" />   
  7. </StackPanel>  
Step 18: Right click on Project Name & select “Manage NuGet Packages…”

Manage NuGet Packages

Select “Browse” tab. Search for “Azure Mobile Client” & install the packages.

Browse

Browse

Browse

Step 19: After adding Azure Mobile App NuGet Packages, open App.xaml.cs.

Azure Mobile App NuGet Packages

Add reference:

  1. using Microsoft.WindowsAzure.MobileServices;  
  2. Also add MobileServiceClient  
  3. //Please change Mobile App name  
  4. public static MobileServiceClient MobileService = new MobileServiceClient("https://<azuremobileappname>.azurewebsites.net");  
Step 20: Again, right click on Project Name & Add new class. Please mention the same name of the class as given to table name on Azure.

add

add

code
  1. public class usertable {  
  2.     public string id {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string firstname {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public string lastname {  
  11.         get;  
  12.         set;  
  13.     }  
  14.     public string city {  
  15.         get;  
  16.         set;  
  17.     }  
  18. }  
Step 21: Open MainPage.xaml.cs file.

code

Add references
  1. using Microsoft.WindowsAzure.MobileServices;  
  2. using Windows.UI.Popups;  
  3. IMobileServiceTable < usertable > userTableObj = App.MobileService.GetTable < usertable > ();  
  4. private void btnSave_Tapped(object sender, TappedRoutedEventArgs e) {  
  5.     try {  
  6.         usertable obj = new usertable();  
  7.         obj.firstname = txtFirstName.Text;  
  8.         obj.lastname = txtLastName.Text;  
  9.         obj.city = txtCity.Text;  
  10.         userTableObj.InsertAsync(obj);  
  11.         MessageDialog msgDialog = new MessageDialog("Data Inserted!!!");  
  12.         msgDialog.ShowAsync();  
  13.     } catch (Exception ex) {  
  14.         MessageDialog msgDialogError = new MessageDialog("Error : " + ex.ToString());  
  15.         msgDialogError.ShowAsync();  
  16.     }  
  17. }  
Step 22: Now, run the app.

app
app

Navigate to Mobile App's Easy Tables on Azure.


Navigate

Congratulations from UWP! The app data is successfully saved on Azure, using Mobile App.

 

Up Next
    Ebook Download
    View all
    Learn
    View all