首页 / C# / Xamarin中的自定义事件页面c#
Xamarin中的自定义事件页面c#
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Xamarin中的自定义事件页面c#,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1801字,纯文字阅读大概需要3分钟。
内容图文
![Xamarin中的自定义事件页面c#](/upload/InfoBanner/zyjiaocheng/827/0f90a4dd86d145adb3f149551405d865.jpg)
我目前面临以下问题:
我试图在用户输入有效凭据时触发事件,以便我可以切换页面等等.
问题是由于某些原因我无法挂钩事件(虽然我很确定这会是一些愚蠢的事情).
发射事件的类:
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";
}
});
});
}
}
}
并在’主类’
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");
}
}
}
当我尝试构建应用程序时,我得到:
类型’Xamarin.Forms.Page’不包含’OnAuthenticated’的定义,也没有扩展方法OnAuthenticated
我已经尝试在LoginPage类中添加一个委托,但它没有帮助.
任何人都可以如此友善地指出我正在制造什么愚蠢的错误?
解决方法:
MainPage定义为Xamarin.Forms.Page.此类没有名为OnAuthenticated的属性.因此错误.
在将LoginPage实例分配给MainPage之前,需要将该实例存储在该类型的变量中,以便能够访问该类中定义的属性和方法:
var loginPage = new LoginPage();
loginPage.OnAuthenticated += new EventHandler(Authenticated);
MainPage = loginPage;
内容总结
以上是互联网集市为您收集整理的Xamarin中的自定义事件页面c#全部内容,希望文章能够帮你解决Xamarin中的自定义事件页面c#所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。