【问题标题】:NullReferenceException with CustomMessageBox带有 CustomMessageBox 的 NullReferenceException
【发布时间】:2013-01-04 06:04:25
【问题描述】:

我对 WP Toolkit 中的 CustomMessageBox 有疑问。目前,我的代码会在每两次单击按钮时启动应用评分提示。

Dispatcher.BeginInvoke(() =>
{
    if (rtcount == 2 && (AppSettings.ShowAgainSetting == true))
    {
        CheckBox checkBox = new CheckBox()
        {
            Content = "Do not ask me again",
            Margin = new Thickness(0, 14, 0, -2)
        };

        TiltEffect.SetIsTiltEnabled(checkBox, true);

        CustomMessageBox messageBox = new CustomMessageBox()
        {
            Caption = "Would you like to rate and review this application?",
            Message =
                "Thank you for using my app."
                + Environment.NewLine + Environment.NewLine
                + "If you've been enjoying the app we'd love if you could leave us a rating in the Store. Would you mind spending a couple of seconds to rate (and/or) review this application?",
            Content = checkBox,
            LeftButtonContent = "ok",
            RightButtonContent = "not now",
        };

        messageBox.Dismissed += (s1, e1) =>
        {
            switch (e1.Result)
            {
                case CustomMessageBoxResult.LeftButton:
                    if ((bool)checkBox.IsChecked)
                    {
                        MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask();
                        marketplaceReviewTask.Show();
                        AppSettings.ShowAgainSetting = false;
                    }
                    else
                    {
                        MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask();
                        marketplaceReviewTask.Show();
                    }
                    break;
                case CustomMessageBoxResult.RightButton:
                    if ((bool)checkBox.IsChecked)
                    {
                        AppSettings.ShowAgainSetting = false;
                    }
                    else
                    {
                    }
                    break;
                case CustomMessageBoxResult.None:
                    if ((bool)checkBox.IsChecked)
                    {
                        AppSettings.ShowAgainSetting = false;
                    }
                    else
                    {
                    }
                    break;
                default:
                    break;
            }
        };

        messageBox.Show();
        rtcount = 0;
    }
});

rtcount++;

除了实际启动 MarketplaceReviewTask 的选项外,所有选项似乎都可以正常工作。任务正确启动,但在恢复应用程序时我遇到了 NullReferenceException:

{System.NullReferenceException: NullReferenceException 在 Microsoft.Phone.Controls.CustomMessageBox.ClosePopup(布尔 restoreOriginalValues) 在 Microsoft.Phone.Controls.CustomMessageBox.c_DisplayClass4.b_1(Object s, EventArgs e) 在 Microsoft.Phone.Controls.Transition.OnCompleted(对象发送者,EventArgs e) 在 MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex,委托 handlerDelegate,对象发送者,对象参数) 在 MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)}

我该如何解决这个问题?更改为 Coding4Fun Toolkit 中的 MessagePrompt 是最后的手段。

【问题讨论】:

标签: c# windows-phone-7 windows-phone-8 windows-phone nullreferenceexception


【解决方案1】:

遇到同样的问题。 CustomMessageBox.cs 中有一个错误。当它为 null 时,他们调用了 popup。

private void ClosePopup(bool restoreOriginalValues)
{
    _popup.IsOpen = false;

已在最新版本中修复http://phone.codeplex.com

【讨论】:

    【解决方案2】:

    我对被解雇的事件使用了一个布尔值来定义按下了哪个按钮。然后我实现了我将在 Unloaded 事件中的被解雇事件中实现的代码。这似乎解决了这个问题。

            messageBox.Dismissed += (s1, e1) =>
            {
                switch (e1.Result)
                {
                    case CustomMessageBoxResult.LeftButton:
                        {
                            delete = true ;
                        }
                        break;
                    case CustomMessageBoxResult.RightButton:
                        break;
                    case CustomMessageBoxResult.None:
                        break;
                    default:
                        break;
                }
            };
    
            messageBox.Unloaded += (s1, e1) =>
            {
                if (delete)
                    DeleteWorkout();
            };
    

    【讨论】:

      【解决方案3】:

      我无法弄清楚这一点,推出更新对我来说非常重要,所以我继续前进,好吧,有点“被骗”了。我已经“处理”了异常:

      if (e.ExceptionObject.Message.ToString() == "NullReferenceException")
              {
                  e.Handled = true;
                  return;
              }
      

      Application_UnhandledException下。

      如果有人对此有更好的解决方法,我很乐意听到。

      【讨论】:

        【解决方案4】:

        我认为问题可能出在您的 Dismissed 处理程序中。我不确定CustomMessageBox 是如何实现的,但checkBox 属性可能为null。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-08-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-26
          • 2013-05-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多