我正在使用Silverlight Toolkit for Windows Phone的CustomMessageBox
控件。当传递一个匿名回调(MS中的'委托代理')作为suggested in the tutorials时,我引用了代码隐藏文件中定义的页面部分类的成员。当我在运行时达到逻辑时,这会构建和部署,但会崩溃。如何在CustomMessageBox中使用NavigationService.Navigate Dismissed事件处理程序?
我从VS调试器注意到,回调中的范围只包含来自页面部分类的XAML一侧的成员,而不包含来自代码隐藏文件的成员。这意味着我所指的成员不在该范围内(尽管智能感知适用于该范围)。此外,我不能在回调中使用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)
更新
它看起来像被执行的代码,只有当委托完成的空引用异常升高,从而可能不范围有问题...
同样是发生在我一个问题,我通过添加的NuGet工具包的版本十月。我无法让它工作 – Berni