新時尚Windows8開發(37):擷取和設定帳戶圖片

來源:互聯網
上載者:User

要擷取使用者相關資訊,主要是利用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、在頁面的右邊,設定使用者映像。

 

開啟系統設定,驗證一個是否設定成功。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.