我可以实现自己的视图解析服务并让RequestNavigate使用它吗?

Pj.*_*Pj. 5 wpf prism mvvm prism-4

我对Prism相当新,我目前正在使用Prism作为概念证明项目重写我们现有的一个应用程序.

该应用程序使用MVVM和ViewModel第一种方法:我们的ViewModel由容器解析,IViewResolver服务计算出它应该连接到哪个视图(使用名称约定等).

此时代码(向选项卡控件添加视图)看起来像这样:

var vm = (get ViewModel from somewhere)
IRegion reg = _regionManager.Regions["MainRegion"];
var vw = _viewResolver.FromViewModel(vm); // Spins up a view and sets its DataContext
reg.Add(vw);
reg.Activate(vw);
Run Code Online (Sandbox Code Playgroud)

这一切都很好,但我真的很喜欢使用Prism导航框架为我做所有这些事情,这样我就可以这样做:

_regionManager.RequestNavigate(
    "MainRegion", 
    new Uri("NameOfMyViewModel", UriKind.Relative)
);
Run Code Online (Sandbox Code Playgroud)

并让Prism旋转ViewModel + View,设置DataContext并将视图插入该区域.

通过创建引用ViewModel类型的DataTemplates,我获得了一些成功,例如:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Module01">
<DataTemplate DataType="{x:Type local:TestViewModel}">
<local:TestView />
</DataTemplate>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

...并且在模块初始化时让模块将相关的资源字典添加到应用程序资源中,但这看起来有点垃圾.

有没有办法从Prism有效地接管视图创建,以便在RequestNavigate调用时我可以查看提供的内容Uri并根据它调整视图/视图模型?有一个重载RegionManager.RegisterViewWithRegion,需要一个代表,允许你自己提供一个视图,我想我是在经历这样的事情.

我想我可能需要提供自己的IRegionBehaviorFactory,但我不确定所涉及的是什么(或者即使我走在正确的道路上!).

任何帮助赞赏!

- 注意:最初发布在prism codeplex网站上

Jon*_*Jon 8

当然你可以做到这一点.我发现Prism v4真的是可扩展的,只要你知道插在哪里.

在这种情况下,您需要自己的自定义实现IRegionNavigationContentLoader.

以下是如何在引导程序中进行设置(示例来自UnityBootstrapper我自己的一个项目的子类):

protected override void ConfigureContainer()
{
    // IMPORTANT: Due to the inner workings of UnityBootstrapper, accessing
    // ServiceLocator.Current here will throw an exception!
    // If you want access to IServiceLocator, resolve it from the container directly.
    base.ConfigureContainer();

    // Set up our own content loader, passing it a reference to the service locator
    // (it will need this to resolve ViewModels from the container automatically)
    this.Container.RegisterInstance<IRegionNavigationContentLoader>(
       new ViewModelContentLoader(this.Container.Resolve<IServiceLocator>()));
}
Run Code Online (Sandbox Code Playgroud)

ViewModelContentLoader本身源于RegionNavigationContentLoader重用代码,看起来像这样:

public class ViewModelContentLoader : RegionNavigationContentLoader
{
    private readonly IServiceLocator serviceLocator;

    public ViewModelContentLoader(IServiceLocator serviceLocator)
        : base(serviceLocator)
    {
        this.serviceLocator = serviceLocator;
    }

    // THIS IS CALLED WHEN A NEW VIEW NEEDS TO BE CREATED
    // TO SATISFY A NAVIGATION REQUEST
    protected override object CreateNewRegionItem(string candidateTargetContract)
    {
        // candidateTargetContract is e.g. "NameOfMyViewModel"

        // Just a suggestion, plug in your own resolution code as you see fit
        var viewModelType = this.GetTypeFromName(candidateTargetContract);
        var viewModel = this.serviceLocator.GetInstance(viewModelType);

        // get ref to viewResolver somehow -- perhaps from the container?
        var view = _viewResolver.FromViewModel(vm);

        return view;
    }

    // THIS IS CALLED TO DETERMINE IF THERE IS ANY EXISTING VIEW
    // THAT CAN SATISFY A NAVIGATION REQUEST
    protected override IEnumerable<object> 
    GetCandidatesFromRegion(IRegion region, string candidateNavigationContract)
    {
        if (region == null) {
            throw new ArgumentNullException("region");
        }

        // Just a suggestion, plug in your own resolution code as you see fit
        var viewModelType = this.GetTypeFromName(candidateNavigationContract);

        return region.Views.Where(v =>
            ViewHasDataContract((FrameworkElement)v, viewModelType) ||
            string.Equals(v.GetType().Name, candidateNavigationContract, StringComparison.Ordinal) ||
            string.Equals(v.GetType().FullName, candidateNavigationContract, StringComparison.Ordinal));
    }

    // USED IN MY IMPLEMENTATION OF GetCandidatesFromRegion
    private static bool 
    ViewHasDataContract(FrameworkElement view, Type viewModelType)
    {
        var dataContextType = view.DataContext.GetType();

        return viewModelType.IsInterface
           ? dataContextType.Implements(viewModelType)
           : dataContextType == viewModelType 
                   || dataContextType.GetAncestors().Any(t => t == viewModelType);
    }

    // USED TO MAP STRINGS OF VIEWMODEL TYPE NAMES TO ACTUAL TYPES
    private Type GetTypeFromName(string typeName)
    {
        // here you need to map the string type to a Type object, e.g.
        // "NameOfMyViewModel" => typeof(NameOfMyViewModel)

        return typeof(NameOfMyViewModel); // hardcoded for simplicity
    }
}
Run Code Online (Sandbox Code Playgroud)