c# - Windows Phone 8 工具包中的 CustomMessageBox 抛出 NullPointerException

标签 c# windows-phone windows-phone-8

用于创建CustomMessageBox的代码:

CustomMessageBox 是一个属性,而不是对 Toolkit 中 C# 类的引用。

CustomMessageBox.Dismissed += (dismissSender, dismissedEvent) =>
{
    switch (dismissedEvent.Result)
    {
        case CustomMessageBoxResult.LeftButton:
            PlaceCall(clickedFavorite.Name, clickedFavorite.PhoneNo);
            break;
        case CustomMessageBoxResult.RightButton:
            HERE ---> SendText(clickedFavorite.PhoneNo);
            break;
    }
};

SendText() 方法的代码:

private void SendText(String phoneNo)
{
    var smsTask = new SmsComposeTask
                      {
                          To = phoneNo
                      };

    smsTask.Show();
}

事实是,当 SmsComposeTask 启动时,手机会导航到 SMS 应用程序,这是正确的。

如果用户随后决定使用硬件后退按钮返回,SMS 应用程序将关闭,手机会再次显示我的应用程序 - 但会立即关闭,这是由 NullPointerException 引起的:

   at Microsoft.Phone.Controls.CustomMessageBox.ClosePopup(Boolean restoreOriginalValues)
   at Microsoft.Phone.Controls.CustomMessageBox.<>c__DisplayClass4.<Dismiss>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)

我还尝试覆盖 OnBackKeyPress 事件,如下所示:

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
    if (CustomMessageBox != null && CustomMessageBox.IsEnabled)
    {
        e.Cancel = true;
    }
    else
    {
        base.OnBackKeyPress(e);
    }
}

有人知道该怎么做吗?

最佳答案

我已经找到了解决我自己问题的方法。我没有使用有缺陷的 CustomMessageBox,而是发现 Coding4Fun Windows Phone Toolkit它提供了一个迄今为止更稳定的消息框,称为 MessagePrompt - 以下是如何使用它。

创建按钮

var smsButton = new Button { Content = "SMS" };
smsButton.Click += (o, args) =>
{
    // do something
};

var buttonList = new List<Button>
{
    smsButton
};

创建实际的消息提示

var msgPrompt = new MessagePrompt
{
    Title = "Message Prompt Title",
    Body = new TextBlock { Text = "Text for the Body", FontSize = 25, TextWrapping = TextWrapping.Wrap },
    ActionPopUpButtons = buttonList
};

显示

msgPrompt.Show()

没有公牛

我在这个 MessagePrompt 中体验到的好处是,您不必绑定(bind)到两个静态 LeftRight 按钮自定义消息框

如果需要,您可以将 Body 属性设置为全新的 XAML 页面,从而使该控件变得灵活。

引用:Coding4Fun WP7 Message Prompt in depth

关于c# - Windows Phone 8 工具包中的 CustomMessageBox 抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14193059/

相关文章:

c# - 在 Windows Phone 中将字符串拆分为多个文本框

c# - 与 WhenAll 并行执行任务时的任务缓存

c# - 使用 VSTO 处理 Excel 数据?

c# - 当文件不存在时,File.Delete 不会抛出错误

windows-phone-8 - 在 LayoutMode=Grid 中使用 LongListSelector 进行延迟加载

c# - 如何在可移植类库中使用PCLStorage?

c# - 获取 Windows Phone 用户帐户

windows-phone-8 - 如何在 Windows Phone 8 中设置视频录制分辨率。

c# - 如何关闭从方法调用返回的 HttpWebResponse

c# - 如何在 C# 2012 版中正确清理 Excel 互操作对象