我有一个页面-页面A,该页面具有订阅另一页面-页面B上的事件的方法。我认为在页面A中的方法订阅页面A之前,可以在页面A的代码中实例化页面B。 B页中的事件,然后最终将B页推入导航堆栈。
不幸的是,当我在iOS上测试我的应用程序时,在该方法订阅事件的那一行上,我不断收到NullReferenceException。当我将其作为Android应用程序进行部署和测试时,代码运行得非常好,但是我总是在iOS上收到NullReferenceException。是什么导致引发此异常,我该如何解决?为什么平台专用于iOS?
页面A上的代码
var confirmationPage = new EmailConfirmationPage(username);
confirmationPage.EmailConfirmed += this.OnEmailConfirmed;
await this.Navigation.PushModalAsync(confirmationPage);
...
private void OnEmailConfirmed(object source, EventArgs args)
{
this.LabelMessage.Text = "Email Confirmed!";
}
B页上的代码
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace appFBLA2019
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class EmailConfirmationPage : ContentPage
{
private string username;
private string email;
public delegate void EmailConfirmedEventHandler(object source, EventArgs args);
public event EmailConfirmedEventHandler EmailConfirmed;
public EmailConfirmationPage(string username)
{
InitializeComponent();
this.username = username;
this.LabelTitle.Text = "Loading...";
GetEmail();
}
private void GetEmail()
{
try
{
ServerConnector.QueryDB($"getEmail/{this.username}/-");
this.email = ServerConnector.ReceiveFromDB();
this.LabelTitle.Text = $"Enter the confirmation code sent to {this.email.Split('/')[1]}";
}
catch
{
this.LabelMessage.Text = "Connection Error: Please Try Again.";
}
}
private async void ButtonConfirmEmail_Clicked(object sender, EventArgs e)
{
try
{
string userInputToken = this.EntryConfirmationCode.Text.Trim();
ServerConnector.QueryDB($"confirmEmail/{this.username}/{userInputToken}/-");
string returnData = ServerConnector.ReceiveFromDB();
if (returnData == "true/-")
{
OnEmailConfirmed();
await this.Navigation.PopModalAsync(true);
}
else
{
this.LabelMessage.Text = "Email could not be confirmed. Please try your code again.";
}
}
catch
{
this.LabelMessage.Text = "Connection Error: Please try again.";
}
}
private void ButtonFixEmail_Clicked(object sender, EventArgs e)
{
string newEmail = this.EntryChangeEmail.Text;
ServerConnector.QueryDB($"changeEmail/{this.username}/{newEmail}/-");
string result = ServerConnector.ReceiveFromDB();
if (result == "true/-")
{
this.LabelMessage.Text = $"Enter the confirmation code sent to {newEmail}";
}
else
{
this.LabelMessage.Text = $"Email could not be changed: {result.Split('/')[1]}";
}
}
private async void ButtonClose_Clicked(object sender, EventArgs e)
{
await this.Navigation.PopModalAsync(true);
}
protected virtual void OnEmailConfirmed()
{
EmailConfirmed?.Invoke(this, EventArgs.Empty);
}
}
}
在向事件订阅方法之前调用堆栈:
appFBLA2019.CreateAccountPage.ButtonCreateAccount_p中的0xC0在C:\ Users \ chung \ source \ repos \ appFBLA2019 \ appFBLA2019 \ appFBLA2019 \ CreateAccountPage.xaml.cs:30,21 C#
在订阅事件方法之后调用堆栈:
appFBLA2019.CreateAccountPage.ButtonCreateAccount_Click中的0x1B8在C:\ Users \ chung \ source \ repos \ appFBLA2019 \ appFBLA2019 \ appFBLA2019 \ CreateAccountPage.xaml.cs:39,13 C#
答案 0 :(得分:0)
在进一步测试中,我注意到此问题在iOS和Android上均会发生,但仅在使用Xamarin Live Player运行应用程序时才会发生。我联系了Microsoft,他们指出Xamarin Live Player不幸地有局限性。直接部署到设备不会造成任何问题,并且代码运行正常。