Domanda

Ho passato una buona giornata cercando di capire il motivo per cui la PasswordBox mi sta causando tanti problemi.
Ho un'applicazione che funziona su ogni macchina che ho testato su. Ora è stato installato su altre macchine che si blocca ogni volta che clicco all'interno del PasswordBox.
Ho anche fatto un applicazione di test con un semplice controllo di TextBox e PasswordBox per vedere se l'incidente è stato causato da qualsiasi codice sottostante nella mia richiesta, ma questo si è rivelato non essere il caso. Clicco sul TextBox, e funziona benissimo. Mi permette di digitare il testo al suo interno. Ma non appena clicco sul PasswordBox, l'applicazione si blocca.

I istituito questi eventi per intercettare eventuali eccezioni non gestite:

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

Ora questo dovrebbe mostrarmi un MessageBox se si verificano errori. La parte peggiore è, è che non ci sono errori gettati a questi eventi.
Ho messo su un po 'di registrazione nella mia richiesta e mi sono eccezioni come:

  • Riferimento all'oggetto non è impostato su un'istanza di un oggetto
  • Tentativo di lettura o scrittura della memoria protetta. Ciò è spesso un'indicazione che l'altra memoria è corrotta.

che avevo creato un evento per il PasswordBox per verificare se ha ottenuto messa a fuoco prima dello schianto e lo ha fatto, quindi non credo che l'eccezione riferimento oggetto si riferisce al controllo PasswordBox.
Ho cercato dappertutto per trovare se qualcun altro ha avuto questi problemi e hanno visto solo problemi con il controllo TextBox e alle sue problematiche di messa a fuoco, che si suppone essere fissati in .NET 4.0

Oh! e che mi ricorda. Sto usando WPF .NET 4.0.

Qualsiasi aiuto sarebbe molto apprezzato.

Il codice di prova è:

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);
}

E il 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>

Visualizzatore eventi

Questa è la deroga al Visualizzatore eventi sulla macchina:

