简体   繁体   English

Prism-从松散耦合的组件中注入视图

[英]Prism - Inject view from loosely-coupled assembly

I have tried to find an answer for the following scenario but have failed to come up with an answer. 我试图为以下情况找到答案,但未能给出答案。 This may well be due to me, by my own admission, not yet having fully grasped the whole Prism concept. 我自己承认,这很可能是由于我尚未完全掌握整个棱镜概念。 In my defense, I started looking into Prism only two days ago, so please be gentle... 为了辩护,我仅在两天前才开始调查Prism,所以请保持谨慎...

After reading numerous literature I have the following setup: 阅读大量文献后,我将进行以下设置:

  1. Framework Project 框架项目
  2. Shell Project 壳牌项目
  3. MainMenu project ( Module ) MainMenu项目(模块)
  4. SideMenu project ( Module ) SideMenu项目(模块)
  5. StatusBar project ( Module ) StatusBar项目(模块)
  6. TestContentA project ( Module ) TestContentA项目(模块)
  7. TestContentB project ( Module ) TestContentB项目(模块)

The Framework project contains, amongst other things, event definitions and payloads. 框架项目除其他外,还包含事件定义和有效负载。

The Shell project has, within the shell.xaml, four regions: Shell项目在shell.xaml中具有四个区域:

  • MainMenuRegion MainMenuRegion
  • SideBarRegion SideBarRegion
  • StatusBarRegion StatusBarRegion
  • ContentRegion ContentRegion

Every non-Shell project has references to the following Prism assemblies: 每个非Shell项目都引用了以下Prism程序集:

  • Microsoft.Practices.Prism Microsoft.Practices.Prism
  • Microsoft.Practices.ServiceLocation Microsoft.Practices.ServiceLocation
  • Microsoft.Practices.Unity Microsoft.Practices.Unity

The Shell project has in addition to these assemblies, a reference to: Shell项目除了这些程序集之外,还引用了:

  • Microsoft.Practices.Prism.UnityExtensions Microsoft.Practices.Prism.UnityExtensions

Every project has a reference to the Framework project, but none have a reference to any other project within the solution. 每个项目都引用了Framework项目,但是没有一个引用解决方案中的任何其他项目。

At this point I would like to point out that everthing works as planned! 在这一点上,我想指出一切都按计划进行!

In the MainMenuView project I have a few MenuItems which have commands in the MainMenuViewModel. 在MainMenuView项目中,我有一些MenuItems,这些菜单项在MainMenuViewModel中具有命令。

The ViewModel, using the EventAggregator, publishes a couple of Events declared in the Framework project. ViewEvent使用EventAggregator,发布在Framework项目中声明的几个Event。

In the Shell project, the ShellViewModel subscribes to these events, eg: 在Shell项目中,ShellViewModel订阅以下事件,例如:

Using a ContentControl as the control for the "ContentRegion", I can switch between TestContentA and TestContentB with no problems. 使用ContentControl作为“ ContentRegion”的控件,我可以毫无问题地在TestContentA和TestContentB之间切换。 For the moment, both TestContentA and TestContentB are also registered using the 目前,TestContentA和TestContentB都使用

.RegisterViewWithRegion method(...)

What I would like to do is inject TestContentA and TestContentB into a TabControl as a new TabItem, instead of into the ContentControl control. 我想做的是将TestContentA和TestContentB作为新的TabItem注入到TabControl中,而不是注入到ContentControl控件中。

So shell.xaml now contains a TabControl along with a futher region called "TabRegion". 因此,shell.xaml现在包含一个TabControl以及一个名为“ TabRegion”的进一步区域。 I then changed the registered Region from ContentRegion to TabRegion , again, by using the 然后,我再次通过使用将注册的Region从ContentRegion更改为TabRegion

.RegisterViewWithRegion(...)

method. 方法。

I have seen a lot of examples like: 我看过很多例子,例如:

IRegion tabRegion = regionManager.Regions["TabRegion"];
var tabView1 = container.Resolve();
tabRegion.Add(tabView1, "FirstTabView");

I do not have the Container in the ShellViewModel but even if I did have it here, I still cannot refer to the TestContentA view, as this is in another assembly which itself will be found at runtime. 我在ShellViewModel中没有容器,但是即使在这里没有容器,我仍然不能引用TestContentA视图,因为它位于另一个程序集中,而该程序集本身将在运行时找到。

Unlike the samples you can find online, where the view which is to be injected always seems to be in the sample project. 与您可以在网上找到的示例不同,要注入的视图始终似乎在示例项目中。

In the Navigate method, if I (for debugging purposes) add the code: 在Navigate方法中,如果我(出于调试目的)添加代码:

var tabRegion = regionManager.Regions["TabRegion"];

then I can see that tabRegion has a View collection, and in this view I can see both views from TestContentA and TestContentB which tells me that the views were registered correctly. 然后我可以看到tabRegion有一个View集合,并且在此视图中,我可以看到来自TestContentA和TestContentB的两个视图,告诉我这些视图已正确注册。 The problem is that I have no idea how to "grab" them so that I can inject a new instance of the view as a TabItem. 问题是我不知道如何“抓住”它们,以便可以将视图的新实例作为TabItem注入。

If I cheat and add a reference to TestContentA, then I can simply add the view and the TabItem will appear. 如果我作弊并添加对TestContentA的引用,则只需添加视图,TabItem就会出现。 This too tells me that I'm almost there but of course, no reference is wished. 这也告诉我,我快到了,但是当然不希望引用。

To sum up, my question would be: 综上所述,我的问题是:

how can I inject a view from a loosely-coupled assembly which is discovered when the application starts up? 我如何从应用程序启动时发现的松耦合组件中注入视图?

I would appreciate it if someone could point me in the right direction as I'm sure that it can be done. 如果有人可以指出正确的方向,我将不胜感激,因为我相信这是可以做到的。

Thank you for taking the time to read this! 感谢您抽出时间来阅读!

You will need to create a bootstrapper. 您将需要创建一个引导程序。 Each loaded module can get the region manager via ServiceLocator and than register its views to the region names. 每个加载的模块都可以通过ServiceLocator获取区域管理器,然后将其视图注册到区域名称。 this way, the Shell project does not need to contain a direct reference to your views. 这样,Shell项目就不需要直接引用您的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建松散耦合/可扩展的软件架构 - Creating A Loosely-Coupled / Scalable software architecture ASP.NET MVC使用松散耦合的WCF Web服务 - ASP.NET MVC using Loosely-Coupled WCF Web Service 具体类型应该如何以松散耦合的方式返回接口的实例? - How should concrete types return an instance of an interface in a loosely-coupled way? 使用 Prism for UWP App 在松散耦合组件之间进行通信 - Communication between loosely coupled Components using Prism for UWP App 为什么订户未收到使用Prism EventAggregator生成的松耦合事件? - Why is loosely coupled event generated with Prism EventAggregator not received by its subscriber? 我可以使用属性,以便我的工厂知道它可以/应该实例化而不破坏“松散耦合”规则吗? - Can I use Attributes so my Factory knows what it can/should instantiate without breaking the “Loosely-Coupled” rule? 管理通过使用'Designer'属性而产生的松散耦合的程序集引用 - Managing loosely coupled assembly references incurred through usage of the 'Designer' attribute 否决选项的松散耦合的视图内模型通知 - Loosely Coupled Intra-View Model Notifications w/Veto Option 松耦合视图模型理论 - Theory on loosely coupled viewmodel 在TDD中与Stream类型的属性松散耦合的类 - Class that is loosely coupled from its properties of Stream type in TDD
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM