【发布时间】:2023-03-27 18:20:02
【问题描述】:
我目前正在通过重新做一个简单的移动应用程序来学习 F#,该应用程序是我在 C# 和 Xamarin.forms 中完成的,该应用程序的目标是连接用户与 facebook 并获取他的个人资料和帖子。
我几乎完成了一切,但我被阻止了。为了在 C# 中连接到 facebook API,我使用了 Xamarin.Auth 库,我想在 F# 中重用这个库。
这是我在 C# 中的 LoginPage ViewModel 的代码:
public class LoginPageViewModel : BaseViewModel
{
private readonly INavigationService _navigationService;
private readonly IConfiguration _config;
private LoginLogic _loginLogic;
public ICommand NavigateCommand { get; set; }
public OAuth2Authenticator MyAuthenticator;
public ICommand ConnectVerification { get; set; }
public bool CanSkipPage { get; set; }
public LoginPageViewModel(INavigationService navigationService, IConfiguration configuration)
{
if (navigationService == null) throw new ArgumentNullException("navigationService");
_navigationService = navigationService;
if (configuration == null) throw new ArgumentNullException("Configuration");
_config = configuration;
NavigateCommand = new RelayCommand(() => { _navigationService.NavigateTo(Locator.FacebookProfilePage); });
MyAuthenticator = new OAuth2Authenticator(
_config.facebookAppId,
_config.scope,
new Uri(_config.facebookAuthUrl),
new Uri(_config.facebookRedirectUrl),
null);
MyAuthenticator.Completed += OnAuthenticationCompleted;
MyAuthenticator.Error += OnAuthenticationFailed;
_loginLogic = SimpleIoc.Default.GetInstance<LoginLogic>();
this.ConnectVerification = new AsyncCommand(() => TokenVerification());
}
public async Task TokenVerification()
{
IsLoading = true;
if (await _loginLogic.CheckToken())
NavigateCommand.Execute(null);
IsLoading = false;
}
async void OnAuthenticationCompleted(object sender, AuthenticatorCompletedEventArgs e)
{
IsLoading = true;
var authenticator = sender as OAuth2Authenticator;
if (authenticator != null)
{
authenticator.Completed -= OnAuthenticationCompleted;
authenticator.Error -= OnAuthenticationFailed;
}
await _loginLogic.SetTokenAsync(e.Account.Properties["access_token"]);
loginLogic.SetTokenAsync(e.Account.Properties["access_token"]);
NavigateCommand.Execute(null);
IsLoading = false;
}
void OnAuthenticationFailed(object sender, AuthenticatorErrorEventArgs e)
{
var authenticator = sender as OAuth2Authenticator;
if (authenticator != null)
{
authenticator.Completed -= OnAuthenticationCompleted;
authenticator.Error -= OnAuthenticationFailed;
}
}
}
我的问题是使用 Xamarin.Auth 。我必须创建一个 OAuth2Authenticator 属性,我在我的类的构造函数中初始化它,然后将此属性的 EventHandler .Complete 和 .Error 订阅到我的类构造函数中的两个事件 OnAuthenticationCompleted 和 OnAuthenticationFailed 我不知道该怎么做在 F# 中。 现在,我的 F# 类看起来像这样:
open Xamarin.Auth
open System
open GalaSoft.MvvmLight.Views
type LoginPageViewModel(navigationService: INavigationService) =
inherit ViewModelBase()
let mutable isLoading = false
let authenticator = new OAuth2Authenticator(config.facebookAppId,
config.scope,
new Uri(config.facebookAuthUrl),
new Uri(config.facebookRedirectUrl),
null)
member this.MyAuthenticator
with get() = authenticator
member this.IsLoading
with get() = isLoading
and set(value) =
isLoading <- value
base.NotifyPropertyChanged(<@ this.IsLoading @>)
member this.TokenVerification() =
this.IsLoading <- true
if loginLogic.CheckToken()
then
navigationService.NavigateTo("FacebookProfilePage")
this.IsLoading <- false
但我不知道:
首先,我应该在哪里创建我的两个方法 OnAuthenticationCompleted 和 OnAuthenticationFailed,它们是否应该是类的方法?
第二,如何在我的类构造函数中订阅我的 OAuth2Authenticator.Complete 到 OnAuthenticationCompleted 和 OAuth2Authenticator.Error 到 OnAuthenticationFailed 方法
【问题讨论】: