gpt4 book ai didi

c# - 如何在 CustomMessageBox Dismissed 事件处理程序中使用 NavigationService.Navigate?

转载 作者:太空宇宙 更新时间:2023-11-03 11:14:44 26 4
gpt4 key购买 nike

我正在使用 Silverlight Toolkit for Windows Phone 中的 CustomMessageBox 控件。将匿名回调(MS 语言中的“委托(delegate)”?)传递为 suggested in the tutorials 时我引用了代码隐藏文件中定义的页面部分类的成员。这会构建和部署,但当我在运行时到达逻辑时崩溃。

我从 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() 是代码隐藏文件中的一个方法。

这是异常(exception),


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)

更新

看起来代码已经执行了,只有当委托(delegate)完成时才会引发空引用异常,所以它可能不是作用域的问题......

最佳答案

好的,明白了。最新版本的 Windows Toolkit .dll(包括 CustomMessageBox)需要在我的解决方案中引用。

显然有一个旧版本的 Windows 工具包包含在某处的默认引用中,因为 ContextMenu 和 CustomMessageBox 至少部分地事先工作,这非常令人困惑......

为了添加更新的引用,我在一个单独的项目中从源代码构建了 .dll,并将其复制到我的项目中。我从 VS 中的 Reference 右键菜单中添加了一个引用,并浏览到 debug\bin 目录中的文件。

关于c# - 如何在 CustomMessageBox Dismissed 事件处理程序中使用 NavigationService.Navigate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13004196/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com