要擷取使用者相關資訊,主要是利用Windows.System.UserProfile 名稱空間下的UserInformation類,這個傢伙是靜態類,你應該知道怎麼用了。
擷取如使用者名稱之類的就TMD簡單了,只需調用對應的方法就完事了,而咱們今天的樣本,是擷取,設定使用者的頭像。
擷取帳戶圖片調用GetAccountPicture方法;設定帳戶圖片調用SetAccountPictureAsync。
建立一個App項目,把首頁面按照新“黃金分割線”劃分為左右兩部分,我們在左邊擷取並顯示帳戶圖片,在右邊設定帳戶圖片。
<Page x:Class="App1.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App1" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition/> </Grid.ColumnDefinitions> <Grid Grid.Column="0" Margin="15"> <Grid.RowDefinitions> <RowDefinition Height="auto"/> <RowDefinition/> </Grid.RowDefinitions> <Button Grid.Row="0" Margin="0,18,0,15" HorizontalAlignment="Center" Content="擷取帳戶圖片" Click="onGetPic_Click"/> <Image Grid.Row="1" x:Name="imgShow" Stretch="Uniform"/> </Grid> <Grid Grid.Column="1" Margin="15"> <Grid.RowDefinitions> <RowDefinition Height="auto"/> <RowDefinition/> </Grid.RowDefinitions> <StackPanel Grid.Row="0" Margin="0,18,0,15" HorizontalAlignment="Center" Orientation="Horizontal"> <Button Content="開啟圖片" Click="onOpenPic_Click"/> <Button Content="設定帳戶圖片" Click="onSetPic_Click"/> </StackPanel> <Image x:Name="imgPreview" Grid.Row="1" Stretch="Uniform"/> </Grid> </Grid></Page>
切換到程式碼檢視,引入以下命名空間:
using Windows.System.UserProfile;using Windows.Storage;using Windows.Storage.Pickers;using Windows.Storage.Streams;using Windows.UI.Xaml.Media.Imaging;
下面的代碼,處理顯示帳戶圖片。
private async void onGetPic_Click(object sender, RoutedEventArgs e) { // 獲得帳戶圖片 var imageFile = UserInformation.GetAccountPicture(AccountPictureKind.LargeImage); using (IRandomAccessStream stream = await imageFile.OpenAsync(FileAccessMode.Read)) { BitmapImage bmp = new BitmapImage(); bmp.SetSource(stream); this.imgShow.Source = bmp; } }
下面兩段代碼,第一段是開啟並預覽圖片,第二段是設定使用者的頭像。
private async void onOpenPic_Click(object sender, RoutedEventArgs e) { // 開啟一張圖片 FileOpenPicker picker = new FileOpenPicker(); picker.FileTypeFilter.Add(".jpg"); picker.FileTypeFilter.Add(".png"); picker.FileTypeFilter.Add(".jpeg"); picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; var file = await picker.PickSingleFileAsync(); if (file != null) { // 顯示圖片預覽 BitmapImage bmp = new BitmapImage(); bmp.SetSource(await file.OpenAsync(FileAccessMode.Read)); this.imgPreview.Source = bmp; // 為了方便後面使用,將檔案賦給tag屬性 this.imgPreview.Tag = file; } } private async void onSetPic_Click(object sender, RoutedEventArgs e) { StorageFile imgFile = this.imgPreview.Tag as StorageFile; if (imgFile == null) return; var result = await UserInformation.SetAccountPictureAsync(imgFile); Windows.UI.Popups.MessageDialog dlg = null; if (result == SetAccountPictureResult.Success) { dlg = new Windows.UI.Popups.MessageDialog("操作成功。"); } else { dlg = new Windows.UI.Popups.MessageDialog("操作失敗。"); } await dlg.ShowAsync(); }
開啟資訊清單檔,切換到“聲明”選項卡,從下拉式清單中選擇“帳戶圖片提供者”,點擊“添加”按鈕。儲存。
添加這個擴充後,在設定使用者圖片的時候,系統就不會發出提示。
現在可以運行一下了。
a、在頁面的左邊,點擊按鈕,就可以顯示目前使用者的頭像。
b、在頁面的右邊,設定使用者映像。
開啟系統設定,驗證一個是否設定成功。