アカウント情報の取得・設定方法
今回は「アカウント情報の取得・設定方法」を紹介します。
アプリケーションを実際に利用するユーザーにとって、アプリケーション内に自身の情報が表示されることで没入感をより感じることが出来ます。
今回触れるアカウント情報は登録名と登録画像(静止画、動画)です。
ちなみに私はこの記事を書くまで、アカウント画像に動画を登録できる事を知りませんでした。5秒だけですが、アカウント画像部分が動くので面白いです。
さて、アカウント情報の取得・設定はWindows.System.UserProfile名前空間を利用します。いかにもな名前ですね。
この名前空間内のUserInformationクラスにある各メソッドを実行することで操作を行います。主なメソッドと機能は次の通りです。
メソッド | 説明 |
---|---|
GetAccountPicture | アカウント画像を取得します |
GetDisplayNameAsync | ディスプレイ表示名を取得します |
GetFirstNameAsync | ユーザーの名前を取得します |
GetLastNameAsync | ユーザーの名字を取得します |
SetAccountPictureAsync | アカウント画像を設定します |
SetAccountPicturesAsync | アカウント画像を小さい静止画、大きい静止画、動画の3つで設定します |
以下に主な利用方法としてサンプルコードを示します。
- アカウント画像の取得
//取得する種類を指定します //小さい静止画:Windows.System.UserProfile.AccountPictureKind.smallImage //大きい静止画:Windows.System.UserProfile.AccountPictureKind.largeImage //動画 :Windows.System.UserProfile.AccountPictureKind.video var getKind = Windows.System.UserProfile.AccountPictureKind.smallImage; //指定した種類のアカウント情報をStorageFileオブジェクトとして取得します var file = Windows.System.UserProfile.UserInformation.getAccountPicture(getKind);
- ディスプレイ表示名の取得
//promiseオブジェクトを返します var promise = Windows.System.UserProfile.UserInformation.getDisplayNameAsync(); //ディスプレイ表示名を取得出来た場合に、onComplete関数を実行します promise.done(onComplete); //resultにはディスプレイ表示名が入っています function onComplete (result) { //処理 }
- アカウント画像(静止画)の登録
//FileOpenPickerクラスで登録画像を指定します var openPicker = new Windows.Storage.Pickers.FileOpenPicker(); //登録画像の選択画面のUIを指定します //サムネイル表示:Windows.Storage.Pickers.PickerViewMode.thumbnail //リスト表示 :Windows.Storage.Pickers.PickerViewMode.list openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail; //画像選択を始める場所を指定します openPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary; //選択できるファイルの拡張子を指定します openPicker.fileTypeFillter.replaceAll([".bmp", ".png", ".jpg", ".jpeg"]); //画像検索が無事終了した場合に、onComplete関数を実行します openPicker.pickSingleFileAsync().done(onComplete); //fileはStorageFileオブジェクトで、選択した画像を表します function onComplete (file) { if (file) { //promiseオブジェクトを返します //第1引数:小さい静止画を指定します(96x96) //第2引数:大きい静止画を指定します(448x448) //第3引数:動画を指定します(448x448, 5sec) var promise = Windows.System.UserProfile.UserInformation.getAccountPicturesAsync(null, file, null); //アカウント画像を登録出来た場合に、onComplete関数を実行します promise.done(onComplete); //resultは登録の結果を表します function onComplete (result) { //処理 } } else { //処理 } }
- アカウント画像(静止画)の登録
//FileOpenPickerクラスで登録画像を指定します var openPicker = new Windows.Storage.Pickers.FileOpenPicker(); openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail; openPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary; //動画ファイルの拡張子を指定します openPicker.fileTypeFillter.replaceAll([".mov", ".mp4", ".wmv"]); openPicker.pickSingleFileAsync().done(onComplete); function onComplete (file) { if (file) { //動画のみ指定した場合、動画から小さい静止画と大きい静止画を自動生成します var promise = Windows.System.UserProfile.UserInformation.getAccountPicturesAsync(null, null, file); promise.done(onComplete); function onComplete (result) { //処理 } } else { //処理 } }