WPF和Prism - 将其他程序集中的视图注入TabControl [英] WPF and Prism - Inject view from other assembly into TabControl

查看:268
本文介绍了WPF和Prism - 将其他程序集中的视图注入TabControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我试图找到以下情况的答案,但没有得到答案。

这可能是由于我自己承认,还没有完全掌握整个棱镜概念。

在我的辩护中,我开始在两天前开始研究Prism,所以请温柔;)



阅读大量文献后,我有以下设置:



1.框架项目

2.壳牌项目

3. MainMenu项目(模块)

4. SideMenu项目(模块)

5。 StatusBar项目(模块)

6. TestContentA项目(模块)

7. TestContentB项目(模块)



Framework项目包含事件定义和有效负载等。



Shell项目在shell.xaml中有四个区域:

- MainMenuRegion

- SideBarRegion

- StatusBarRegio n $ / $
- ContentRegion



每个非Shell项目都引用了以下Prism程序集:

- Microsoft .Practices.Prism

- Microsoft.Practices.ServiceLocation

- Microsoft.Practices.Unity



壳牌除了这些程序集之外,project还引用了:

- Microsoft.Practices.Prism.UnityExtensions



每个项目都有一个引用框架项目,但没有人参考解决方案中的任何其他项目。



此时我想指出外部按计划工作!



在MainMenuView项目中,我有几个MenuItems,它们在MainMenuViewModel中有命令。

ViewModel使用EventAggregator发布一些事件在Framework项目中声明。



在Shell项目中,ShellViewModel订阅这些事件,例如:



使用ContentControl作为ContentRegion的控件,我可以毫无问题地在TestContentA和TestContentB之间切换。

(目前,TestContentA和TestContentB也都使用.RegisterViewWithRegion方法注册)



我想做的是将TestContentA和TestContentB作为一个新的TabItem注入TabControl,而不是注入ContentControl控件。

所以shell.xaml现在包含一个TabControl以及一个名为TabRegion的进一步区域。



我见过很多例子:



IRegion tabRegion = regionManager.Regions [TabRegion];

var tabView1 = container.Resolve< firsttabview>();

tabRegion.Add(tabView1,FirstTabView);



我在ShellViewModel中没有Container,但即使我在这里有它,我仍然无法参考TestContentA视图,因为这是在另一个程序集中,它本身将在runti中找到我。

与你可以在网上找到的样本不一样,注入的视图似乎总是在示例项目中。



在Navigate方法中,如果我(用于调试目的)添加代码:



IRegion tabRegion = regionManager.Regions [TabRegion];



然后我可以看到tabRegion有一个View集合,在这个视图中我可以看到来自TestContentA和TestContentB的两个视图,但我不知道如何抓住它们以便我可以将一个新的视图实例注入一个TabItem。



如果有人能指出我正确的方向我会很感激,因为我确信它可以是完成。



感谢您花时间阅读本文!

Hello everybody,

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

After reading numerous literature I have the following setup:

1. Framework Project
2. Shell Project
3. MainMenu project ( Module)
4. SideMenu project ( Module)
5. StatusBar project ( Module)
6. TestContentA project ( Module)
7. TestContentB project ( Module)

The Framework project contains, amongst other things, event definitions and payloads.

The Shell project has, within the shell.xaml, four regions:
- MainMenuRegion
- SideBarRegion
- StatusBarRegion
- ContentRegion

Every non-Shell project has references to the following Prism assemblies:
- Microsoft.Practices.Prism
- Microsoft.Practices.ServiceLocation
- Microsoft.Practices.Unity

The Shell project has in addition to these assemblies, a reference to:
- 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.

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.
The ViewModel, using the EventAggregator, publishes a couple of Events declared in the Framework project.

In the Shell project, the ShellViewModel subscribes to these events, e.g.:

Using a ContentControl as the control for the "ContentRegion", I can switch between TestContentA and TestContentB with no problems.
(For the moment, both TestContentA and TestContentB are also registered using the .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.
So shell.xaml now contains a TabControl along with a futher region called "TabRegion".

I have seen a lot of examples like:

IRegion tabRegion = regionManager.Regions["TabRegion"];
var tabView1 = container.Resolve<firsttabview>();
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.
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:

IRegion 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, but I have no idea how to "grab" them so that I can inject a new instance of the view as a TabItem.

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!

推荐答案

感谢您使用的一种在stackoverflow,我能够让这个工作。



我有订阅MainMenu事件的shell想要注入 TestControlA TestControlB 进入 TabRegion 区域,这是错误的。



我从shell中删除了订阅并且已经而 TestContentA TestContentB 的ViewModel则订阅。这是我将视图添加到该地区的地方。



我希望这可以帮助其他可能一直在思考的人。



在附注中,我没有反对某人给这个问题投票1,但请你如此友善并说明是什么让你以这种方式行事。我无法在其他地方看到这个问题,我相信我提供了足够的信息以便获得帮助,而且我非常有礼貌。



我谢谢你所有,祝你有个美好的一天!
Thanks to a kind used at stackoverflow, I was able to get this working.

I had the shell subscribing to the MainMenu event which wanted to inject either a TestControlA or TestControlB into the TabRegion region, which was wrong.

I removed the subscription from the shell and had the ViewModels for both TestContentA and TestContentB subscribe instead. This is where I added the view to the region.

I hope this helps someone else who may have been thinking along the same lines.

On a side note, I have nothing against somebody giving this question a vote of 1 but please be so kind and state what brought you to act in such a manner. I couldn't see this question being asked elsewhere he and I believe I provided enough information in order to receive assistance and I was most certainly courteous enough.

I thank you all, have a great day!


这篇关于WPF和Prism - 将其他程序集中的视图注入TabControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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