使用CustomMessageBox的NullReferenceException

时间:2013-01-21 07:46:26

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

我在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(Boolean restoreOriginalValues)      在Microsoft.Phone.Controls.CustomMessageBox。<> c_ DisplayClass4.b _1(Object s,EventArgs e)      在Microsoft.Phone.Controls.Transition.OnCompleted(Object sender,EventArgs e)      在MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex,Delegate handlerDelegate,Object sender,Object args)      在MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj,IntPtr unmanagedObjArgs,Int32 argsTypeIndex,Int32 actualArgsTypeIndex,String eventName)}

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

4 个答案:

答案 0 :(得分:1)

我无法弄清楚这一点,对我来说推出更新非常重要,所以我已经做得很好,“欺骗”了一点。我已经“处理”了例外:

if (e.ExceptionObject.Message.ToString() == "NullReferenceException")
        {
            e.Handled = true;
            return;
        }
<{1>}下的

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

        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 :(得分:0)

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

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

它已在最新版本http://phone.codeplex.com

中修复