【问题标题】:Facebook authentication callback on Windows Phone using Caliburn.Micro使用 Caliburn.Micro 在 Windows Phone 上的 Facebook 身份验证回调
【发布时间】:2015-07-17 21:10:26
【问题描述】:
我正在借助 Caliburn.Micro 框架开发一个 Windows Phone 应用程序。我正在尝试使用此tutorial 实现 Facebook 登录。
ContinuationManager 给我带来了问题,因为它假定不使用 MVVM 模型并将整个代码保留在视图后面的代码中。有没有一种干净的方式来重新控制并将WebAuthenticationBrokerContinuationEventArgs 传递给LogInViewModel(而不是LogInView),以便身份验证过程可以继续?
换句话说,我怎样才能使
用户成功完成 Facebook 登录过程后,LogInViewModel 中会调用 public async void ContinueWithWebAuthenticationBroker( WebAuthenticationBrokerContinuationEventArgs args) 方法吗?
【问题讨论】:
标签:
c#
facebook
mvvm
windows-phone-8.1
caliburn.micro
【解决方案1】:
我正在使用 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 令牌并使用服务进行实际登录。
【解决方案2】:
有几种方法可以让这个工作,让我给你我的......
我需要与第 3 方建立 OAuth 连接,并且对于与该方的所有通信,我创建了一个 Service class - 这个类实现了 IWebAuthenticationContinuable 接口。
换句话说,所有 API 调用都在那里,还有 ContinueWebAuthentication 方法。
在 ContinueWebAuthentication 方法中,我调用了一个 Status 事件 - 这也是在 Service 类上声明的 - 以通知任何侦听器 OAuth 处理的实际结果状态。
所以剩下要做的就是在 ViewModel 中注册该事件,您将在其中启动 OAuth 流程并根据状态更改采取行动,以验证 OAuth 流程是否正常。
启动实际的 OAuth 过程只需触发一个方法(在我的情况下是在 Service 类上的方法 GetAccessToken() ),在此方法中您启动 WebuthenticationBroker.AuthenticateAndContinue() 方法。
其他解决方案是使用 MVVM 消息传递而不是真实事件。但这只是语义。