ReactiveUI 路由后如何将键盘焦点移动到视图

How can I move the keyboard focus to the view after ReactiveUI routing

你好,我正在为我的 Wpf 应用程序使用 ReactiveUI,但我 运行 遇到了问题。我的主屏幕使用 RouterViewHost 来显示当前活动的视图。

在我的主屏幕路由到新视图后,键盘焦点移动到 Mainform,我想将其移动到当前显示在 RoutedViewHost 中的视图内的第一个可聚焦控件。

实现此目标的最佳方法是什么。我发现以下代码有效,但这是 best/desired 的方法吗?

        RoutedViewHost.TransitionCompleted += (s, e) => RoutedViewHost.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));

谢谢尼克

我不使用内置路由,但是你可以在视图后面的代码中使用 this.WhenActivated 吗?

public partial class FooView : UserControl, IViewFor<FooViewModel>
{
    public FooView()
    {
        InitializeComponent();

        this.WhenActivated(d =>
        {
            SomeTextField.Focus();
        });
    }

    public FooViewModel ViewModel
    {
        get { return (FooViewModel) GetValue(ViewModelProperty); }
        set { SetValue(ViewModelProperty, value); }
    }

    public static readonly DependencyProperty ViewModelProperty =
        DependencyProperty.Register("ViewModel", typeof(FooViewModel), typeof(FooView), new PropertyMetadata(null));

    object IViewFor.ViewModel
    {
        get { return ViewModel; }
        set { ViewModel = (FooViewModel) value; }
    }
}