Getting Started with Windows Phone development-windows Azure mobile Service

Source: Internet
Author: User



Before using azure, I've been able to do only local apps, or use a third-party API, which is not particularly good, though it's mostly enough, but still can't manipulate the data at will. So after Azure was published, I tried to use Azure as my personal data center, and there were many options, but today I'm going to introduce you to Azure mobile service.



1. Create Mobile Service



Creating mobile service in Azure is simple, similar to creating other projects, with the following process:



I.






Ii.






Iii.



Ii









Here I am using Windows Azure International, a simple process to create a mobile service, it should be noted that the current service is still empty, we need to deploy the appropriate Web API service.



2. Deploying the Web API Service



The following are the empty mobile service dashboards created:









We select the section of the platform, choose Windows Phone, you can see the Getting Started Guide, you can choose any one, here we choose "Connect to an existing Windows Phone app."



Next you need to create and publish an ASP. NET Web API project to our mobile Service, here are two options:



1. Create New from Visual Studio, select Web API or Azure Mobile service template to









2. Or download a standard Azure Mobile service project Engineering from the Azure dashboard and unzip to local edit






Both of these methods will eventually get the same result, an ASP. NET Web API project, as follows:









If you are familiar with the ASP. NET Web API project, you can find the core controller code directly under the Controller folder, make a series of modifications, and generate your own method, as follows:






In the controller class you can see the relevant API, you can try the effect, the default is to return the JSON format data.


Get/tables/todoitem





Results:


[{"id": "1", "complete": false, "text": "First item"},{"id": "2", "complete": false, "text": "Second Item"}]





Below we simply run the build project and publish it to Azure Mobile Service, as follows:









When the release is complete, let's look at the effect of the operation as follows:









3. Using the mobile Service in a Windows Phone project



Open our Windows Phone project and use NuGet to installWindowsAzure.MobileServices SDK。这里要特别注意:Windows Azure Mobile Service SDK支持WinRT& Silverlight框架,所以基于WinRT的WP8.1项目完全可以使用。





To create a new data class Todoitem, the structure should be consistent with what is defined in the Web API project as follows:


public class TodoItem {
    public string Id { get; set; }
    public string Text { get; set; }
    public bool Complete { get; set; }
}


Create in App.xaml.csMobileServiceClient实例,初始化参数分别为applicationUri和applicationKey,参数你们替换为自己的即可如下


public static Mobileserviceclient Mobileservice = new Mobileserviceclient ("https:// azuretestmobileservice.azure-mobile.net/"," {Applicationkey} ");


Below we can get to the cloud data, upload, add and remove changes and other operations, and the results presented to the client UI, we can simply operate, as follows:



MainPage.xaml


 
 
<StackPanel>
            <ListView x:Name="TestDataList">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <CheckBox IsChecked="{Binding Complete}"/>
                            <TextBlock TextWrapping="Wrap" FontSize="30" Text="{Binding Text}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>

            </ListView>
            <Button x:Name="insertButton" Click="insertButton_Click">插入记录</Button>
            <TextBox x:Name="inputTextBox" ></TextBox>
        </StackPanel> 

MainPage.xaml.cs


 protected async override void OnNavigatedTo(NavigationEventArgs e)
        { await loadAzureData();
        } private async void insertButton_Click(object sender, RoutedEventArgs e)
        { if (inputTextBox.Text.Trim() != null && inputTextBox.Text.Trim() != "")
            {
                TodoItem newItem = new TodoItem { Complete = false, Text = inputTextBox.Text.Trim() }; await App.mobileService.GetTable<TodoItem>().InsertAsync(newItem);
            } await loadAzureData();
        } private async Task loadAzureData()
        { if (todoItems == null)
                todoItems = new List<TodoItem>();
            todoItems = await App.mobileService.GetTable<TodoItem>().ToListAsync();
            TestDataList.ItemsSource = todoItems;
        }


Finally, we have successfully deployed an instance of Azure Mobile service and integrated it into the Windows Phone 8&8.1 project





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.