问题
I'm developing a Windows 8.1 App (XAML/C#) with MVVM Light.
I used to keep my LiveId inside the code just for debugging, but now it's time to do the LogIn.
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:
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.
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.
Thanks in advance.
回答1:
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 .
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.
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 class:
public class OneDriveLoginRequestMessage
{
public Action CallbackAction { get; set; }
}
Register instance of your OneDriveClient
(or however you are calling your wrapper) I am using MVVMLight's SimpleIoc
SimpleIoc.Default.Register(() => new OneDriveClient());
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.
Best, M
来源:https://stackoverflow.com/questions/24779831/w8-1-live-sdk-5-6-liveauthclient-initializeasync-system-nullreferenceexception