Xamarin 页面 c# 中的自定义事件
Custom Events in Xamarin Page c#
我目前面临以下问题:
我正在尝试在用户输入有效凭据时触发一个事件,以便我可以切换页面等。
问题是由于某种原因我无法连接到事件中(尽管我很确定这会很愚蠢)。
触发事件的Class:
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";
}
});
});
}
}
}
并且在 'main class'
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 class 内部添加委托,但在它外部没有帮助。
任何人都可以指出我犯的 愚蠢 错误吗?
MainPage
定义为 Xamarin.Forms.Page
。这个class没有属性叫OnAuthenticated
。因此错误。
在将 LoginPage
的实例分配给 MainPage
之前,您需要将其存储在该类型的变量中,以便访问 class:[=16= 中定义的属性和方法]
var loginPage = new LoginPage();
loginPage.OnAuthenticated += new EventHandler(Authenticated);
MainPage = loginPage;
我目前面临以下问题:
我正在尝试在用户输入有效凭据时触发一个事件,以便我可以切换页面等。
问题是由于某种原因我无法连接到事件中(尽管我很确定这会很愚蠢)。
触发事件的Class:
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";
}
});
});
}
}
}
并且在 'main class'
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 class 内部添加委托,但在它外部没有帮助。
任何人都可以指出我犯的 愚蠢 错误吗?
MainPage
定义为 Xamarin.Forms.Page
。这个class没有属性叫OnAuthenticated
。因此错误。
在将 LoginPage
的实例分配给 MainPage
之前,您需要将其存储在该类型的变量中,以便访问 class:[=16= 中定义的属性和方法]
var loginPage = new LoginPage();
loginPage.OnAuthenticated += new EventHandler(Authenticated);
MainPage = loginPage;