我正在关注Windows Live SDK 5.6示例代码并尝试使用我自己的简单应用程序登录OneDrive。随着我的Microsoft帐户一步一步给出,似乎一切都很好,但是,我总是得到System.NullReferenceException,当应用程序再次转到此页面时,当我单击单个按钮时:
private async void signInBtn_Click(object sender, RoutedEventArgs e)
{
try
{
authClient = new LiveAuthClient();
System.Diagnostics.Debug.WriteLine("authClient = " + authClient);
loginResult = await authClient.LoginAsync(new string[] { "wl.signin", "wl.skydrive", "wl.skydrive_update", "wl.photos" });
if (loginResult.Status == LiveConnectSessionStatus.Connected)
{
liveClient = new LiveConnectClient(loginResult.Session);
var meResult = await liveClient.GetAsync("me");
System.Diagnostics.Debug.WriteLine(meResult.Result["name"].ToString() + ", " + "You have logged in OneDrive!");
}
}
catch (LiveAuthException authExp)
{
System.Diagnostics.Debug.WriteLine("LiveAuthException = " + authExp.ToString());
}
catch (LiveConnectException connExp)
{
System.Diagnostics.Debug.WriteLine("LiveConnectException = " + connExp.ToString());
}
}
它在此行抛出异常:
loginResult = await authClient.LoginAsync(new string[] { "wl.signin", "wl.skydrive", "wl.skydrive_update", "wl.photos" });
我的代码出了什么问题?甚至提到了样本代码?
答案 0 :(得分:0)
尝试传递列表而不是String数组:
public static async Task<LiveLoginResult> LoginAsync()
{
List<String> oneDriveScopes = new List<String>() { "wl.signin", "wl.basic", "wl.skydrive_update" };
LiveAuthClient authClient = new LiveAuthClient();
LiveLoginResult authResult;
try
{
authResult = await authClient.LoginAsync(oneDriveScopes);
}
catch
{
return null;
}
return authResult;
}
答案 1 :(得分:0)
我终于设法使代码正常工作:在运行代码之前,我必须在Windows商店中关联我的应用程序,以便在项目中有一个名为Package.StoreAssociation的文件。 xml生成。 通过身份验证,我的真实帐户信息被正确检索,不再有任何例外。