gpt4 book ai didi

.net - 为什么PasswordBox控件会使WPF应用程序崩溃?

转载 作者:行者123 更新时间:2023-12-03 08:47:05 30 4
gpt4 key购买 nike

我度过了美好的一天,试图找出PasswordBox为何导致我这么多问题。
我有一个可以在我测试过的每台机器上运行的应用程序。现在,它已经安装在其他计算机上,每当我在PasswordBox中单击时,它就会崩溃。
我什至制作了一个仅带有TextBoxPasswordBox控件的测试应用程序,以查看崩溃是否是由我的应用程序中的任何底层代码引起的,但事实并非如此。我单击TextBox,它工作正常。请允许我在其中输入文字。但是,一旦我单击PasswordBox,应用程序就会崩溃。

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

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

  • 现在,如果出现任何错误,这应该向我显示 MessageBox。最糟糕的是,这些事件没有引发任何错误。
    我在应用程序中设置了一些日志记录,但出现了如下异常:
  • 对象引用未设置为对象
  • 的实例
  • 尝试读取或写入 protected 内存。这通常表明其他内存已损坏。

  • 我已经为 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控制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, bool(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, bool(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, bool(boolean) 值, bool(boolean) 值, bool(boolean) 值)
    在System.Windows.Input.KeyboardDevice.Focus处(System.Windows.DependencyObject, bool(boolean) 值, bool(boolean) 值, bool(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, bool(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, bool(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, bool(boolean) ByRef)
    在System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr,Int32,IntPtr,IntPtr, bool(boolean) ByRef)
    在MS.Win32.HwndWrapper.WndProc(IntPtr,Int32,IntPtr,IntPtr, bool(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()

    最佳答案

    似乎.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的字体,该字体无法在该计算机上找到,因此是您的例外。

    关于.net - 为什么PasswordBox控件会使WPF应用程序崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8821648/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com