XAML GettingFocus ArgumentException

时间:2017-11-08 00:36:17

标签: c#

我们正在尝试使用GettingFocus将焦点从祖先控件重定向到子节点,但我们看到ArgumentExceptions在某些情况下被抛出。

到目前为止我注意到的案例:

  1. NewFocusedElement的ActualHeight或ActualWidth为0
  2. NewFocusedElement无法获得焦点
  3. 无论如何都要保护args.NewFocusedElement = element; ?

    我们将异常作为临时解决方案。

    以下是代码:

    private void OnGettingFocus(UIElement sender, GettingFocusEventArgs args)
            {
                if ((args.Direction == FocusNavigationDirection.Up || args.Direction == FocusNavigationDirection.Down)
                    && (sender == this) && (DefaultFocusButton != null))
                    // Add extra guard here so that the try / catch is no longer needed
                {
                    try
                    {
                        args.NewFocusedElement = DefaultFocusButton;
                    }
                    catch (ArgumentException e)
                    {
                        // This focus redirect causes an exception when the element can't receive focus
                    }
                }
            }
    

0 个答案:

没有答案