寻找将模块自身加载到菜单中的棱镜示例 [英] Looking for Prism example of Modules loading themselves into a menu

查看:72
本文介绍了寻找将模块自身加载到菜单中的棱镜示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人知道使用Prism的WPF代码示例,其中每个模块将自己注册为另一个模块中菜单的菜单项?



(我目前有一个尝试使用EventAggregator进行此操作的应用程序,因此一个模块侦听来自其他模块的已发布事件,这些模块需要将其标题作为菜单项显示在菜单中,但是我在订购时遇到了问题我想找到一个使用经典Prism结构来执行此操作的示例。)



我正在考虑以下方面:



Shell.xaml:

 < DockPanel> 
< TextBlock Text = Menu: DockPanel.Dock = Top />
< Menu
Name = MenuRegion
cal:RegionManager.RegionName = MenuRegion
DockPanel.Dock = Top />
< / DockPanel>

合同视图:

 < UserControl x:Class = ContractModule.Views.AllContracts 
xmlns = http://schemas.microsoft.com/winfx/2006/xaml/演示文稿
xmlns:x = http://schemas.microsoft.com/winfx/2006/xaml>
< MenuItem Header = Contracts>
< / MenuItem>
< / UserControl>

客户视图:

 < UserControl x:Class = CustomerModule.Views.CustomerView 
xmlns = http://schemas.microsoft.com/winfx/2006/xaml/演示文稿
xmlns:x = http://schemas.microsoft.com/winfx/2006/xaml>
< MenuItem Header = Customers>
< / MenuItem>
< / UserControl>

但是据我所知,我已经完成了非棱镜MVVM应用程序结构,并且菜单始终与之绑定ViewModel及以上版本中的ObservableCollections似乎打破了这种不错的模式。以上是在Prism中完成的常规方法吗?

解决方案

更新:



我为您创建了一个示例。它在这里:示例



还有一些您可能尚未想到的事情,例如一个合同,该合同将允许您的模块控制您的外壳程序(这样您就可以执行诸如Open Window之类的工作)。它在设计时就考虑了MVVM ...如果您正在使用它,我不知道,但我会考虑的。



我尝试了几分钟,以使标签标题正确,但最后我还是选择了 A Tab。如果使用选项卡式UI,则留给您作为练习。我将其设计为无外观设计,因此您可以在Shell.xaml中替换XAML,而不会破坏任何内容。如果正确使用,这就是RegionManager的优点之一。



无论如何,祝您好运!






我从未见过这样的示例,但是您必须自己实现。



创建自己的界面,如下所示:

 公共接口IMenuRegistry 
{
void RegisterViewWithMenu(string MenuItemTitle,System.Type viewType);
}

然后,您的模块将声明对IMenuRegistry的依赖并注册其视图。 / p>

您的IMenuRegistry实现(您可能会在托管Bootstrapper的同一项目中实现并注册),您可以将这些菜单项添加到菜单或treeview或任何其他内容中



当用户单击某个项目时,您将必须使用您的 Bootstrapper.Container.Resolve(viewType)方法来创建视图的实例,并将其塞入要在其中显示的任何占位符。


Does anyone know of WPF code examples using Prism in which modules each register themselves as a menuitem in a menu within another module?

(I've currently got an application which tries to do this with the EventAggregator, so one module listens for published events from other modules which need to have their title in the menu as a menu item, but I'm getting problems with the order of loading and threading etc. I want to find an example that uses classic Prism structure to do this.)

I'm thinking in terms of this:

Shell.xaml:

<DockPanel>
    <TextBlock Text="Menu:" DockPanel.Dock="Top"/>
    <Menu 
        Name="MenuRegion" 
        cal:RegionManager.RegionName="MenuRegion" 
        DockPanel.Dock="Top"/>
</DockPanel>

Contracts View:

<UserControl x:Class="ContractModule.Views.AllContracts"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <MenuItem Header="Contracts">
    </MenuItem>
</UserControl>

Customers View:

<UserControl x:Class="CustomerModule.Views.CustomerView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <MenuItem Header="Customers">
    </MenuItem>
</UserControl>

But up to know I've done non-Prism MVVM application structure and Menus were always nicely bound to ObservableCollections in the ViewModel and the above seems to break this nice pattern. Is the above the customary way to do it in Prism?

解决方案

Update:

I created a sample for you. It's here: Sample

It's got a few things you've probably not thought of yet, like a contract that will allow your modules to control your shell (so you can do stuff like Open Window, that kind of thing). It's designed with MVVM in mind... I dunno if you are using that, but I would consider it.

I tried for a few minutes to get the tab titles correct, but I ended up leaving off with "A Tab". It's left as an exercise for you if you go with a tabbed UI. I've designed it to be lookless, so you can replace the XAML in the Shell.xaml without breaking anything. That's one of the advantages to the RegionManager stuff if you use it right.

Anyway, good luck!


I've never seen an example of this, but you'd have to implement this yourself.

You'd have to create your own interface, something like this:

public interface IMenuRegistry
{
     void RegisterViewWithMenu(string MenuItemTitle, System.Type viewType);
}

Your Modules then would declare a dependency on an IMenuRegistry and register their views.

Your implementation of IMenuRegistry (which you would likely implement and register in the same project that hosts your Bootstrapper) you would add those menu items to your menu or treeview or whatever you are using for your menu.

When a user clicks on an item you'll have to use your Bootstrapper.Container.Resolve(viewType) method to create an instance of the view and stuff it in whatever placeholder you want to show it in.

这篇关于寻找将模块自身加载到菜单中的棱镜示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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