W8.1直播SDK 5.6 - LiveAuthClient.InitializeAsync System.NullReferenceException [英] W8.1 Live SDK 5.6 - LiveAuthClient.InitializeAsync System.NullReferenceException

查看:207
本文介绍了W8.1直播SDK 5.6 - LiveAuthClient.InitializeAsync System.NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Windows应用程序8.1(XAML / C#)与MVVM光。

I'm developing a Windows 8.1 App (XAML/C#) with MVVM Light.

我曾经让我的code只是为了调试里面LiveID的,但现在是时候做登录。

I used to keep my LiveId inside the code just for debugging, but now it's time to do the LogIn.

目前我是卡着这片code的:

Currently i'm stuck with this piece of code:

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

它不断给我的错误:

It keeps giving me the error:

型System.NullReferenceException的异常出现在mscorlib.dll但在用户code没有处理

An exception of type 'System.NullReferenceException' occurred in mscorlib.dll but was not handled in user code

其他信息:未将对象引用设置的一个实例
  对象。

Additional information: Object reference not set to an instance of an object.

来源$ C ​​$ C:

Source Code:

    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;
    }

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

I even tried some different scopes and this was my last try.

先谢谢了。

推荐答案

据我已经注意到调用时出现在我的情况下,这个问题 LiveAuthClient.LoginAsync 通过接口和/或类库。为了解决我已经使用中介使者类从MVVMLight库从与存储相关的应用程序进入项目登录的问题。要应用与存储关联按照本文的http://www.$c$cproject.com/Articles/708863/Developer-Guide-to-Write-Windows-Store-App-usi

As far as i have noticed that problem occurred in my case when calling LiveAuthClient.LoginAsync through interface and/or class library. In order to solve the problem I have used mediator Messenger class from MVVMLight library to login from application entry project which is associated with the store. To associate app with the store follow this article http://www.codeproject.com/Articles/708863/Developer-Guide-to-Write-Windows-Store-App-usi .

我的视图模型在不同的(便携式)库和它们引用的合同,其中包含了服务界面库,为了没有得到登录的NullReferenceException 创建一个消息类。

My view models are in separate (portable) library and they reference contracts library which contains interfaces for services, in order to login without getting NullReferenceException create a message class.

这解决方案是WP8.1,但由于平台共享相同的SDK,它应该工作。在样品code我使用Messenger和SimpleIoc从MVVM光。

This solution is for WP8.1, but as platforms are sharing same SDK, it should work. In sample code I am using Messenger and SimpleIoc from MVVM light.

Message类:

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

填写您的 OneDriveClient (或者不管你在呼唤您的包装)我使用MVVMLight的 SimpleIoc

Register instance of your OneDriveClient (or however you are calling your wrapper) I am using MVVMLight's SimpleIoc

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

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

Inside App.xaml.cs file in method RootFrame_FirstNavigated add following code to login:

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

和最后将其从视图模型登录:

And finally to login from view model:

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

我希望解决您的问题。

I hope that solves your problem.

最佳,男

这篇关于W8.1直播SDK 5.6 - LiveAuthClient.InitializeAsync System.NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