设为首页 收藏本站
查看: 1423|回复: 0

重新想象 Windows 8 Store Apps (58)

[复制链接]

尚未签到

发表于 2015-5-22 13:09:09 | 显示全部楼层 |阅读模式
  [源码下载]




重新想象 Windows 8 Store Apps (58) - 微软账号  
作者:webabcd

介绍
重新想象 Windows 8 Store Apps 之 微软账号


  • 获取微软账号的用户相关的信息
  • 获取或设置微软账号的图片和视频
  • 微软账号的验证,和相关信息的获取
  
示例
1、演示如何获取微软账号的用户相关的信息
Account/AccountInfo.xaml










  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 中增加配置 ,且必须使用公司账号上传 app
        }
}
}
  
2、演示如何获取或设置微软账号的图片和视频
Account/AccountPicture.xaml

















  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












  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();
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[0].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[0].Value;
lblMsg.Text += Environment.NewLine;
}
else
{
lblMsg.Text += "未得到 token ,errorCode: " + result.Tickets[0].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
[源码下载]

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-69594-1-1.html 上篇帖子: Windows Phone 8 SDK 正式版初探(Native, C++, DirectX 11.1) 下篇帖子: Windows 8 学习笔记(十四)--.map文件与.kml文件的解析
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表