为什么PasswordBox控件会导致我的WPF应用程序崩溃?

时间:2012-01-11 15:14:59

标签: .net wpf crash controls passwordbox

我花了很多时间试图找出PasswordBox导致我这么多问题的原因 我有一个应用程序,可以在我测试过的每台机器上运行。现在它已安装在其他机器上,只要我在PasswordBox内点击它就会崩溃 我甚至使用TextBoxPasswordBox控件创建了一个测试应用程序,以查看崩溃是否是由我的应用程序中的任何底层代码引起的,但结果并非如此。我点击TextBox,它运行正常。允许我在其中键入文本。但是一旦我点击PasswordBox,应用程序崩溃了。

我设置这些事件以捕获任何未处理的异常:

  • Application.Current.DispatcherUnhandledException
  • AppDomain.CurrentDomain.UnhandledException
  • AppDomain.CurrentDomain.FirstChanceException

现在,如果发生任何错误,这应该显示MessageBox。最糟糕的是,这些事件没有任何错误 我在我的应用程序中设置了一些日志记录,我得到了例外:

  • 对象引用未设置为对象的实例
  • 尝试读取或写入受保护的内存。这通常表明其他内存已损坏。

我已经为PasswordBox设置了一个事件来测试它是否在崩溃之前得到了焦点而且确实如此,所以我不认为Object引用异常是指PasswordBox控制。
我一直在各处搜索,以查找是否有其他人遇到过这些问题,并且只看到TextBox控件及其焦点问题的问题,这些问题应该在.NET 4.0中得到修复

哦!这让我想起了。我正在使用WPF .NET 4.0。

非常感谢任何帮助。

测试代码是:

private void Application_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
    Application.Current.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler((x, y) => { ShowError(x, y); });  

    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler((x, y) => { ShowError(x, y); });
    AppDomain.CurrentDomain.FirstChanceException += new EventHandler<System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs>((x, y) => { ShowError(x, y); });
}

private void ShowError(object sender, object o)
{
    Exception ex = null;
    if (o.GetType() == typeof(FirstChanceExceptionEventArgs))
    {
        ex = ((FirstChanceExceptionEventArgs)o).Exception;
    }
    else if (o.GetType() == typeof(DispatcherUnhandledExceptionEventArgs))
    {
        ex = ((DispatcherUnhandledExceptionEventArgs)o).Exception;
    }
    else if (o.GetType() == typeof(UnhandledExceptionEventArgs))
    {
        ex = (Exception)((UnhandledExceptionEventArgs)o).ExceptionObject;
    }
    MessageBox.Show(ex.Message + "\n\r" + ex.StackTrace, "Error at: " + ex.Source, MessageBoxButton.OK, MessageBoxImage.Error);
}

和Xaml:

<Window x:Class="WPF_Control_Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="200" Width="300">
    <Grid>
        <StackPanel Orientation="Vertical" HorizontalAlignment="Left">
            <StackPanel Orientation="Horizontal" Width="260">
                <TextBlock Text="TextBox: " TextAlignment="Right" Width="80" Height="Auto" VerticalAlignment="Center" />
                <TextBox Name="textBox" Width="120" VerticalAlignment="Center" />
            </StackPanel>
            <StackPanel Orientation="Horizontal" Width="260">
                <TextBlock Text="PasswordBox: " TextAlignment="Right" Width="80" Height="Auto" VerticalAlignment="Center" />
                <PasswordBox Name="passwordBoxTest" Width="120" VerticalAlignment="Center" />
            </StackPanel>
        </StackPanel>
    </Grid>
</Window>

事件查看器

以下是计算机上事件查看器的例外情况:

