C# InvalidEnumArgumentException: значение аргумента 'direction' (3) недопустимо для типа Enum 'FocusNavigationDirection'
У меня есть приложение WPF-C# с ленточным меню и несколькими текстовыми полями на главной панели. когда я фокусирую одну из них и нажимаю стрелку влево, я получаю следующую ошибку. когда я нажимаю на любую другую стрелку, она работает хорошо. я попытался установить точку останова для previewKeyDoyn определенного метода текстового поля, но перед тем, как я смогу в него попасть, выдается исключение.
Все остальные текстовые поля в моем приложении работают хорошо, когда я нажимаю стрелку влево.
System.ComponentModel.InvalidEnumArgumentException: La valeur de l'argument 'direction' (3) n'est pas valide pour le type Enum 'FocusNavigationDirection'.
Nom du paramètre : direction
à System.Windows.Input.KeyboardNavigation.IsInDirection(Rect fromRect, Rect toRect, FocusNavigationDirection direction)
à System.Windows.Input.KeyboardNavigation.FindNextInDirection(DependencyObject sourceElement, Rect sourceRect, DependencyObject container, FocusNavigationDirection direction, Double startRange, Double endRange)
à System.Windows.Input.KeyboardNavigation.MoveNext(DependencyObject sourceElement, DependencyObject container, FocusNavigationDirection direction, Double startRange, Double endRange)
à System.Windows.Input.KeyboardNavigation.GetNextInDirection(DependencyObject sourceElement, FocusNavigationDirection direction)
à System.Windows.Input.KeyboardNavigation.PredictFocusedElement(DependencyObject sourceElement, FocusNavigationDirection direction)
à System.Windows.FrameworkElement.PredictFocus(FocusNavigationDirection direction)
à Microsoft.Windows.Controls.Ribbon.RibbonHelper.PredictFocus(DependencyObject element, FocusNavigationDirection direction) dans e:\dd\WPFOOB\src\wpfoob\Ribbon\RibbonControlsLibrary\Microsoft\Windows\Controls\Ribbon\RibbonHelper.cs:ligne 488
à Microsoft.Windows.Controls.Ribbon.RibbonApplicationMenu.OnPreviewKeyDown(KeyEventArgs e) dans e:\dd\WPFOOB\src\wpfoob\Ribbon\RibbonControlsLibrary\Microsoft\Windows\Controls\Ribbon\RibbonApplicationMenu.cs:ligne 520
à System.Windows.UIElement.OnPreviewKeyDownThunk(Object sender, KeyEventArgs e)
à System.Windows.Input.KeyEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
à System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
à System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
à System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
à System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
à System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
à System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
à System.Windows.Input.InputManager.ProcessStagingArea()
à System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
à System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
à System.Windows.Interop.HwndKeyboardInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawKeyboardActions actions, Int32 scanCode, Boolean isExtendedKey, Boolean isSystemKey, Int32 virtualKey)
à System.Windows.Interop.HwndKeyboardInputProvider.ProcessKeyAction(MSG& msg, Boolean& handled)
à System.Windows.Interop.HwndSource.CriticalTranslateAccelerator(MSG& msg, ModifierKeys modifiers)
à System.Windows.Interop.HwndSource.OnPreprocessMessage(Object param)
à System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
à MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
1 ответ
Я декомпилировал сборку ribbonControlsLibrary.dll с отражателем.NET и нашел эту часть кода. если мы посмотрим внимательнее, то увидим, что используются право, вверх, вниз и последний. это должно быть оставлено вместо последнего. Единственный способ решить эту проблему - использовать.NET Framework 4.5 или переопределить OnPreviewKeyDown для обработки левой клавиши (поэтому исключение не выдается, но символ каретки не перемещается на предыдущий символ).
мой код:
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
if (e.Key == Key.Left)
{
e.Handled = true;
}
}
RibbonControlsLibrary.dll:
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
if (!e.Handled)
{
DependencyObject originalSource;
DependencyObject obj3;
if (e.Key == Key.Down)
{
originalSource = e.OriginalSource as DependencyObject;
if (originalSource != null)
{
UIElement footerPaneHost = this.FooterPaneHost;
if (((footerPaneHost != null) && footerPaneHost.IsKeyboardFocusWithin) && TreeHelper.IsVisualAncestorOf(footerPaneHost, originalSource))
{
obj3 = RibbonHelper.PredictFocus(originalSource, FocusNavigationDirection.Down);
if (((obj3 == null) || (obj3 == originalSource)) && (this.ItemsPaneMoveFocus(FocusNavigationDirection.First) || this.AuxiliaryPaneMoveFocus(FocusNavigationDirection.First)))
{
e.Handled = true;
}
}
}
}
else
{
UIElement auxiliaryPaneHost;
if (e.Key == Key.Up)
{
UIElement element2 = this._popup.TryGetChild();
if ((element2 != null) && !element2.IsKeyboardFocusWithin)
{
if (this.FooterPaneMoveFocus(FocusNavigationDirection.Last) || this.AuxiliaryPaneMoveFocus(FocusNavigationDirection.Last))
{
e.Handled = true;
}
}
else
{
originalSource = e.OriginalSource as DependencyObject;
if (originalSource != null)
{
auxiliaryPaneHost = this.AuxiliaryPaneHost;
if (((auxiliaryPaneHost != null) && auxiliaryPaneHost.IsKeyboardFocusWithin) && TreeHelper.IsVisualAncestorOf(auxiliaryPaneHost, originalSource))
{
obj3 = RibbonHelper.PredictFocus(originalSource, FocusNavigationDirection.Up);
if (((obj3 == null) || (obj3 == originalSource)) && (this.ItemsPaneMoveFocus(FocusNavigationDirection.Last) || this.FooterPaneMoveFocus(FocusNavigationDirection.Last)))
{
e.Handled = true;
}
}
}
}
}
else if ((e.Key == Key.Left) || (e.Key == Key.Right))
{
originalSource = e.OriginalSource as DependencyObject;
if (originalSource != null)
{
if ((e.Key == Key.Left) == (base.FlowDirection == FlowDirection.LeftToRight))
{
auxiliaryPaneHost = this.AuxiliaryPaneHost;
if (((auxiliaryPaneHost != null) && auxiliaryPaneHost.IsKeyboardFocusWithin) && TreeHelper.IsVisualAncestorOf(auxiliaryPaneHost, originalSource))
{
obj3 = RibbonHelper.PredictFocus(originalSource, FocusNavigationDirection.Last);
if (((obj3 != null) && !TreeHelper.IsVisualAncestorOf(auxiliaryPaneHost, obj3)) && RibbonHelper.Focus(obj3))
{
e.Handled = true;
}
}
}
else if (e.Key == Key.Left)
{
ScrollViewer subMenuScrollViewer = base.SubMenuScrollViewer;
if (((subMenuScrollViewer != null) && subMenuScrollViewer.IsKeyboardFocusWithin) && TreeHelper.IsVisualAncestorOf(subMenuScrollViewer, originalSource))
{
RibbonMenuItem item = originalSource as RibbonMenuItem;
if (item == null)
{
item = TreeHelper.FindVisualAncestor<RibbonMenuItem>(originalSource);
}
if ((item != null) && !item.CanOpenSubMenu)
{
obj3 = item.PredictFocus(FocusNavigationDirection.Right);
if ((obj3 != null) && RibbonHelper.Focus(obj3))
{
e.Handled = true;
}
}
}
}
}
}
}
base.OnPreviewKeyDown(e);
}
}
FocusNavigationDirection:
public enum FocusNavigationDirection
{
// Résumé :
// Déplacer le focus sur l'élément pouvant être actif suivant dans l'ordre de
// tabulation.Non pris en charge pour System.Windows.UIElement.PredictFocus(System.Windows.Input.FocusNavigationDirection).
Next = 0,
//
// Résumé :
// Déplacer le focus sur l'élément pouvant être actif précédent dans l'ordre
// de tabulation.Non pris en charge pour System.Windows.UIElement.PredictFocus(System.Windows.Input.FocusNavigationDirection).
Previous = 1,
//
// Résumé :
// Déplacer le focus sur le premier élément pouvant être actif dans l'ordre
// de tabulation.Non pris en charge pour System.Windows.UIElement.PredictFocus(System.Windows.Input.FocusNavigationDirection).
First = 2,
//
// Résumé :
// Déplacer le focus sur le dernier élément pouvant être actif dans l'ordre
// de tabulation.Non pris en charge pour System.Windows.UIElement.PredictFocus(System.Windows.Input.FocusNavigationDirection).
Last = 3,
//
// Résumé :
// Déplacer le focus sur un autre élément pouvant être actif et situé à gauche
// de l'élément ayant actuellement le focus.
Left = 4,
//
// Résumé :
// Déplacer le focus sur un autre élément pouvant être actif et situé à droite
// de l'élément ayant actuellement le focus.
Right = 5,
//
// Résumé :
// Déplacer le focus sur un autre élément pouvant être actif et situé plus haut
// par rapport à l'élément ayant actuellement le focus.
Up = 6,
//
// Résumé :
// Déplacer le focus sur un autre élément pouvant être actif et situé plus bas
// par rapport à l'élément ayant actuellement le focus.
Down = 7,
}