C# XAML GettingFocus参数异常

C# XAML GettingFocus参数异常,c#,C#,我们正在尝试使用GettingFocus将焦点从祖先控件重定向到子控件,但在某些情况下会抛出异常 到目前为止,我注意到的情况如下: NewFocusedElement的实际高度或实际宽度为0 NewFocusedElement无法获取焦点 是否仍要保护args.NewFocusedElement=element 我们正在捕获异常作为临时解决方案 代码如下: private void OnGettingFocus(UIElement sender, GettingFocusEventArgs ar

我们正在尝试使用GettingFocus将焦点从祖先控件重定向到子控件,但在某些情况下会抛出异常

到目前为止,我注意到的情况如下:

  • NewFocusedElement的实际高度或实际宽度为0
  • NewFocusedElement无法获取焦点
  • 是否仍要保护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
                    }
                }
            }