Fundamentals Of Microsoft Azure Mobile Service

Azure

Microsoft Azure Mobile Service gives us the authority to create a Cloud Service Mobile Application. This will leverage us with more flexibility and make an Application more portable.

This is a short introduction of Azure Mobile Service. Now, let’s create and integrate new mobile Service with new or an existing Application. Thus, let’s understand Azure Mobile Service and I am sure, it would mesmerize the users.

Let’s create a new mobile Service

First go to your Azure portal and select the Mobile Service option. Click plus new icon in the bottom of the page.

Azure

Create a new Mobile Service.

After creating Mobile Service, open the Mobile Service and select the dropdown option “CONNECT AN EXISTING WINDOWS OR WINDOWS PHONE APP” below the Get Started option.

Azure

Now, copy the code and paste it in the App.xaml.cs file of your Application. Please focus on the additional instruction, which is right above the code.

Now, create a new class named MyItems, which will make all the necessary tables of your Mobile Service. 

  1. class MyItems  
  2. {  
  3.     [JsonProperty(PropertyName = "id")]  
  4.     public string Id { get; set; }  
  5.   
  6.     [JsonProperty(PropertyName = "message")]  
  7.     public string Message { get; set; }  
  8.   
  9.     [JsonProperty(PropertyName = "location")]  
  10.     public string Location { get; set; }  
  11.   
  12.     [JsonProperty(PropertyName = "Date")]  
  13.     public DateTime Date { get; set; }  
  14. }   

I have created four attributes namely fieldsID, Message, Location and Date. Please remember for JsonProperty attribute, which we have to install ‘Newtonsoft.JsonNuGet package manager’.

Now, to perform any CRUD operation like insert, update and delete the data in your SQL table, let’s write the code in .cs files.

To begin with, let’s create MainPage.xaml. We need a ListBox to display the data from out Mobile Service table. As there are multiple attributes and columns, here we re showing only Message, Location and Date values. 

  1. <ScrollViewer>  
  2.     <StackPanel Margin="10,10,0,0">  
  3.         <ListBox x:Name="listBox"  
  4.                         Margin="10,10"  
  5.                         RequestedTheme="Dark"  
  6.                         FontSize="20"  
  7.                         Background="White"  
  8.                         Foreground="Black" BorderThickness="1"  
  9.                         BorderBrush="Transparent" >  
  10.             <ListBox.ItemContainerStyle>  
  11.                 <Style TargetType="ListBoxItem">  
  12.                     <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>  
  13.                 </Style>  
  14.             </ListBox.ItemContainerStyle>  
  15.             <ListBox.ItemTemplate>  
  16.                 <DataTemplate>  
  17.                     <Border BorderThickness="0,0,0,1" BorderBrush="#c0c0c0">  
  18.                         <StackPanel>  
  19.                             <TextBlock Foreground="Black" Text="{Binding Message}" />  
  20.                             <TextBlock Foreground="Black" Text="{Binding Location}" />  
  21.                             <TextBlock Foreground="Black" Text="{Binding Date}" />  
  22.                         </StackPanel>  
  23.                     </Border>  
  24.                 </DataTemplate>  
  25.             </ListBox.ItemTemplate>  
  26.         </ListBox>  
  27.     </StackPanel>  
  28. </ScrollViewer>   

Listing: 2

Put the code given below in MainPage.cs, which is above the constructor.

  1. /// <summary>  
  2. /// Mobile Service  
  3. /// </summary>  
  4. private MobileServiceCollection<Item, Item> Myitems;  
  5. private IMobileServiceTable<Item> itemTable = App.MobileService.GetTable<Item>();   

Listing: 3

Now, in constructor; initialize MainPage_Loaded method and implement, as shown below. 

  1. public MainPage()  
  2. {  
  3.     this.InitializeComponent();  
  4.     this.Loaded += MainPage_Loaded;  
  5. }  
  6.   
  7. async void MainPage_Loaded(object sender, RoutedEventArgs e)  
  8. {  
  9.     Myitems = await itemTable.ToCollectionAsync();  
  10.     this.listBox.ItemsSource = Myitems;  
  11. }   

Listing: 4

Here, listbox is the name of out ListBox control. Thus, we set an item source as Myitems, which sets Mobile Service table’s values.

If you run the Application, it will show all the data in the ListBox control.

Conclusion

I hope, you understood the basics of Microsoft Windows Azure Mobile Service. Till next time, keep learning.

Up Next
    Ebook Download
    View all
    Learn
    View all