2012-10-22 22 views
2

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

更新

它看起来像被执行的代码,只有当委托完成的空引用异常升高,从而可能不范围有问题...

回答

1

好吧,算出来。 Windows Toolkit .dll的最新版本(其中包括CustomMessageBox)需要我的解决方案中的参考。

显然存在被包含在默认引用某个地方,因为这两个文本菜单和CustomMessageBox工作至少部分地提前,这是非常令人困惑的的Windows工具包的旧版本...

要添加更新后的基准,我在单独的项目中从源代码构建.dll文件并将其复制到我的项目中。我添加了VS中参考右键菜单的参考,并浏览到debug\bin目录中的文件。

+0

同样是发生在我一个问题,我通过添加的NuGet工具包的版本十月。我无法让它工作 – Berni

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

相关问题