应用程序:WPF Control Test.exe
框架版本:v4.0.30319
描述:应用程序通过System.Environment.FailFast(字符串消息)请求进程终止 消息:无法恢复的系统错误 堆栈:
    在System.Environment.FailFast(System.String)
    在MS.Internal.Invariant.FailFast(System.String,System.String)
    在System.Windows.Media.FontFamily.get_FirstFontFamily()
    在System.Windows.Documents.TextSelection.CalculateCaretRectangle(System.Windows.Documents.ITextSelection,System.Windows.Documents.ITextPointer)
    在System.Windows.Documents.TextSelection.UpdateCaretStateWorker(System.Object)
    在System.Windows.Documents.TextSelection.UpdateCaretState(System.Windows.Documents.CaretScrollMethod)
    在System.Windows.Documents.TextSelection.EnsureCaret(Boolean,System.Windows.Documents.CaretScrollMethod)
    在System.Windows.Documents.TextSelection.System.Windows.Documents.ITextSelection.UpdateCaretAndHighlight()
    在System.Windows.Documents.TextEditor.OnGotKeyboardFocus(System.Object,System.Windows.Input.KeyboardFocusChangedEventArgs)
    在System.Windows.Controls.PasswordBox.OnGotKeyboardFocus(System.Windows.Input.KeyboardFocusChangedEventArgs)
    在System.Windows.UIElement.OnGotKeyboardFocusThunk(System.Object,System.Windows.Input.KeyboardFocusChangedEventArgs)
    在System.Windows.Input.KeyboardFocusChangedEventArgs.InvokeEventHandler(System.Delegate,System.Object)
    在System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate,System.Object)
    在System.Windows.RoutedEventHandlerInfo.InvokeHandler(System.Object,System.Windows.RoutedEventArgs)
    在System.Windows.EventRoute.InvokeHandlersImpl(System.Object,System.Windows.RoutedEventArgs,Boolean)
    在System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject,System.Windows.RoutedEventArgs)
    在System.Windows.UIElement.RaiseTrustedEvent(System.Windows.RoutedEventArgs)
    在System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs,Boolean)
    在System.Windows.Input.InputManager.ProcessStagingArea()
    在System.Windows.Input.InputManager.ProcessInput(System.Windows.Input.InputEventArgs)
    在System.Windows.Input.KeyboardDevice.ChangeFocus(System.Windows.DependencyObject,Int32)
    在System.Windows.Input.KeyboardDevice.TryChangeFocus(System.Windows.DependencyObject,System.Windows.Input.IKeyboardInputProvider,Boolean,Boolean,Boolean)
    在System.Windows.Input.KeyboardDevice.Focus(System.Windows.DependencyObject,Boolean,Boolean,Boolean)
    在System.Windows.Input.KeyboardDevice.Focus(System.Windows.IInputElement)
    在System.Windows.UIElement.Focus()
    在System.Windows.Documents.TextEditorMouse.MoveFocusToUiScope(System.Windows.Documents.TextEditor)
    在System.Windows.Documents.TextEditorMouse.OnMouseDown(System.Object,System.Windows.Input.MouseButtonEventArgs)
    在System.Windows.Controls.PasswordBox.OnMouseDown(System.Windows.Input.MouseButtonEventArgs)
    在System.Windows.UIElement.OnMouseDownThunk(System.Object,System.Windows.Input.MouseButtonEventArgs)
    在System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(System.Delegate,System.Object)
    在System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate,System.Object)
    在System.Windows.RoutedEventHandlerInfo.InvokeHandler(System.Object,System.Windows.RoutedEventArgs)
    在System.Windows.EventRoute.InvokeHandlersImpl(System.Object,System.Windows.RoutedEventArgs,Boolean)
    在System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject,System.Windows.RoutedEventArgs)
    在System.Windows.UIElement.RaiseTrustedEvent(System.Windows.RoutedEventArgs)
    在System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs,Boolean)
    在System.Windows.Input.InputManager.ProcessStagingArea()
    在System.Windows.Input.InputManager.ProcessInput(System.Windows.Input.InputEventArgs)
    在System.Windows.Input.InputProviderSite.ReportInput(System.Windows.Input.InputReport)
    在System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr,System.Windows.Input.InputMode,Int32,System.Windows.Input.RawMouseActions,Int32,Int32,Int32)
    在System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr,MS.Internal.Interop.WindowMessage,IntPtr,IntPtr,Boolean ByRef)
    在System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr,Int32,IntPtr,IntPtr,Boolean ByRef)
    在MS.Win32.HwndWrapper.WndProc(IntPtr,Int32,IntPtr,IntPtr,Boolean ByRef)
    在MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object)
    在System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate,System.Object,Int32)
    在MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object,System.Delegate,System.Object,Int32,System.Delegate)
    在System.Windows.Threading.Dispatcher.InvokeImpl(System.Windows.Threading.DispatcherPriority,System.TimeSpan,System.Delegate,System.Object,Int32)
    在MS.Win32.HwndSubclass.SubclassWndProc(IntPtr,Int32,IntPtr,IntPtr)
    在MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef)
    在System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame)
    在System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame)
    在System.Windows.Application.RunDispatcher(System.Object)
    在System.Windows.Application.RunInternal(System.Windows.Window)
    在System.Windows.Application.Run(System.Windows.Window)
    在System.Windows.Application.Run()
    在WPF_Control_Test.App.Main()

2 个答案:

答案 0 :(得分:3)

似乎.Net Framework无法找到合适的默认字体。我不知道为什么会发生这种情况,但FontFamily中的以下内部代码行包括抛出异常的行:

fontFamily = this.FindFirstFontFamilyAndFace(
    ref normal, ref normal2, ref normal3);
if (fontFamily == null)
{
    // NullFontFamilyCanonicalName
    //  = CanonicalFontFamilyReference.Create(null, "#ARIAL");
    fontFamily = FontFamily.LookupFontFamily(
        FontFamily.NullFontFamilyCanonicalName);
    Invariant.Assert(fontFamily != null); // <- Your exception
}

显然,如果你没有指定字体,它将寻找一个基于Arial的字体,它无法在该机器上找到,因此你的例外。

答案 1 :(得分:1)

我在Windows Embedded 7上遇到了同样的问题,并且在链接中提到了评论;

http://social.msdn.microsoft.com/Forums/ar/wpf/thread/fc2c9a54-8f66-4f1a-82be-cb40ada5fba5

我只是将以下字体复制到“c:\ windows \ fonts”目录,问题就解决了;

  • Arial(TrueType)
  • Courier New(TrueType)
  • Times New Roman(TrueType)