反应式 UI 路由和 UWP

Reactive UI Routing and UWP

我正在努力为我的 UWP 导航视图进行 ReactiveUI 路由。由于 Navigation View Item 没有实现命令,因此我使用 ItemInvoked 事件并在我的视图模型中执行我的命令。不幸的是,我无法在视图中显示另一个页面。我用的是官方教程,也是Reactive UI UWP Example。使用断点时,我可以看到我的命令已执行但没有任何反应。我不知道如何调试更多。有人用 ReactiveUI Routing 实现了 Navigation View 吗? 我的代码:My repo

@编辑

POCOObservableForProperty: The class InwentarzRzeczowy.UWP.Views.MainView property ViewModel is a POCO type and won't send change notifications, WhenAny will only return a single value!

POCOObservableForProperty: The class InwentarzRzeczowy.UWP.Views.MainView property RoutedViewHost is a POCO type and won't send change notifications, WhenAny will only return a single value!

我相信您需要在事件处理程序中的 AddPage.Execute() 命令之后添加一个 .Subscribe()。不过,我是凭记忆做的,我记得有类似的事情让我绊倒了。

根据我们在 Slack 中的对话,将解决方案也发布在这里。使用 ReactiveUI 路由,您必须在使用路由器之前将视图注册到 Splat.Locator.CurrentMutable(请参阅 View Location) or write a custom view locator that matches your view models and returns the views (see Routing)。后一个选项希望您实现 IViewLocator 接口并将其分配给 RoutedViewHost.ViewLocator 属性。所以,在 C# 代码中我们通常有这样的:

public class YourViewLocator : IViewLocator
{
    public IViewFor ResolveView<T>(T viewModel, string contract = null) => viewModel switch
    {
        NewEntryViewModel _ => new NewEntryView(),
        // Also match other routable view models...
        // The RoutedViewHost will initialize the ViewModel 
        // properties of your views automatically.
        _ => throw new System.NotImplementedException()
    };
}

在 XAML 标记中我们有这个:

<reactiveUi:RoutedViewHost Router="{Binding Router}">
    <reactiveUi:RoutedViewHost.ViewLocator>
        <yourAssembly:YourViewLocator />
    </reactiveUi:RoutedViewHost.ViewLocator>
</reactiveUi:RoutedViewHost>

此外,为根视图初始化 IViewFor.ViewModel 属性 也很重要。这可以在调用 this.InitializeComponent() 后立即在视图的构造函数中完成,也可以在应用程序的组合根中完成。