Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Events in Xamarin Page c#

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 ?

like image 834
RVandersteen Avatar asked Oct 29 '15 14:10

RVandersteen


People also ask

What is custom event in C#?

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.

How do you raise an event in C#?

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 .

Can events be static C#?

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.

How Use delegates and events in C#?

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.


1 Answers

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;
like image 118
Wosi Avatar answered Oct 10 '22 00:10

Wosi