I currently am facing the following issue:
I am trying to fire an event when a user entered valid credentials so that I can switch page and so on.
The Issue is I am unable to hook into the event for some reason (altho I'm pretty sure it will be something stupid).
The Class firing the event:
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";
}
});
});
}
}
}
And in the '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");
}
}
}
When I try to build the application I get:
Type 'Xamarin.Forms.Page' does not containt a definition for 'OnAuthenticated' and no extension method OnAuthenticated
I've tried adding a delegate inside the LoginPage class, outside of it but it doesn't help.
Could anyone be so kind to point me out what stupid mistake I am making ?
This is an easy way to create custom events and raise them. You create a delegate and an event in the class you are throwing from. Then subscribe to the event from another part of your code. You have already got a custom event argument class so you can build on that to make other event argument classes.
Typically, to raise an event, you add a method that is marked as protected and virtual (in C#) or Protected and Overridable (in Visual Basic). Name this method On EventName; for example, OnDataReceived .
An event may be declared as a static event by using the static keyword. This makes the event available to callers at any time, even if no instance of the class exists. For more information, see Static Classes and Static Class Members. An event can be marked as a virtual event by using the virtual keyword.
A delegate is a way of telling C# which method to call when an event is triggered. For example, if you click a Button on a form, the program would call a specific method. It is this pointer that is a delegate. Delegates are good, as you can notify several methods that an event has occurred, if you wish so.
MainPage
is defined as Xamarin.Forms.Page
. This class has no property called OnAuthenticated
. Hence the error.
You need to store the instance of LoginPage
in a variable of that type before assigning it to MainPage
in order to have access to the properties and methods defined in that class:
var loginPage = new LoginPage();
loginPage.OnAuthenticated += new EventHandler(Authenticated);
MainPage = loginPage;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With