我正在使用CustomMessageBox
Silverlight Toolkit for Windows Phone 中的控件。当按照教程中的建议传递匿名回调(MS 中的“代表”?)时,我引用了代码隐藏文件中定义的页面部分类的成员。当我在运行时到达逻辑时,它会构建和部署但崩溃。
我从 VS 调试器中注意到,回调内的范围仅包含来自页面部分类的 XAML 端的成员,而不包含来自代码隐藏文件的成员。这意味着我所指的成员不在范围内(即使 Intellisense 可以使用它)。此外,我不能NavigationService.Navigate
在回调中使用。
如何从回调中调用包含类中的代码?
这是代码,
// This is a member of the partial class which inherits from
// PhoneApplicationPage
private void cancelBtn_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
if ((this.nameTextBox.Text != String.Empty) || (bool)this.protectCheckBox.IsChecked)
{
CustomMessageBox messageBox = new CustomMessageBox()
{
Caption = "Confirm leave page",
Message = "You have entered some profile data which will be lost if you leave this page. Are you sure you want to leave this page?",
LeftButtonContent = "no",
RightButtonContent = "yes"
};
messageBox.Dismissed += (s1, e1) =>
{
if (e1.Result == CustomMessageBoxResult.RightButton)
{
// Both of these raise an exception ...
GoToProfilePage();
//NavigationService.Navigate(new Uri("/View/MainPage.xaml", UriKind.Relative));
// Inspecting the debugger here shows only half the expected
// number of methods in the 'this' object - specifically only
// those defined in XAML
}
};
messageBox.Show();
}
else
{
// This works fine
GoToProfilePage();
}
}
GoToProfilePage()
代码隐藏文件中的方法在哪里。
这是例外,
System.NullReferenceException was unhandled
Message=NullReferenceException
StackTrace:
at Microsoft.Phone.Controls.CustomMessageBox.ClosePopup(Boolean restoreOriginalValues)
at Microsoft.Phone.Controls.CustomMessageBox.c__DisplayClass4.b__1(Object s, EventArgs e)
at Microsoft.Phone.Controls.Transition.OnCompleted(Object sender, EventArgs e)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
更新
看起来代码已执行,只有当委托完成时才会引发空引用异常,因此范围可能不是问题......