Applicazione: WPF controllo Test.exe
Versione quadro: v4.0.30319
Descrizione: La terminazione processo di applicazione richiesto attraverso System.Environment.FailFast (string messaggio)
. Messaggio:. Errore di sistema irreversibile
Stack:
a System.Environment.FailFast (System.String)
a MS.Internal.Invariant.FailFast (System.String, System.String)
a System.Windows.Media.FontFamily.get_FirstFontFamily ()
a System.Windows.Documents.TextSelection.CalculateCaretRectangle (System.Windows.Documents.ITextSelection, System.Windows.Documents.ITextPointer)
a System.Windows.Documents.TextSelection.UpdateCaretStateWorker (System.Object)
a System.Windows.Documents.TextSelection.UpdateCaretState (System.Windows.Documents.CaretScrollMethod)
a System.Windows.Documents.TextSelection.EnsureCaret (booleano, System.Windows.Documents.CaretScrollMethod)
a System.Windows.Documents.TextSelection.System.Windows.Documents.ITextSelection.UpdateCaretAndHighlight ()
a System.Windows.Documents.TextEditor.OnGotKeyboardFocus (System.Object, System.Windows.Input.KeyboardFocusChangedEventArgs)
a System.Windows.Controls.PasswordBox.OnGotKeyboardFocus (System.Windows.Input.KeyboardFocusChangedEventArgs)
a System.Windows.UIElement.OnGotKeyboardFocusThunk (System.Object, System.Windows.Input.KeyboardFocusChangedEventArgs)
a System.Windows.Input.KeyboardFocusChangedEventArgs.InvokeEventHandler (System.Delegate, System.Object)
a System.Windows.RoutedEventArgs.InvokeHandler (System.Delegate, System.Object)
a System.Windows.RoutedEventHandlerInfo.InvokeHandler (System.Object, System.Windows.RoutedEventArgs)
a System.Windows.EventRoute.InvokeHandlersImpl (System.Object, System.Windows.RoutedEventArgs, booleano)
a System.Windows.UIElement.RaiseEventImpl (System.Windows.DependencyObject, System.Windows.RoutedEventArgs)
a System.Windows.UIElement.RaiseTrustedEvent (System.Windows.RoutedEventArgs)
a System.Windows.UIElement.RaiseEvent (System.Windows.RoutedEventArgs, booleano)
a System.Windows.Input.InputManager.ProcessStagingArea ()
a System.Windows.Input.InputManager.ProcessInput (System.Windows.Input.InputEventArgs)
a System.Windows.Input.KeyboardDevice.ChangeFocus (System.Windows.DependencyObject, Int32)
a System.Windows.Input.KeyboardDevice.TryChangeFocus (System.Windows.DependencyObject, System.Windows.Input.IKeyboardInputProvider, booleano, booleano, booleano)
a System.Windows.Input.KeyboardDevice.Focus (System.Windows.DependencyObject, booleano, booleano, booleano)
a System.Windows.Input.KeyboardDevice.Focus (System.Windows.IInputElement)
a System.Windows.UIElement.Focus ()
a System.Windows.Documents.TextEditorMouse.MoveFocusToUiScope (System.Windows.Documents.TextEditor)
a System.Windows.Documents.TextEditorMouse.OnMouseDown (System.Object, System.Windows.Input.MouseButtonEventArgs)
a System.Windows.Controls.PasswordBox.OnMouseDown (System.Windows.Input.MouseButtonEventArgs)
a System.Windows.UIElement.OnMouseDownThunk (System.Object, System.Windows.Input.MouseButtonEventArgs)
a System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler (System.Delegate, System.Object)
a System.Windows.RoutedEventArgs.InvokeHandler (System.Delegate, System.Object)
a System.Windows.RoutedEventHandlerInfo.InvokeHandler (System.Object, System.Windows.RoutedEventArgs)
a System.Windows.EventRoute.InvokeHandlersImpl (System.Object, System.Windows.RoutedEventArgs, booleano)
a System.Windows.UIElement.RaiseEventImpl (System.Windows.DependencyObject, System.Windows.RoutedEventArgs)
a System.Windows.UIElement.RaiseTrustedEvent (System.Windows.RoutedEventArgs)
a System.Windows.UIElement.RaiseEvent (System.Windows.RoutedEventArgs, booleano)
a System.Windows.Input.InputManager.ProcessStagingArea ()
a System.Windows.Input.InputManager.ProcessInput (System.Windows.Input.InputEventArgs)
a System.Windows.Input.InputProviderSite.ReportInput (System.Windows.Input.InputReport)
a System.Windows.Interop.HwndMouseInputProvider.ReportInput (IntPtr, System.Windows.Input.InputMode, Int32, System.Windows.Input.RawMouseActions, Int32, Int32, Int32)
a System.Windows.Interop.HwndMouseInputProvider.FilterMessage (IntPtr, MS.Internal.Interop.WindowMessage, IntPtr, IntPtr, booleano ByRef)
a System.Windows.Interop.HwndSource.InputFilterMessage (IntPtr, Int32, IntPtr, IntPtr, booleano ByRef)
a MS.Win32.HwndWrapper.WndProc (IntPtr, Int32, IntPtr, IntPtr, booleano ByRef)
a MS.Win32.HwndSubclass.DispatcherCallbackOperation (System.Object)
a System.Windows.Threading.ExceptionWrapper.InternalRealCall (System.Delegate, System.Object, Int32)
a MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen (System.Object, System.Delegate, System.Object, Int32, System.Delegate)
a System.Windows.Threading.Dispatcher.InvokeImpl (System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32)
a MS.Win32.HwndSubclass.SubclassWndProc (IntPtr, Int32, IntPtr, IntPtr)
a MS.Win32.UnsafeNativeMethods.DispatchMessage (System.Windows.Interop.MSG ByRef)
a System.Windows.Threading.Dispatcher.PushFrameImpl (System.Windows.Threading.DispatcherFrame)
a System.Windows.Threading.Dispatcher.PushFrame (System.Windows.Threading.DispatcherFrame)
a System.Windows.Application.RunDispatcher (System.Object)
a System.Windows.Application.RunInternal (System.Windows.Window)
a System.Windows.Application.Run (System.Windows.Window)
a System.Windows.Application.Run ()
a WPF_Control_Test.App.Main ()

È stato utile?

Soluzione

It appears that it the .Net Framework is unable to find a suitable default font. I don't know why this is happening, but the following lines of internal code from FontFamily include the line where the exception is being thrown:

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
}

Apparently, if you do not specify a font it is going to look for an Arial based font, which it is unable to find on that machine, hence your exception.

Altri suggerimenti

I had this same problem on a Windows Embedded 7 box, and as mentioned in the link the comments;

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

I simply copied across the following fonts to the "c:\windows\fonts" directory, and the problem was solved;

  • Arial (TrueType)
  • Courier New (TrueType)
  • Times New Roman (TrueType)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top