我在Channel 9网站上关注了43分钟的视频教程并阅读了LiveConnect页面,其中显示了代码,我看不出我做错了什么。它一直给我一个NullReferenceException错误,它甚至没有提出“你想让app X访问skydrive”的事情,它只是立即中断。我到处都设置了断点但没有任何东西。只是null,无处不在。
OnNavigatedTo事件:
LoadProfile();
private async void LoadProfile()
{
try
{
LiveAuthClient auth = new LiveAuthClient();
LiveLoginResult loginResult = await auth.LoginAsync(new string[] { "wl.basic" });
if (loginResult.Status == LiveConnectSessionStatus.Connected)
{
this.pageTitle.Text = "Signed in.";
}
}
catch (LiveAuthException exception)
{
this.pageTitle.Text = "Error signing in: " + exception.Message;
}
}
例外情况说:
答案 0 :(得分:2)
我终于找到了解决方案。
订阅按钮点击事件或其他任何内容,然后使用此代码:
LoadProfile();
调用此方法:
public async void LoadProfile()
{
try
{
LiveAuthClient auth = new LiveAuthClient();
LiveLoginResult initializeResult = await auth.InitializeAsync();
try
{
LiveLoginResult loginResult = await auth.LoginAsync(new string[] { "wl.basic" });
if (loginResult.Status == LiveConnectSessionStatus.Connected)
{
LiveConnectClient connect = new LiveConnectClient(auth.Session);
LiveOperationResult operationResult = await connect.GetAsync("me");
dynamic result = operationResult.Result;
if (result != null)
{
this.pageTitle.Text = string.Join(" ", "Hello", result.name, "!");
}
else
{
this.pageTitle.Text = "Error getting name.";
}
}
}
catch (LiveAuthException exception)
{
this.pageTitle.Text = "Error signing in: " + exception.Message;
}
catch (LiveConnectException exception)
{
this.pageTitle.Text = "Error calling API: " + exception.Message;
}
}
catch (LiveAuthException exception)
{
this.pageTitle.Text = "Error initializing: " + exception.Message;
}
}
在调试之前,将您的应用程序添加到Windows应用商店信息中心。然后返回Visual Studio,在解决方案资源管理器中找到Package.appxmanifest并添加Internet功能。然后转到项目菜单>商店>将应用程序与商店关联。
在显示的应用列表中找到您的应用名称,选择它并单击下一步/完成然后调试。现在应该可以了。
答案 1 :(得分:0)
请尝试使用此代码而不是您的代码:
LiveAuthClient auth = new LiveAuthClient();
LiveLoginResult loginResult = await auth.InitializeAsync(new string[] { "wl.basic" });
if ( loginResult.Status == LiveConnectSessionStatus.Connected )
{
LiveConnectClient connect = new LiveConnectClient( auth.Session );
...