MetroStyleApp入門 vol.20 色々なコントラクト

金曜日 , 8, 6月 2012 Leave a comment

(この記事はWindows 8 RP + VisualStudio2012RCでテストされています)

 

Contact Picker (連絡先ピッカー) コントラクト

 

 文字通り連絡先を共有するコントラクト。

 

宣言で連絡先ピッカーを追加。

デバッグして全然連絡先候補が出ない場合はこれが抜けている可能性が高いぞ!!

 

            // using Windows.ApplicationModel.Contacts;
            var picker = new ContactPicker();
            var res = await picker.PickSingleContactAsync();

 

 住所一覧から連絡先を選択できます。

 選択結果はContactInformationクラスが返ってくる。

 

カメラの利用

 

 コントラクトじゃないかも・・・

 なんかWindows Phoneのタスクなんかの印象で書いてしまった。

 

宣言に追加が必要

 

 

 

            CameraCaptureUI dialog = new CameraCaptureUI();
            Size aspectRatio = new Size(16, 9);
            dialog.PhotoSettings.CroppedAspectRatio = aspectRatio;

            StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
            if (file != null)
            {
                BitmapImage bitmapImage = new BitmapImage();
                using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read))
                {
                    bitmapImage.SetSource(fileStream);
                }
                CameraResult.Source = bitmapImage;
            }

 

 

 カメラの撮影はOS側に処理を任せるのはWindows Phoneと同じですね。

 

共有コントラクト(画像編)

 

 上記カメラの応用で、カメラで取得した画像を別アプリに転送する場合。

 送り手側のアプリはMetroStyleApp入門 vol11.共有コントラクトを利用するその1で紹介したようにDataTransferManagerを利用します。

 

 カメラ画像を処理するコードは以下のよう

 

            // カメラを起動する
            CameraCaptureUI dialog = new CameraCaptureUI();
            Size aspectRatio = new Size(16, 9);
            dialog.PhotoSettings.CroppedAspectRatio = aspectRatio;

            // 画像ファイルを取得
            this.file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);

            if (file != null)
            {
                // 画面表示とボタンコントロール
                BitmapImage bitmapImage = new BitmapImage();
                using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read))
                {
                    bitmapImage.SetSource(fileStream);
                }
                CameraResult.Source = bitmapImage;

                this.takePhotoButton.IsEnabled = false;
                this.shareButton.IsEnabled = true;
            }

 

画像を共有するコードは以下、

 

        private void shareButton_Click(object sender, RoutedEventArgs e)
        {
            DataTransferManager manager;
            manager = DataTransferManager.GetForCurrentView();

            manager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.DataRequested);

            DataTransferManager.ShowShareUI();
        }

        async private void DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            DataRequest request = args.Request;

            args.Request.Data.Properties.Title = "コントラクトサンプル";
            args.Request.Data.Properties.Description = "画像データを共有します";

            var fileStream = await file.OpenAsync(FileAccessMode.Read);
            request.Data.SetBitmap(RandomAccessStreamReference.CreateFromStream(fileStream));
 
        }

 

SetBitmapメソッドで画像を追加してあげます。
テキストを共有する場合は以下のようにSetTextでした。
args.Request.Data.Properties.Title = "眠るシーラカンスと水底のプログラマー";
    args.Request.Data.Properties.Description = "MetroStyleApp入門 vol11.共有機能を利用する";
    args.Request.Data.SetText("共有コントラクトの機能を紹介します");
受け取り側は、マニフェスト(Package.appxmanifest)で画像を共有することを宣言します。

 <a href="http://coelacanth.heteml.jp/blog/wp-content/uploads/2012/06/画像共有.png"><img class="alignnone size-large wp-image-1143" title="画像共有" src="http://coelacanth.heteml.jp/blog/wp-content/uploads/2012/06/画像共有-600x454.png" alt="" width="600" height="454" /></a>

 共有の対象として起動された場合はApp.xaml.csのOnShareTargetActivatedイベントハンドラーが呼び出されるので、そこで受っとった画像データを処理します。
        protected override void OnShareTargetActivated(Windows.ApplicationModel.Activation.ShareTargetActivatedEventArgs args)
        {
            var rootFrame = new Frame();
            rootFrame.Navigate(typeof(MainPage), args.ShareOperation);
            Window.Current.Content = rootFrame;
            Window.Current.Activate();
        }
arg.ShareOperationに共有データが格納されています。


ちょっとだけ、6月9日のCommunity Open Dayのセッションチラ見せでした。
<a href="http://atnd.org/events/28584" target="_blank">広島会場はATNDで登録</a>できますので、ぜひ参加してみてください。

 


Please give us your valuable comment

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

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