【问题标题】:WPF MessageBox is not displayed at all if long message is passed如果传递长消息,则根本不显示 WPF MessageBox
【发布时间】:2018-11-09 18:09:21
【问题描述】:

我遇到了一些带有错误消息的 MessageBox 根本没有显示的情况。经过仔细调查,我能够将问题缩小到以messageBoxText 传递很长字符串的情况。

在这种情况下,调用 MessageBox.Show 不会显示任何内容,而是返回 MessageBoxResult.No,并且在大多数情况下,Output 窗口中会显示一条消息 The thread 0xHEXNUMBER has exited with code 0 (0x0)。对我来说,这种方法默默地失败了。

这很奇怪 - WPF 是一项非常成熟的技术,我希望这段代码能够工作 according to spec 或抛出一些异常(例如 OutOfMemory、StackOverflowException)。我已经调试了程序,并且没有抛出和捕获任何管理或未管理的异常。

MessageBox 不显示的根本原因是什么?是否有一些简单的方法来调试这些事情(因为没有抛出或记录异常)。什么是 messageBoxText 长度限制以及它取决于什么(我可以在我的计算机上凭经验检查,但对于一个 OS/平台/.NET Framework 版本最多只能是结论性的)?

复制:

这是一个演示问题的代码(它可以与 Visual Studio 中的标准 WPF 应用程序模板一起使用)。

MainWindow.xaml:

<Window x:Class="LongMessageInMessageBox.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
  <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition Height="Auto" />
  </Grid.RowDefinitions>
  <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto" />
  </Grid.ColumnDefinitions>

  <Button Grid.Row="0" Grid.Column="0" Click="ShortMessage_Clicked">Short Message</Button>
  <Button Grid.Row="1" Grid.Column="0" Click="LongMessage_Clicked">Long Message</Button>

</Grid>

MainWindow.xaml.cs:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LongMessageInMessageBox
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        /// Method which shows that displaying short message works correctly
        private void ShortMessage_Clicked(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("ShortMessage_Clicked");
            var result = MessageBox.Show("Short Message");
            Console.WriteLine(result);
        }

        /// Method which shows that displaying a long message does not work
        private void LongMessage_Clicked(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("LongMessage_Clicked");
            var longTextBuilder = new System.Text.StringBuilder(10000);
            longTextBuilder.Append("Long Message \n");
            for (int i = 1; i <= 100000; i++)
            {
                longTextBuilder.Append(" ").Append(i);
            }
            var result = MessageBox.Show(longTextBuilder.ToString());
            Console.WriteLine(result);
        } 
    }
}

来自Output 窗口的示例:

ShortMessage_Clicked
OK
ShortMessage_Clicked
OK
LongMessage_Clicked
No
The thread 0x55bc has exited with code 0 (0x0).

【问题讨论】:

  • 我的猜测是它会像 WPF 经常那样默默地失败。你检查了吗?

标签: wpf messagebox


【解决方案1】:

WPF(和 WinForms)中的MessageBox 类包装了 Win32 MessageBox 函数,并且不会引发异常。在旧的 Win32 世界中,错误是通过设置 last-error code 来报告的,如果你不调用GetLastError 来检查,你可以继续前进而不知道有什么坏了。

来自Reference Source

//so it just translates the return code to a MessageBoxResult

MessageBoxResult result = Win32ToMessageBoxResult (UnsafeNativeMethods.MessageBox (new HandleRef (null, owner), messageBoxText, caption, style));

我没有测试Win32 MessageBox functionlpText字符串的容量。但如果容量限制是1024409665536 之类的,这并不奇怪,毕竟MessageBox 功能是为显示短消息而设计的。

【讨论】:

    猜你喜欢
    • 2020-04-07
    • 2017-09-28
    • 2016-12-19
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 2012-05-31
    • 2023-03-03
    • 1970-01-01
    相关资源
    最近更新 更多