提问人:ReignOfComputer 提问时间:1/21/2013 最后编辑:Mario SReignOfComputer 更新时间:10/9/2013 访问量:973
NullReferenceException 与 CustomMessageBox
NullReferenceException with CustomMessageBox
问:
我对 WP 工具包中的 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(布尔恢复原始值) 在 Microsoft.Phone.Controls.CustomMessageBox.<>c_DisplayClass4.b_1 (对象 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 工具包中更改为 MessagePrompt 是最后的手段。
答:
我认为问题可能出在您的处理程序上。我不确定如何实现,但该属性可能是 null。Dismissed
CustomMessageBox
checkBox
我无法弄清楚这一点,推出更新对我来说非常重要,所以我继续前进,好吧,“欺骗”了一点。我已经“处理”了异常:
if (e.ExceptionObject.Message.ToString() == "NullReferenceException")
{
e.Handled = true;
return;
}
下。Application_UnhandledException
如果有人对此有更好的解决方法,我很想听听。
我在 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();
};
有同样的问题。CustomMessageBox.cs 中存在一个错误。当弹出窗口为 null 时,他们调用了弹出窗口。
private void ClosePopup(bool restoreOriginalValues)
{
_popup.IsOpen = false;
此问题已在最新版本中修复 http://phone.codeplex.com
评论