[英]Why am I getting a NullReferenceException when subscribing to event in Xamarin.Forms, but only on iOS?
I have a page - Page A, that has a method that subscribes to an event on another page - Page B. I figured I could instantiate Page B in my code in Page A before having my method in Page A subscribe to the event in Page B, and then finally pushing Page B to the navigation stack. 我有一个页面-页面A,该页面具有一种方法可以订阅另一个页面-页面B上的事件。我认为可以先在页面A的代码中实例化页面B,然后让页面A的方法订阅页面中的事件B,然后最终将Page B推入导航堆栈。
Unfortunately, I keep getting a NullReferenceException on the line in which the method subscribes to the event when I test my application on iOS. 不幸的是,当我在iOS上测试我的应用程序时,在该方法订阅事件的那一行上,我一直收到NullReferenceException。 The code runs perfectly fine when I deploy and test as an Android application, but I always get the NullReferenceException on iOS.
当我将其作为Android应用程序进行部署和测试时,代码运行得非常好,但是我总是在iOS上收到NullReferenceException。 What's causing this exception to be thrown, and how can I fix it?
是什么导致引发此异常,我该如何解决? Why is it platform specific to iOS?
为什么平台专用于iOS?
Code on Page A 网页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!";
}
Code on Page B 网页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);
}
}
}
Call Stack before subscribing method to event: 在订阅方法之前,先调用Stack:
0xC0 in appFBLA2019.CreateAccountPage.ButtonCreateAccount_Clicked at C:\\Users\\chung\\source\\repos\\appFBLA2019\\appFBLA2019\\appFBLA2019\\CreateAccountPage.xaml.cs:30,21 C#
appFBLA2019.CreateAccountPage.ButtonCreateAccount_Click中的0xC0在C:\\ Users \\ chung \\ source \\ repos \\ appFBLA2019 \\ appFBLA2019 \\ appFBLA2019 \\ CreateAccountPage.xaml.cs:30,21 C#
Call stack after subscribing method to event: 在订阅事件方法之后调用堆栈:
0x1B8 in appFBLA2019.CreateAccountPage.ButtonCreateAccount_Clicked at C:\\Users\\chung\\source\\repos\\appFBLA2019\\appFBLA2019\\appFBLA2019\\CreateAccountPage.xaml.cs:39,13 C#
appFBLA2019.CreateAccountPage.ButtonCreateAccount_Click中的0x1B8在C:\\ Users \\ chung \\ source \\ repos \\ appFBLA2019 \\ appFBLA2019 \\ appFBLA2019 \\ CreateAccountPage.xaml.cs:39,13 C#
Upon further testing, I noticed that this issue occurs on both iOS and Android, but ONLY when running the application with Xamarin Live Player. 经过进一步测试,我注意到此问题在iOS和Android上均会发生,但仅在使用Xamarin Live Player运行应用程序时才会发生。 I contacted Microsoft and they pointed out that Xamarin Live Player unfortunately has limitations.
我联系了Microsoft,他们指出Xamarin Live Player不幸地有局限性。 Deploying directly to a device causes no issues, and the code runs fine.
直接部署到设备不会造成任何问题,并且代码运行良好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.