Custom Events on Xamarin C # Page

I am currently facing the following problem:

I am trying to fire an event when the user has entered valid credentials so that I can switch the page, etc.

Problem: for some reason I can’t connect to the event (although I’m sure it will be something stupid).

The class that triggers 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 create an application, I get:

The type "Xamarin.Forms.Page" does not contain a definition for "OnAuthenticated" and no OnAuthenticated extension method

I tried to add a delegate inside the LoginPage class, outside of it, but that doesn't help.

Can anyone be so kind as to point me to the dumb mistake I am making?

+4
1

MainPage Xamarin.Forms.Page. , OnAuthenticated. . LoginPage , MainPage, , :

var loginPage = new LoginPage();
loginPage.OnAuthenticated += new EventHandler(Authenticated); 
MainPage = loginPage;
+5

Source: https://habr.com/ru/post/1613669/


All Articles