パスワードボックス制御がWPFアプリケーションをクラッシュさせるのはなぜですか?

StackOverflow https://stackoverflow.com/questions/8821648

質問

私はなぜなのかを理解しようとして良い一日を過ごしました PasswordBox 私に非常に多くの問題を引き起こしています。
テストしたすべてのマシンで動作するアプリケーションがあります。今では、内部をクリックするたびにクラッシュしている他のマシンにインストールされています PasswordBox.
私もaだけでテストアプリケーションを作成しました TextBoxPasswordBox クラッシュが私のアプリケーションの根本的なコードによって引き起こされたかどうかを確認するための制御ですが、これは事実ではないことが判明しました。をクリックします TextBox, 、そしてそれは正常に動作します。内部にテキストを入力できます。しかし、私がクリックするとすぐに PasswordBox, 、アプリケーションがクラッシュします。

これらのイベントを設定して、未処理の例外をキャッチします。

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

今、これは私に示すはずです MessageBox エラーが発生した場合。最悪の部分は、これらのイベントにエラーが投げかけられていないことです。
アプリケーションにログインしたログを設定しましたが、次のような例外がありました。

  • オブジェクト参照はオブジェクトのインスタンスに設定されていません
  • 保護されたメモリの読み取りまたは書き込みを試みました。多くの場合、これは他のメモリが破損していることを示しています。

私はイベントを設定しました PasswordBox クラッシュする前に焦点が当てられているかどうかをテストするために、オブジェクト参照の例外が次のことを指すとは思いません 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(String Message)を介してプロセス終了を要求しました。
メッセージ:回復不可能なシステムエラー。
スタック:
at System.Environment.Failfast(System.String)
Ms.internal.invariant.failfast(System.String、System.String)で
at System.Windows.Media.FontFamily.get_firstfontfamily()
at System.Windows.Documents.TextSelection.CalculateCaretrectangle(System.Windows.Documents.IteXtelection、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()
at System.Windows.Documents.TextEditor.OngotKeyBoardFocus(System.Object、System.Windows.input.KeyBoardFocusChangedEventargs)
at System.Windows.Controls.PassWordBox.OngotKeyBoardFocus(System.Windows.input.KeyBoardFOCUSCHANGEDEVENTARGS)
at System.Windows.uielement.OngotKeyBoardFocustunk(System.Object、System.Windows.input.keyboardfocuschangedeventargs)
at System.windows.input.keyboardfocuschangedeventargs.invokeeventhandler(system.delegate、system.object)
at System.Windows.Routedeventargs.invokeHandler(System.Delegate、System.Object)
at System.Windows.RoutedEventHandlerinfo.invokeHandler(System.Object、System.Windows.RoutedEventargs)
at System.Windows.Eventroute.InvokeHandlerSimpl(System.Object、System.Windows.RouteDeventargs、boolean)
at System.Windows.uielement.raiseeventimpl(system.windows.dependencyobject、system.windows.routedeventargs)
at System.Windows.uielement.raisetrustedevent(system.windows.routedeventargs)
at System.Windows.uielement.raiseevent(system.windows.routedeventargs、boolean)
at System.Windows.input.inputmanager.processStagingarea()
at System.Windows.input.inputmanager.processinput(system.windows.input.inputeventargs)
at System.Windows.input.KeyBoardDevice.ChangeFocus(System.Windows.DependencyObject、int32)
at System.Windows.input.KeyBoardDevice.TryChangeFocus(System.Windows.DependencyObject、System.Windows.input.ikeyBoardInputProvider、boolean、boolean、boolean)
at System.Windows.input.keyboarddevice.focus(system.windows.dependencyObject、boolean、boolean、boolean)
at System.Windows.input.keyboarddevice.focus(system.windows.iinputelement)
at System.Windows.uielement.focus()
System.windows.documents.texteditormouse.movefocustouiscope(system.windows.documents.texteditor)で
System.windows.documents.texteditormouse.onmousedown(system.object、system.windows.input.moutebuttoneventargs)
System.windows.controls.passwordbox.onmousedown(system.windows.input.mousebuttoneventargs)
at System.Windows.uielement.onmousedownthunk(system.object、system.windows.input.mousebuttoneventargs)
at System.Windows.input.MouseButtoneventargs.invokeEventhandler(System.Delegate、System.Object)
at System.Windows.Routedeventargs.invokeHandler(System.Delegate、System.Object)
at System.Windows.RoutedEventHandlerinfo.invokeHandler(System.Object、System.Windows.RoutedEventargs)
at System.Windows.Eventroute.InvokeHandlerSimpl(System.Object、System.Windows.RouteDeventargs、boolean)
at System.Windows.uielement.raiseeventimpl(system.windows.dependencyobject、system.windows.routedeventargs)
at System.Windows.uielement.raisetrustedevent(system.windows.routedeventargs)
at System.Windows.uielement.raiseevent(system.windows.routedeventargs、boolean)
at System.Windows.input.inputmanager.processStagingarea()
at System.Windows.input.inputmanager.processinput(system.windows.input.inputeventargs)
at 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)
at System.Windows.interop.hwndmouseinputprovider.filtermessage(intptr、ms.internal.interop.windowmessage、intptr、intptr、boolean byref)
at 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)で
at 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)で
at System.Windows.Application.RundisPatcher(System.Object)
at System.Windows.Application.runinternal(System.Windows.Window)
System.windows.application.run(system.windows.window)
at System.Windows.Application.run()
wpf_control_test.app.main()で

役に立ちましたか?

解決

.NETフレームワークは、適切なデフォルトフォントを見つけることができないようです。なぜこれが起こっているのかわかりませんが、次の行は 内部 からのコード 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
}

どうやら、フォントを指定しないと、そのマシンで見つけることができないアリアベースのフォントを探すため、例外があります。

他のヒント

私はこの同じ問題を抱えており、7ボックスに埋め込まれたWindowsで、リンクに記載されているように、コメントがありました。

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

次のフォントを介して「C: Windows fonts」ディレクトリにコピーするだけで、問題は解決しました。

  • arial(trueType)
  • クーリエニュー(TrueType)
  • Times New Roman(TrueType)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top