Universal Windows Platform - Azure Mobile Service

Microsoft Azure Mobile Service gives you the power to create a cloud service mobile application. This will make your work more flexible and gives your application more portability in an efficient way. This is a short introduction of Azure Mobile service. You will get to know, how to create and integrate new mobile service in your new or existing application. So let’s get crack in Azure Mobile Service with Universal Windows Platform.

Create a New Mobile Service

Firstly, go to your azure portal. The select the Mobile Service option and click plus new icon in the bottom of the page.

new
                                             Figure 1: Create a new Mobile Service

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

start
                                    Figure 2: Connection Uri of Mobile Service.

Now, copy the code and paste it in the App.xaml.cs file of your application. You will get the additional instruction right above the code.

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

  1. classItem  
  2. {  
  3.     [JsonProperty(PropertyName = "id")]  
  4.     publicstring Id  
  5.     {  
  6.         get;  
  7.         set;  
  8.     }  
  9.   
  10.     [JsonProperty(PropertyName = "message")]  
  11.     publicstring Message   
  12.     {  
  13.         get;  
  14.         set;  
  15.     }  
  16.   
  17.     [JsonProperty(PropertyName = "location")]  
  18.     publicstring Location   
  19.     {  
  20.         get;  
  21.         set;  
  22.     }  
  23.   
  24.     [JsonProperty(PropertyName = "Date")]  
  25.     publicDateTime Date  
  26.     {  
  27.         get;  
  28.         set;  
  29.     }  
  30. }  
Listing: 1

Here we’ve created three attribute or you can say fieldsID, Message, Location and Date. For JsonProperty attribute, you need to install Newtonsoft.JsonNuGet package manager. Now, to insert, update and delete the data in your SQL table, we’ve to write little bit of code in .cs files.

Now, design the MainPage.xaml. We need a ListBox to display the data from our 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.         <ListBoxx:Name="listBox" Margin="10,10" RequestedTheme="Dark" FontSize="20" Background="White" Foreground="Black" BorderThickness="1" BorderBrush="Transparent">  
  4.             <ListBox.ItemContainerStyle>  
  5.                 <StyleTargetType="ListBoxItem">  
  6.                     <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>  
  7.                     </Style>  
  8.             </ListBox.ItemContainerStyle>  
  9.             <ListBox.ItemTemplate>  
  10.                 <DataTemplate>  
  11.                     <BorderBorderThickness="0,0,0,1" BorderBrush="#c0c0c0">  
  12.                         <StackPanel>  
  13.                             <TextBlock Foreground="Black" Text="{Binding Message}" />  
  14.                             <TextBlock Foreground="Black" Text="{Binding Location}" />  
  15.                             <TextBlock Foreground="Black" Text="{Binding Date}" />  
  16.                         </StackPanel>  
  17.                         </Border>  
  18.                 </DataTemplate>  
  19.             </ListBox.ItemTemplate>  
  20.             </ListBox>  
  21.     </StackPanel>  
  22. </ScrollViewer>  
  23.   
  24. Listing: 2 Put the code below in MainPage.cs above the constructor. ///  
  25.   
  26. <summary>  
  27.     /// Mobile Service ///  
  28. </summary>  
  29. privateMobileServiceCollection  
  30. <Item, Item> items; privateIMobileServiceTable  
  31.     <Item>itemTable = App.MobileService.GetTable  
  32.         <Item>(); Listing: 3 Now in constructor initialize MainPage_Loaded method and implement like below. publicMainPage() { this.InitializeComponent(); this.Loaded += MainPage_Loaded; } asyncvoidMainPage_Loaded(object sender, RoutedEventArgs e) { items = awaititemTable.ToCollectionAsync(); this.listBox.ItemsSource = items; }  
Listing: 4

Here, ‘listbox’ is the name of out ListBox control. So, we set item source as ‘items’, which set to Mobile Service tables values.

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

jam

Closure

Hopefully, you have understood the basics of Microsoft Windows Azure Mobile service and the implementation in Universal Windows Platform Application. This is only the basic, you can get deep knowledge by reading the documentation of Mobile Service and visit the Azure website to get the latest information about Mobile Service. 


Similar Articles