使用 Caliburn.Micro Windows Phone 上的 Facebook 身份验证回调

Facebook authentication callback on Windows Phone using Caliburn.Micro

我正在 Windows Phone 框架的帮助下开发一个应用程序。我正在尝试使用此 tutorial.

实现 Facebook 登录

ContinuationManager 给我带来了问题,因为它假定不使用 MVVM 模型并将整个代码保存在视图后面的代码中。是否有一种干净的方法来返回控制并将 WebAuthenticationBrokerContinuationEventArgs 传递给 LogInViewModel(而不是 LogInView)以便身份验证过程可以继续?

换句话说,我怎样才能使 public async void ContinueWithWebAuthenticationBroker( WebAuthenticationBrokerContinuationEventArgs args) 方法在 LogInViewModel 用户成功完成 Facebook 登录过程后被调用?

有几种方法可以让这个工作,让我给你我的...

我需要与第 3 方建立 OAuth 连接,对于与该方的所有通信,我创建了一个 Service class - 这个 class 实现了 IWebAuthenticationContinuable 接口。 换句话说,所有 API 调用都在那里,还有 ContinueWebAuthentication 方法。 在 ContinueWebAuthentication 方法中,我调用一个 Status 事件 - 这也在服务 class 上声明 - 以通知任何侦听器有关 OAuth 处理的实际结果状态。

所以唯一剩下要做的就是在 ViewModel 中注册该事件,您将在其中启动 OAuth 流程并根据状态更改采取行动,以验证 OAuth 流程是否正常。 启动实际的 OAuth 过程只需触发一个方法(在我的例子中,服务 class 方法 GetAccessToken() ),在这个方法中你启动 WebuthenticationBroker.AuthenticateAndContinue() 方法。

其他解决方案是使用 MVVM 消息而不是真实事件。但这只是语义。

我正在使用 Caliburn,最近我实现了 Facebook 登录。

在我的 LoginViewModel 中,我调用 WebAuthenticationBroker.AuthenticateAndContinue(url); 来启动登录。

在 App.xaml.cs 中,我覆盖了 OnActivated 方法,我检查参数是否来自身份验证代理,如果是,则将它们作为消息发送。

protected override void OnActivated(IActivatedEventArgs args)
{            
    base.OnActivated(args);

    switch (args.Kind)
    {
        case ActivationKind.WebAuthenticationBrokerContinuation:
                OnWebAuthenticationBrokerContinuation((WebAuthenticationBrokerContinuationEventArgs)args);
                break;
        }
}

private void OnWebAuthenticationBrokerContinuation(WebAuthenticationBrokerContinuationEventArgs args)
{
    var eventAggregator = container.GetInstance<IEventAggregator>()
    eventAggregator.PublishOnUIThread(args);
}

我的 LoginViewModel 实现 IHandle<WebAuthenticationBrokerContinuationEventArgs> 并在 Handle(WebAuthenticationBrokerContinuationEventArgs message) 中获取 Facebook 令牌并使用该服务进行实际登录。