W8.1 Live SDK 5.6 - LiveAuthClient.InitializeAsync System.Nu



我正在使用MVVM Light开发Windows 8.1应用程序(XAML/c#)。

我曾经把我的LiveId保存在代码中,只是为了调试,但现在是时候做登录了。

目前我被这段代码困住了:

this.authClient = new LiveAuthClient();
LiveLoginResult loginResult = await this.authClient.InitializeAsync(scopes);

它一直给我错误:

类型为'System '的异常。在mscorlib.dll中发生了NullReferenceException,但未在用户代码中处理

附加信息:对象引用未设置为类的实例对象。

源代码:

    private static readonly string[] scopes =
            new string[] { 
               "wl.signin", 
               "wl.basic", 
               "wl.offline_access"};
    private LiveAuthClient authClient;
    private LiveConnectClient liveClient;

    public DashboardView()
    {
        this.InitializeComponent();
    }
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        this.InitializePage();
    }
    private async void InitializePage()
    {
        this.authClient = new LiveAuthClient();
        LiveLoginResult loginResult = await this.authClient.InitializeAsync(scopes);
        if (loginResult.Status == LiveConnectSessionStatus.Connected)
        {
            if (this.authClient.CanLogout)
            {
                this.btnLogin.Content = "Sign Out";
            }
            else
            {
                this.btnLogin.Visibility = Visibility.Collapsed;
            }
            this.liveClient = new LiveConnectClient(loginResult.Session);
            this.GetMe();
        }
    }
    private async void btnLogin_Click(object sender, RoutedEventArgs e)
    {
        if (this.btnLogin.Content.ToString() == "Sign In")
        {
            LiveLoginResult loginResult = await this.authClient.LoginAsync(scopes);
            if (loginResult.Status == LiveConnectSessionStatus.Connected)
            {
                if (this.authClient.CanLogout)
                {
                    this.btnLogin.Content = "Sign Out";
                }
                else
                {
                    this.btnLogin.Visibility = Visibility.Collapsed;
                }
                this.liveClient = new LiveConnectClient(loginResult.Session);
                this.GetMe();
            }
        }
        else
        {
            this.authClient.Logout();
            this.btnLogin.Content = "Sign In";
        }
    }
    private async void GetMe()
    {
        Task<LiveOperationResult> task = this.liveClient.GetAsync("me");
        var result = await task;
        dynamic profile = result.Result;
    }

我甚至尝试了一些不同的范围,这是我最后一次尝试。

据我所知,在我的情况下,通过接口和/或类库调用LiveAuthClient.LoginAsync时发生了问题。为了解决这个问题,我使用了MVVMLight库中的中介Messenger类从与商店关联的应用程序入口项目中登录。要将应用程序与商店关联,请参阅本文http://www.codeproject.com/Articles/708863/Developer-Guide-to-Write-Windows-Store-App-usi。

我的视图模型在单独的(可移植)库中,它们引用了包含服务接口的契约库,以便在没有获得NullReferenceException的情况下登录创建消息类。

此解决方案适用于WP8.1,但由于平台共享相同的SDK,它应该可以工作。在示例代码中,我使用Messenger和SimpleIoc从MVVM光。

消息类:

public class OneDriveLoginRequestMessage
{
    public Action CallbackAction { get; set; }
}

注册您的OneDriveClient实例(或者无论您如何调用您的包装器),我使用MVVMLight的SimpleIoc

SimpleIoc.Default.Register(() => new OneDriveClient());

RootFrame_FirstNavigated方法的App.xaml.cs文件中添加以下代码登录:

Messenger.Default.Register<OneDriveLoginRequestMessage>(this, async (msg) =>
{
    await SimpleIoc.Default.GetInstance<OneDriveClient>().Login();
    if (msg != null && msg.CallbackAction != null)
    {
        msg.CallbackAction();
    }
});

最后从视图模型登录:

private void NavigateToOneDrivePage()
{
    MessengerInstance.Send(new OneDriveLoginRequestMessage
    {
        CallbackAction = async () =>
        {
            // after login continue here...
            var client = SimpleIoc.Default.GetInstance<OneDriveClient>();
        }
    });
}

希望这能解决你的问题。

最好,M

最新更新