我正在开发一个应用程序来通过 xamarin auth 验证 facebook 用户名,这意味着用户不需要登录,但他们可以验证他们的用户电子邮件。以下是我的用户界面设计代码:
登录.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage BackgroundColor="Blue" xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Test1.Login"
Padding="10,20,0,0">
<StackLayout VerticalOptions="Center" Spacing="20">
<Label Text="Login ID" TextColor="Black" FontSize="Large"/>
<Entry x:Name="Username" Keyboard="Default" Placeholder="sample@hotmail.com" TextColor="AntiqueWhite"/>
<Label Text="Password" TextColor="AntiqueWhite" FontSize="Large"/>
<Entry IsPassword="True" TextColor="AntiqueWhite"/>
<Label x:Name="label"/>
<Button Text="Sign In" Clicked="Button_Clicked"/>
</StackLayout>
我已经设置了验证器功能,但它无法验证 facebook 用户电子邮件,下面是我的代码:
登录.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Xamarin.Auth;
using UIKit;
namespace Test1
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Login : ContentPage
{
static NavigationPage _NavPage;
static string _Token;
public static string Token
{
get
{
return _Token;
}
}
public static void SaveToken(string token)
{
_Token = token;
}
public static Action SuccessfulLoginAction
{
get
{
return new Action(() =>
{
_NavPage.Navigation.PopModalAsync();
});
}
}
public void Button_Clicked(object sender, EventArgs e)
{
var username = Username.Text;
var emailpattern =
"^(?(\".+?(?<!\\\\)\"@)|(([0-9a-z]((\\.(?!\\.))|[-!#\\$%&\'\\*\\+/=\\?\\^'\\{\\}\\|~\\w])*)(?<=[0-9a-z])@))(?(\\[)(\\[(\\d{1,3}\\.){3}\\d{1,3}\\])|(([0-9a-z][-\\w]*[0-9a-z]*\\.)+[a-z0-9][\\-a-z0-9]{0,22}[a-z0-9]))$";
if (Regex.IsMatch(username, emailpattern))
{
var auth = new OAuth2Authenticator(
clientId: "371xxxxxxxxx",
scope: "",
authorizeUrl: new Uri("https://m.facebook.com/dialog/oauth/"),
redirectUrl: new Uri("http://www.facebook.com/connect/login_success.html")
);
auth.Completed += (Sender, EventArgs) =>
{
if (EventArgs.IsAuthenticated)
{
Login.SuccessfulLoginAction.Invoke();
Login.SaveToken(EventArgs.Account.Properties["EAxxxxxxxxxxxxx"]);
} else { }
};
label.Text = "Email is valid";
}
else
{
label.Text = "Email is not valid";
};
}
public Login()
{
InitializeComponent();
}
}
}
任何人都可以分享我的想法吗?