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

标签 wpf mvvm prism prism-4

我对 Prism 还很陌生,我目前正在使用 Prism 作为概念验证项目重写我们现有的应用程序之一。

该应用程序使用 MVVM 和 ViewModel 优先方法:我们的 ViewModel 由容器解析,并且 IViewResolver服务确定它应该连接到哪个 View (使用名称约定等)。

目前的代码(将 View 添加到选项卡控件)如下所示:

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);

这一切都很好,但是我真的很想使用 Prism 导航框架来为我做所有这些事情,这样我就可以做这样的事情:
_regionManager.RequestNavigate(
    "MainRegion", 
    new Uri("NameOfMyViewModel", UriKind.Relative)
);

并让 Prism 启动 ViewModel + View,设置 DataContext 并将 View 插入该区域。

我通过创建引用 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>

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

有没有办法有效地从 Prism 接管 View 创建,以便当 RequestNavigate被称为我可以查看提供的Uri并基于此启动 View / View 模型? RegionManager.RegisterViewWithRegion 的过载这需要一个允许您自己提供 View 的委托(delegate),我想我正在追求类似的东西。

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

任何帮助表示赞赏!

--
注意:最初发布于 the prism codeplex site

最佳答案

你当然可以这样做。我发现 Prism v4 确实是可扩展的,只要您知道在哪里插入即可。

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

以下是如何在您的 Bootstrap 中进行设置(示例来自我自己的一个项目的 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>()));
}
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
    }
}

关于wpf - 我可以实现自己的 View 解析服务并让 RequestNavigate 使用它吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7453157/

相关文章:

wpf - 通过 setter v ContentTemplate 设置内容样式

c# - 在不暂停 UI 的情况下 sleep 运行

Silverlight 将 DataContext 传递给 ConverterParameter?

c# - 扩展 WPF 第三方控件以启用对现有属性的双向绑定(bind)

c# - 将文件拖放到 .exe 文件顶部以获取文件信息

wpf - Powershell中的WPF网格高度降低

c# - 如何动态绑定(bind)两个 ViewModel 的公共(public)属性? (MVVM,ViewModel-First)

vb.net - Silverlight中Prism MVVM的VB.net示例

wpf - 如何在 Prism 框架中加载实际 shell 之前显示登录屏幕

wpf - 在ViewModel中有 Prism 的RegionManager引用是一种好习惯吗