棱镜/MEF:如何在不对区域名称进行硬编码的情况下注册ViewWithRegion [英] Prism / MEF: How to RegisterViewWithRegion Without Hard-Coding the Region Name

查看:142
本文介绍了棱镜/MEF:如何在不对区域名称进行硬编码的情况下注册ViewWithRegion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在构建WPF Prism应用程序.我们有不同的开发人员从事不同的模块项目,并且将多个模块注入到主Application Shell中.主应用程序也是一个单独的项目.我们还希望能够在不同的应用程序中使用这些模块.我们不想在每个应用程序中都使用相同的名称来命名区域.

We are building a WPF Prism application. We have different developers working on different module projects, and multiple modules are injected into the main Application Shell. The main application is also a separate project. We also want to be able to use the modules in different applications. We do not want to have to name the Regions with the same names in every application.

例如,假设我们有一个要在两个不同应用程序中使用的模块.在一个应用程序中,其开发人员可以将模块的区域命名为"DetailsRegion",在另一个应用程序中,其开发人员可以将其命名为"ResultsRegion".

For instance, say we have a module to be used in two different applications. In one application, its developer may name the module's region "DetailsRegion," and in the other, its developer may name it "ResultsRegion."

每个示例中,我都可以通过在模块的类定义中将区域名称​​硬编码为来找到向该区域注册视图:

Every example I can find registers the View with the Region by hard-coding the region name in the module's class definition:

myRegionManager.RegisterViewWithRegion("RegionNameHere", GetType(ModuleViewType))

我想要做的是将Region名称放入主应用程序的app.config文件中,并将该名称传递给模块.像这样:

What I want to do is put the Region name in the main application's app.config file, and pass this name to the module. Something like this:

在主Shell应用程序的app.config中:

In the main Shell Application's app.config:

<Modules>
   <SearchModule>
       <add key="RegionName" value="SearchRegion" />
    </SearchModule>
</Modules>

并在模块的类文件中:

Dim settings As NameValueCollection = CType(ConfigurationManager.GetSection("Modules/SearchModule"), NameValueCollection)
Dim regionName as string = settings("RegionName")
myRegionManager.RegisterViewWithRegion(regionName, GetType(SearchModuleType)

从某种意义上讲,这是将模块与外壳以及彼此完全分离的最后一步.

In a way, this would be the last step to completely decouple the modules from the shell and from each other.

这在模块的 视图 中可以完美地运行.但是我无法在模块的 类定义 文件中执行此操作,因为ConfigurationManager在该级别不可用.

This works perfectly in the views of the module. But I cannot do it in the module's class definition file, as ConfigurationManager is not available at that level.

我可以通过将区域名称放在模块的 app.config的ApplicatonSettings部分中来实现.但这达不到将模块存储在一个位置以供多个应用程序加载的目的.它确实需要在主应用程序的 app.config中.

I can do this by putting the region name in the ApplicatonSettings section of the module's app.config. But this defeats the purpose of being able to store the module in one location to be loaded by multiple applications. It really needs to be in the main application's app.config.

有没有一种方法可以在不使用代码硬编码区域名称的情况下向区域注册模块的视图?我们非常努力地不对任何内容进行硬编码.在这里真的有必要吗?

Is there a way to register a module's View with a Region, without hard-coding the name of the Region in the code? We try so hard NOT to hard-code anything. Is it truly necessary here?

推荐答案

正如Meleak在其评论中已经提到的:使用静态类

As Meleak already mentioned in his comment: Use a static class

namespace Infrastructure
{
    public static class RegionNames
    {
        public const string MainRegion = "MainRegion";
    }
}

在您的xaml代码中,您可以按以下方式使用区域名称:

In your xaml code you can use the region name as follows:

<UserControl 
    xmlns:Inf="clr-namespace:Infrastructure;assembly=Infrastructure"
    xmlns:Regions="clr-namespace:Microsoft.Practices.Prism.Regions;assembly=Microsoft.Practices.Prism">
    <ContentControl Regions:RegionManager.RegionName="{x:Static Inf:RegionNames.MainRegion}"/>
</UserControl>

这篇关于棱镜/MEF:如何在不对区域名称进行硬编码的情况下注册ViewWithRegion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