ViewModel 未解析

ViewModel not resolved

我有一个使用 ReactiveUI 的 UWP 应用程序。我使用以下代码导航到页面:

Router.Navigate.Execute(new AccountListViewModel(this));

导航完成。但是我为导航创建的 ViewModel 并没有分配给我在 View 中的 ViewModel。 IViewFor<>实现如下:

public sealed partial class AccountListView : IViewFor<AccountListViewModel>
{
    public static readonly DependencyProperty ViewModelProperty = DependencyProperty
        .Register(nameof(ViewModel), typeof(AccountListViewModel), typeof(AccountListView), null);

    public AccountListView()
    {
        this.InitializeComponent();

        this.WhenActivated(disposables =>
        {
           // My Bindings
           ...
        });
    }

    object IViewFor.ViewModel
    {
        get => ViewModel;
        set => ViewModel = (AccountListViewModel) value;
    }

    public AccountListViewModel ViewModel {
        get => (AccountListViewModel)GetValue(ViewModelProperty);
        set => SetValue(ViewModelProperty, value);
    }

或者我在这里完全错了?

根据用于通用 Windows 平台和 Windows Presentation Foundation 的 ReactiveUI RoutedViewHost implementation for Windows,视图模型肯定应该分配给 IViewFor.ViewModel 属性。您可以跟踪 IScreen.Router.CurrentViewModel 属性 中的更改以确保它发生更改。

如果是,请确保将 IScreen.Router 属性 正确绑定到 UWP 特定 RoutedViewHost [=25] 的 Router 属性 =] 控制,路由 应该 终于可以工作了。事实上,我最近在 UWP 上测试了这种行为,它在 ReactiveUI 9.13.1 和最新的 UWP SDK 上对我来说运行良好。尝试关注 routing tutorial to fully understand how routing works. If this still won't work for you, then uploading a minimal repro that compiles to GitHub could help us understand your issue better. Also, come join ReactiveUI Slack,我们随时准备提供帮助。