我目前面临以下问题:
我正在尝试在用户输入有效凭据时触发事件,以便可以切换页面等。
问题是由于某种原因我无法参与该事件(尽管我很确定这将是愚蠢的)。
引发事件的类:
namespace B2B
{
public partial class LoginPage : ContentPage
{
public event EventHandler OnAuthenticated;
public LoginPage ()
{
InitializeComponent ();
}
void onLogInClicked (object sender, EventArgs e)
{
loginActivity.IsRunning = true;
errorLabel.Text = "";
RestClient client = new RestClient ("http://url.be/api/");
var request = new RestRequest ("api/login_check", Method.POST);
request.AddParameter("_username", usernameText.Text);
request.AddParameter("_password", passwordText.Text);
client.ExecuteAsync<Account>(request, response => {
Device.BeginInvokeOnMainThread ( () => {
loginActivity.IsRunning = false;
if(response.StatusCode == HttpStatusCode.OK)
{
if(OnAuthenticated != null)
{
OnAuthenticated(this, new EventArgs());
}
}
else if(response.StatusCode == HttpStatusCode.Unauthorized)
{
errorLabel.Text = "Invalid Credentials";
}
});
});
}
}
}
而在“主要阶级”中
namespace B2B
{
public class App : Application
{
public App ()
{
// The root page of your application
MainPage = new LoginPage();
MainPage.OnAuthenticated += new EventHandler (Authenticated);
}
static void Authenticated(object source, EventArgs e) {
Console.WriteLine("Authed");
}
}
}
当我尝试构建应用程序时,我得到:
类型“ Xamarin.Forms.Page”不包含“ OnAuthenticated”的定义,也没有扩展方法OnAuthenticated
我试过在LoginPage类的外部添加委托,但这无济于事。
有人会这么友善地指出我在犯什么愚蠢的错误吗?
MainPage
定义为Xamarin.Forms.Page
。此类没有名为的属性OnAuthenticated
。因此,错误。您必须LoginPage
先将的实例存储在该类型的变量中,然后才能将其分配给该变量,MainPage
以便可以访问该类中定义的属性和方法:
var loginPage = new LoginPage();
loginPage.OnAuthenticated += new EventHandler(Authenticated);
MainPage = loginPage;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句