如何将 containerRegistry.RegisterForNavigation 与通用 ViewModel 一起使用?

How to use containerRegistry.RegisterForNavigation with a generic ViewModel?

我有一个包含两个区域的应用程序,一个用作数据类型的选择器(称为 NavigationPane),另一个用作该数据类型的 setter 视图(称为 SimulationPane)。我用来填充 SimulationPane 的 SimulatorView.xaml 有一个相应的 SimulatorViewModel,它动态地为 TDataType 创建一个可设置的属性列表,并最终将它绑定到 SimulatorView.xaml 中的一个 ItemsControl。所以我的 ViewModel 需要 System.Type 作为输入:

我想设置如下:

    public void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.RegisterForNavigation<TopicSelectorView, TopicSelectorViewModel>("Selector");
        containerRegistry.RegisterForNavigation<SimulatorView, SimulatorViewModel<A>>("Simulator_A");
        containerRegistry.RegisterForNavigation<SimulatorView, SimulatorViewModel<B>>("Simulator_B"); // and so on..
    }

注册后,我希望能够调用(从其他模块):

regionManager.RequestNavigate("SimulationPane", $"Simulator_{topicType.Name}");

其中 topicType 是基于用户选择的数据类型(此处:'A'、'B',依此类推)。

这种方法的问题是我最终只有一个 ViewModel,即我最后注册的那个。我的印象是注册使用某种以视图为键的字典。

我在这里做错了什么?我还能如何实现能够在运行时为 VM 提供类型并导航到它的目标?

为什么需要将 AB 作为通用参数传递?

我有一个 SimulatorViewModel 并注入一个服务,视图模型可以从该服务中获取要由用户设置的参数列表。这可以通过反射(在模拟器类型上)或参数描述符列表(IReadOnlyCollection<Parameter> ISimulator.Parameters { get; })来完成,无论你喜欢什么。

当更改模拟器类型时,您在服务中更新它并且 SimulatorViewModel 更新它的参数列表,因为它监听服务的 INotifyPropertyChanged.PropertyChanged.

How else can I achieve my goal of being able to provide a type for VM at runtime, and navigate to it?

Prism 不支持使用相同的视图注册多个视图模型。请参阅 GitHub 上的@brianlagunas 回答:

As I said in my reply to your PR, this is not something we will support in Prism. You have a few options, two of which we have already discussed; create a unique view, or have a single VM and load the relevant data.