Windowsストアアプリ入門 vol68:Windows Azure Mobile Servicesを利用する(その1)

日曜日 , 30, 9月 2012 Leave a comment

 Windows Azure Mobile Servicesの告知を参考にWindows Azure Mobile Servicesを利用してみる。

 

アカウントの取得

 

 まずは体験版でアカウントの取得。

 以下のサイトでトライアル版のWindows Azureアカウントを取得します。

 https://www.windowsazure.com/en-us/pricing/free-trial/

 

 

90日試用に申し込む。

入力フォームに情報を入力(要クレジットカード)

登録完了

 

 以前とUIが変わっていますね。

 (このぐらいの入力ならいいですが、あんまりUIは変えてほしくないですね)

 

Mobile Serviceを利用する

 

 Accountタブのpreview featuresに移動します。

 

 

 有効になってるなぁ。

 ポータルに移動してMobileServiceのアプリケーションを作成します。

 「Create Aew App」をクリック。

 

アカウントが作成されました。

(フローはGet started with Mobile Services(英語)で確認できます)

 

使ってみる

 

 Get started with Mobile Services(英語)を参考に簡単なアプリケーションを作成してみます。

 アプリ名をクリックすると以下のようなページに遷移します。

 

 「Connect an existing Windows Store app」をクリックします。

 

 

 

Mobile Services SDKをインストールする

 

 Install Mobile Services SDKリンクよりSDKをインストールします。

 

 

Windowsストアアプリで利用する

 

 SDKのインストールが終了したらWindowsストアアプリのプロジェクトを作成します。

 参照の追加で「Windows Azure Mobile Services Managed Client」を追加します。

 

 

 App.xaml.csに以下のusingを追加します。

 

using Microsoft.WindowsAzure.MobileServices;

 

 サイトの指示に合わせて以下の変数を追加します。

 

        public static MobileServiceClient MobileService = new MobileServiceClient(
                                                                  "{URL}",
                                                                  "{キー}"
                                                              );

 

MobileService側でテーブルを追加

 

 一旦MobileServiceのWebページに戻ってDBのテーブルを作成します。

 サンプルでは自動でテーブルを追加するボタンがあるので今回はそのままそれを利用します。

 

 

 DATAタブを確認するとテーブルが追加されていることがわかります。

 

 

Windowsストアアプリから利用する

 

 再びVisualStudioに戻ります。

 MainPage.xaml.csに以下のようにMobileServiceを利用するコードを追加します。

 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// 空白ページのアイテム テンプレートについては、http://go.microsoft.com/fwlink/?LinkId=234238 を参照してください

namespace MobileServiceSample
{
    /// <summary>
    /// それ自体で使用できる空白ページまたはフレーム内に移動できる空白ページ。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// このページがフレームに表示されるときに呼び出されます。
        /// </summary>
        /// <param name="e">このページにどのように到達したかを説明するイベント データ。Parameter 
        /// プロパティは、通常、ページを構成するために使用します。</param>
        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            Item item = new Item { Text = "Awesome item" };
            await App.MobileService.GetTable<Item>().InsertAsync(item);
        }
    }

    public class Item
    {
        public int Id { get; set; }
        public string Text { get; set; }
    }
}

 

 ここでプロジェクトを実行してみます。

 実行後にMobileServiceのサイトでデータを確認すると以下のようにデータが入っています。

 

 

 かなり使いやすいですね。ラーニングでは認証とプッシュ通知もあるので、そちらも試してレビューしてみたいと思います。

 


Please give us your valuable comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください