MetroStyleApp入門 vol4.アカウント情報の取得、設定

火曜日 , 3, 4月 2012 Leave a comment

(この投稿はWindows8 Consumer Preview + VisualStudio11betaで動作検証されています)

 

今回はログインアカウントの名前や画像を取得する方法を紹介します。

 

            // 名前を取得します
            this.getNameButton.Click += (e, s) =>
                {
                    this.nameText.Text = UserInformation.DisplayName;
                };

            // 名を取得します
            this.getFirstNameButton.Click += (e, s) =>
                {
                    this.firstNameText.Text = UserInformation.FirstName;
                };

            // 姓を取得します
            this.getLastNameButton.Click += (e, s) =>
                {
                    this.lastNameText.Text = UserInformation.LastName;
                };

            // 画像を取得します
            this.getImageButton.Click += async (e, s) =>
                {
                    StorageFile image = Windows.System.UserProfile.UserInformation.GetAccountPicture(Windows.System.UserProfile.AccountPictureKind.LargeImage) as StorageFile;
                    if (image != null)
                    {
                        IRandomAccessStream imageStream = await image.OpenReadAsync();
                        BitmapImage bitmapImage = new BitmapImage();
                        bitmapImage.SetSource(imageStream);
                        this.accountImage.Source = bitmapImage;
                    }
                };

            // 画像を保存します
            this.setImageButton.Click += async (e, s) =>
                {
                    FileOpenPicker imagePicker = new FileOpenPicker
                    {
                        ViewMode = PickerViewMode.Thumbnail,
                        SuggestedStartLocation = PickerLocationId.PicturesLibrary,
                        FileTypeFilter = { ".jpg", ".jpeg", ".png", ".bmp" }
                    };

                    StorageFile imageFile = await imagePicker.PickSingleFileAsync();
                    if (imageFile != null)
                    {
                        Windows.System.UserProfile.SetAccountPictureResult result = await Windows.System.UserProfile.UserInformation.SetAccountPicturesAsync(null, imageFile, null);
                        if (result == Windows.System.UserProfile.SetAccountPictureResult.Success)
                        {
                            this.setImageResultText.Text = "画像を変更しました";
                        }
                        else
                        {
                            this.setImageResultText.Text = "画像変更に失敗しました";
                        }
                    }

                    StorageFile image = Windows.System.UserProfile.UserInformation.GetAccountPicture(Windows.System.UserProfile.AccountPictureKind.LargeImage) as StorageFile;
                    if (image != null)
                    {
                        IRandomAccessStream imageStream = await image.OpenReadAsync();
                        BitmapImage bitmapImage = new BitmapImage();
                        bitmapImage.SetSource(imageStream);
                        this.accountImage.Source = bitmapImage;
                    }
                };

UserInformationで色々なアカウント情報を取得できます。

Skydrive上の画像をアカウント画像に選ぼうとするとエラーになるなぁと思ったら、アプリでない設定画面でもSkydrive上の画像はエラーになった。

 

あと、アプリをデフォルトの設定ページに関連付けて表示することもできるようだ。

 画像下部にアカウントの画像アプリとして登録されたアプリがありますね。

 


Please give us your valuable comment

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

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