Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Xamarin c页中的自定义事件#_C#_Events_Xamarin_Xamarin.forms - Fatal编程技术网

C# Xamarin c页中的自定义事件#

C# Xamarin c页中的自定义事件#,c#,events,xamarin,xamarin.forms,C#,Events,Xamarin,Xamarin.forms,我目前面临以下问题: 我试图在用户输入有效凭据时触发事件,以便切换页面等 问题是由于某种原因,我无法参与这项活动(尽管我很确定这将是一件愚蠢的事情) 触发事件的类: namespace B2B { public partial class LoginPage : ContentPage { public event EventHandler OnAuthenticated; public LoginPage () {

我目前面临以下问题:

我试图在用户输入有效凭据时触发事件,以便切换页面等

问题是由于某种原因,我无法参与这项活动(尽管我很确定这将是一件愚蠢的事情)

触发事件的类:

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";
                    }
                });

            });

        }
    }
}
当我尝试构建应用程序时,我得到:

键入“Xamarin.Forms.Page”不包含“OnAuthenticated”的定义,也不包含OnAuthenticated的扩展方法

我曾尝试在LoginPage类内部、外部添加一个委托,但没有帮助


有谁能告诉我我犯了什么愚蠢的错误吗?

MainPage
被定义为
Xamarin.Forms.Page
。此类没有名为OnAuthenticated的属性。因此出现了错误。 在将
LoginPage
的实例分配给
MainPage
之前,需要将该实例存储在该类型的变量中,以便访问该类中定义的属性和方法:

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

非常感谢!虽然binding MainPage->LoginPage MainPage=new LoginPage();引发一个错误,抱怨一个根视图控制器。我看到你编辑了你的awser,工作非常好。再次感谢
var loginPage = new LoginPage();
loginPage.OnAuthenticated += new EventHandler(Authenticated); 
MainPage = loginPage;