2

我正在使用CustomMessageBoxSilverlight 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)

更新

看起来代码已执行,只有当委托完成时才会引发空引用异常,因此范围可能不是问题......

4

2 回答 2

1

一个快速的技巧是在工具包源代码中注释 CustomMessageBox.cs 文件的第 657 行并再次编译。然后在您的应用程序中引用这个新的 dll。

...
private void ClosePopup(bool restoreOriginalValues)
        {
            // Remove the popup.

            _popup.IsOpen = false;
            //_popup = null; <-- THIS IS LINE 657
...

http://phone.codeplex.com/workitem/10575中发布了一个问题

于 2012-11-12T23:01:22.803 回答
1

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

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

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

于 2012-10-22T14:43:00.110 回答