Custom Events in Xamarin Page c#

10,327

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;
Share:
10,327

Related videos on Youtube

RVandersteen
Author by

RVandersteen

Updated on May 25, 2022

Comments

  • RVandersteen
    RVandersteen about 2 years

    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 ?

  • RVandersteen
    RVandersteen over 8 years
    Thanks alot ! Although binding MainPage -> LoginPage MainPage = new LoginPage(); fires an error complaining about a root view controller.
  • RVandersteen
    RVandersteen over 8 years
    I see you edited your awser, works perfectly like that. Thanks again