重新想象 Windows 8 Store Apps (58) - 微软账号
作者:webabcd
介绍
重新想象 Windows 8 Store Apps 之 微软账号
- 获取微软账号的用户相关的信息
- 获取或设置微软账号的图片和视频
- 微软账号的验证,和相关信息的获取
示例
1、演示如何获取微软账号的用户相关的信息
Account/AccountInfo.xaml
<Page
x:Class="XamlDemo.Account.AccountInfo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Account"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" /> </StackPanel>
</Grid>
</Page>
Account/AccountInfo.xaml.cs
/*
* 演示如何获取微软账号的用户相关的信息
*/ using System;
using Windows.System.UserProfile;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation; namespace XamlDemo.Account
{
public sealed partial class AccountInfo : Page
{
public AccountInfo()
{
this.InitializeComponent();
} protected async override void OnNavigatedTo(NavigationEventArgs e)
{
if (UserInformation.NameAccessAllowed) // 是否允许访问用户名
{
// 获取用于显示的名称
lblMsg.Text = "display name: " + await UserInformation.GetDisplayNameAsync();
lblMsg.Text += Environment.NewLine; // 获取 first name
lblMsg.Text += "first name: " + await UserInformation.GetFirstNameAsync();
lblMsg.Text += Environment.NewLine; // 获取 last name
lblMsg.Text += "last name: " + await UserInformation.GetLastNameAsync();
lblMsg.Text += Environment.NewLine;
} // 如果需要获取 GetDomainNameAsync(), GetPrincipalNameAsync(), GetSessionInitiationProtocolUriAsync() 等信息
// 则需要在 Package.appxmanifest 中增加配置 <Capability Name="enterpriseAuthentication" />,且必须使用公司账号上传 app
}
}
}
2、演示如何获取或设置微软账号的图片和视频
Account/AccountPicture.xaml
<Page
x:Class="XamlDemo.Account.AccountPicture"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Account"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="120 0 0 0"> <StackPanel Orientation="Horizontal">
<Button x:Name="btnSetImage" Content="设置当前微软账号的图片(可以分别指定小图,大图,视频)" Click="btnSetImage_Click_1" />
</StackPanel> <StackPanel Orientation="Horizontal" Margin="0 10 0 0">
<Image x:Name="imgSmall" Width="96" Height="96" HorizontalAlignment="Left" />
<Image x:Name="imgLarge" Width="448" Height="448" Margin="10 0 0 0" HorizontalAlignment="Left" />
<MediaElement x:Name="mediaElement" Width="200" Height="200" Margin="10 0 0 0" HorizontalAlignment="Left" />
</StackPanel> </StackPanel>
</Grid>
</Page>
Account/AccountPicture.xaml.cs
/*
* 演示如何获取或设置微软账号的图片和视频
*/ using System;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.System.UserProfile;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
using XamlDemo.Common; namespace XamlDemo.Account
{
public sealed partial class AccountPicture : Page
{
public AccountPicture()
{
this.InitializeComponent();
} protected override void OnNavigatedTo(NavigationEventArgs e)
{
GetSmallImage();
GetLargeImage();
GetVideo(); // 当微软账号的图片或视频发生变化时触发的事件
UserInformation.AccountPictureChanged += PictureChanged;
} protected override void OnNavigatedFrom(NavigationEventArgs e)
{
UserInformation.AccountPictureChanged -= PictureChanged;
} private void PictureChanged(object sender, object e)
{
GetSmallImage();
GetLargeImage();
GetVideo();
} // 获取小图片
private async void GetSmallImage()
{
// UserInformation.GetAccountPicture(AccountPictureKind.SmallImage) - 获取当前微软账号的小图片
StorageFile image = UserInformation.GetAccountPicture(AccountPictureKind.SmallImage) as StorageFile;
if (image != null)
{
try
{
IRandomAccessStream imageStream = await image.OpenReadAsync();
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(imageStream);
imgSmall.Source = bitmapImage;
}
finally { }
}
} // 获取大图片
private async void GetLargeImage()
{
// UserInformation.GetAccountPicture(AccountPictureKind.LargeImage) - 获取当前微软账号的大图片
StorageFile image = UserInformation.GetAccountPicture(AccountPictureKind.LargeImage) as StorageFile;
if (image != null)
{
try
{
IRandomAccessStream imageStream = await image.OpenReadAsync();
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(imageStream);
imgLarge.Source = bitmapImage;
}
finally { }
}
} // 获取视频
private async void GetVideo()
{
// UserInformation.GetAccountPicture(AccountPictureKind.Video) - 获取当前微软账号的视频
StorageFile video = UserInformation.GetAccountPicture(AccountPictureKind.Video) as StorageFile;
if (video != null)
{
try
{
IRandomAccessStream videoStream = await video.OpenAsync(FileAccessMode.Read);
mediaElement.SetSource(videoStream, "video/mp4");
}
finally { }
}
} // 设置图片
private async void btnSetImage_Click_1(object sender, RoutedEventArgs e)
{
if (Helper.EnsureUnsnapped())
{
FileOpenPicker imagePicker = new FileOpenPicker
{
ViewMode = PickerViewMode.Thumbnail,
SuggestedStartLocation = PickerLocationId.PicturesLibrary,
FileTypeFilter = { ".jpg", ".jpeg", ".png", ".bmp" }
}; StorageFile imageFile = await imagePicker.PickSingleFileAsync();
if (imageFile != null)
{
// UserInformation.SetAccountPicturesAsync() - 设置微软账号的图片和视频(可以分别指定:小图片,大图片,视频)
SetAccountPictureResult result = await UserInformation.SetAccountPicturesAsync(null, imageFile, null);
if (result == SetAccountPictureResult.Success)
{ }
}
}
}
}
}
3、演示微软账号的验证,和相关信息的获取
Account/AccountAuthorization.xaml
<Page
x:Class="XamlDemo.Account.AccountAuthorization"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Account"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" />
<Button Name="btnSignIn" Content="登录" Margin="0 10 0 0" Click="btnSignIn_Click_1" />
<Button Name="btnSignOut" Content="注销" Margin="0 10 0 0" Click="btnSignOut_Click_1" /> </StackPanel>
</Grid>
</Page>
Account/AccountAuthorization.xaml.cs
/*
* 演示微软账号的验证,和相关信息的获取
*
* 注:
* 1、如果要使用此功能,需要先去 https://manage.dev.live.com/Applications/Index 注册,否则会出现“应用程序请求身份验证令牌被禁用或者配置错误”错误
* 2、关于 Live Connect 的更多东西,请参见“Live Connect 开发人员中心”:http://msdn.microsoft.com/zh-cn/live
*/ using System;
using System.Collections.Generic;
using Windows.Security.Authentication.OnlineId;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace XamlDemo.Account
{
public sealed partial class AccountAuthorization : Page
{
// OnlineIdAuthenticator - 用于身份验证,以及信息获取
private OnlineIdAuthenticator _authenticator; public AccountAuthorization()
{
this.InitializeComponent(); _authenticator = new OnlineIdAuthenticator();
} private async void btnSignIn_Click_1(object sender, RoutedEventArgs e)
{
lblMsg.Text += "登录中";
lblMsg.Text += Environment.NewLine; try
{
// 用于身份验证,以及获取 token(通过此 token 可以用 rest 方式获取指定范围内的信息)
var targetArray = new List<OnlineIdServiceTicketRequest>();
targetArray.Add(new OnlineIdServiceTicketRequest("wl.basic wl.contacts_photos wl.calendars", "DELEGATION")); /*
* OnlineIdAuthenticator.AuthenticateUserAsync() - 登录
* CredentialPromptType.DoNotPrompt - 不显示登录 UI(可能会导致无法登录)
* CredentialPromptType.PromptIfNeeded - 如果需要的话才显示登录 UI(一般来说一个 app 在登录成功一次之后,就不必再通过 UI 登录了)
* CredentialPromptType.RetypeCredentials - 始终显示登录 UI
*/
var result = await _authenticator.AuthenticateUserAsync(targetArray, CredentialPromptType.PromptIfNeeded);
if (result.Tickets[].Value != string.Empty)
{
lblMsg.Text += "已登录";
lblMsg.Text += Environment.NewLine; // 获取 token (此 token 可以通过 rest 方式访问 wl.basic wl.contacts_photos wl.calendars 信息)
// 相关信息的访问地址 https://apis.live.net/v5.0/me?access_token=
lblMsg.Text += "token: " + result.Tickets[].Value;
lblMsg.Text += Environment.NewLine;
}
else
{
lblMsg.Text += "未得到 token ,errorCode: " + result.Tickets[].ErrorCode.ToString();
lblMsg.Text += Environment.NewLine;
}
}
catch (Exception ex)
{
lblMsg.Text += ex.ToString();
lblMsg.Text += Environment.NewLine;
}
} private async void btnSignOut_Click_1(object sender, RoutedEventArgs e)
{
lblMsg.Text += "注销中";
lblMsg.Text += Environment.NewLine; // OnlineIdAuthenticator.SignOutUserAsync() - 注销
await _authenticator.SignOutUserAsync(); lblMsg.Text += "已注销";
lblMsg.Text += Environment.NewLine;
}
}
}
OK
[源码下载]
重新想象 Windows 8 Store Apps (58) - 微软账号的更多相关文章
- 重新想象 Windows 8 Store Apps 系列文章索引
[源码下载][重新想象 Windows 8.1 Store Apps 系列文章] 重新想象 Windows 8 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...
- 重新想象 Windows 8 Store Apps (41) - 打印
[源码下载] 重新想象 Windows 8 Store Apps (41) - 打印 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 打印 示例1.需要打印的文档Pr ...
- 重新想象 Windows 8 Store Apps (1) - 控件之文本控件: TextBlock, TextBox, PasswordBox, RichEditBox, RichTextBlock, RichTextBlockOverflow
原文:重新想象 Windows 8 Store Apps (1) - 控件之文本控件: TextBlock, TextBox, PasswordBox, RichEditBox, RichTextBl ...
- 重新想象 Windows 8 Store Apps (24) - 文件系统: Application Data 中的文件操作, Package 中的文件操作, 可移动存储中的文件操作
原文:重新想象 Windows 8 Store Apps (24) - 文件系统: Application Data 中的文件操作, Package 中的文件操作, 可移动存储中的文件操作 [源码下载 ...
- 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo
[源码下载] 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo 作者:webabcd 介绍重新想象 Wind ...
- 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解
[源码下载] 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Toa ...
- 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解
[源码下载] 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Tile ...
- 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract
[源码下载] 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...
- 重新想象 Windows 8 Store Apps (38) - 契约: Search Contract
[源码下载] 重新想象 Windows 8 Store Apps (38) - 契约: Search Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 ...
随机推荐
- ecshop登陆后价格可见,会员注册登陆才能显示价格
打开模版文件夹里面的goods.dwt 查找{$lang.shop_price}<font class="price" id="ECS_SHOPPRICE" ...
- 解决Win7旗舰版开机后无线网络识别非常慢的问题
最近电脑开机后WIFI识别和连接非常慢,不知何故.查看百度安全卫士的优化记录,发现其禁用了 Network List Service,将该服务设为自动启动,重启服务后,问题解决.PS:如此优化太可恶!
- Hadoop - Ambari集群管理剖析
1.Overview Ambari是Apache推出的一个集中管理Hadoop的集群的一个平台,可以快速帮助搭建Hadoop及相关以来组件的平台,管理集群方便.这篇博客记录Ambari的相关问题和注意 ...
- 【原】Jqxgrid在Java服务器端分页
研究这个后台分页一天多,特此写个文章记录备忘 jsp页面中有两个需要注意的地方:一个是source中beforeprocessing,另一个是rendergridrows中数据的获取. 说明:grid ...
- sql2008清空日志
USE[master] GO ALTER DATABASE MeSizeSNS SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE MeSizeSNS ...
- [leetode]Binary Search Tree Iterator
用个stack模拟递归即可 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * Tr ...
- 15款效果很酷的最新jQuery/CSS3特效
很久没来博客园发表文章了,今天就分享15款效果很酷的最新jQuery/CSS3特效,废话不说,一起来看看吧. 1.3D图片上下翻牌切换 一款基于jQuery+CSS3实现的3D图片上下翻牌切换效果,支 ...
- asp.net MVC之 自定义过滤器(Filter)
一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration:缓存的时间,以秒为 ...
- Oracle数据库入门——pctfree和pctused详解
一.建立表时候,注意PCTFREE参数的作用 PCTFREE:为一个块保留的空间百分比,表示数据块在什么情况下可以被insert,默认是10,表示当数据块的可用空间低于10%后,就不可以被insert ...
- 用SQL server导出到oracle,查询时提示“表或视图不存在ORA-00942”错误
用SQL server2005的导出工具,将数据导出表到oracle,表名称里看到有这张表了,但查询或删除时都提示“ORA-00942表或者试图不存在”的错误,上网查了一下,是如下原因: “查询或删除 ...