有什么方法可以登录到应用程序(Silverlight,WP7 可以)而无需单击登录按钮。
我想动态登录我,例如:当你启动应用程序时,我想登录我。如何在不使用按钮的情况下做到这一点?
有什么方法可以登录到应用程序(Silverlight,WP7 可以)而无需单击登录按钮。
我想动态登录我,例如:当你启动应用程序时,我想登录我。如何在不使用按钮的情况下做到这一点?
我想出了怎么做,所以我决定分享:
using System.Windows;
using Microsoft.Live;
public class LiveLogin
{
private static readonly string[] scopes =
new string[] {
"wl.signin",
"wl.basic",
"wl.calendars",
"wl.calendars_update",
"wl.contacts_calendars",
"wl.events_create" };
private LiveAuthClient authClient;
private LiveConnectClient liveClient;
public LiveLogin()
{
this.authClient = new LiveAuthClient("**your client id here**");
this.authClient.InitializeCompleted += authClient_InitializeCompleted;
this.authClient.InitializeAsync(scopes);
}
private void authClient_InitializeCompleted(object sender, LoginCompletedEventArgs e)
{
if (e.Status == LiveConnectSessionStatus.Connected)
{
this.liveClient = new LiveConnectClient(e.Session);
}
else
{
this.authClient.LoginCompleted += authClient_LoginCompleted;
this.authClient.LoginAsync(scopes);
}
}
private void authClient_LoginCompleted(object sender, LoginCompletedEventArgs e)
{
if (e.Status == LiveConnectSessionStatus.Connected)
{
this.liveClient = new LiveConnectClient(e.Session);
MessageBox.Show("Signed");
}
else
{
MessageBox.Show("Failed!");
}
}
}
很好的答案理查德。这真的很有帮助。
我注意到有人抱怨他们找不到 InitializedCompleted 事件的一些评论。如果您在 .Net 4.5 中进行编码,那么您需要遵循异步方法的 async/await 模式。上面的类看起来像这样:
public class LiveLogin
{
private static readonly string[] Scopes =
new[]
{
"wl.signin",
"wl.basic",
"wl.calendars",
"wl.calendars_update",
"wl.contacts_calendars",
"wl.events_create"
};
private LiveAuthClient _authClient;
public async Task<LiveConnectClient> Login()
{
_authClient = new LiveAuthClient("**your client id here**");
LiveLoginResult result = await _authClient.InitializeAsync(Scopes);
if (result.Status == LiveConnectSessionStatus.Connected)
{
return new LiveConnectClient(result.Session);
}
result = await _authClient.LoginAsync(Scopes);
if (result.Status == LiveConnectSessionStatus.Connected)
{
return new LiveConnectClient(result.Session);
}
return null;
}
}
MS在这里有一个异步等待入门
感谢代码示例 - 帮助我想出了 Windows Phone 8 等的代码更新版本 :)
using System.Windows;
using Microsoft.Live;
public class LiveLogin : PhoneApplicationPage
{
private static readonly string[] _scopes =
new[] {
"wl.signin",
"wl.basic",
"wl.calendars",
"wl.calendars_update",
"wl.contacts_calendars",
"wl.events_create" };
private LiveConnectClient _connection;
private LiveLoginResult _login;
public LiveLogin()
{
this.Loaded += this.OnLoaded;
}
private async void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
//----------------------------------------------------------------------
// Login to skydrive
//----------------------------------------------------------------------
await SkydriveLogin();
}
private async Task SkydriveLogin()
{
try
{
//----------------------------------------------------------------------
// Initialize our auth client with the client Id for our specific application
//----------------------------------------------------------------------
LiveAuthClient authClient = new LiveAuthClient("**your client id here**");
//----------------------------------------------------------------------
// Using InitializeAsync we can check to see if we already have an connected session
//----------------------------------------------------------------------
_login = await authClient.InitializeAsync(_scopes);
//----------------------------------------------------------------------
// If not connected, bring up the login screen on the device
//----------------------------------------------------------------------
if (_login.Status != LiveConnectSessionStatus.Connected)
{
_login = await authClient.LoginAsync(_scopes);
}
//----------------------------------------------------------------------
// Initialize our connection client with our login result
//----------------------------------------------------------------------
_connection = new LiveConnectClient(_login.Session);
}
catch (Exception ex)
{
//TODO: Add connection specific exception handling
}
}
}