有谁知道使用 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/presentation"
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/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<MenuItem Header="Customers">
</MenuItem>
</UserControl>
但据我所知'我们已经完成了非 Prism MVVM 应用程序结构,并且菜单总是很好地绑定到 ViewModel 中的 ObservableCollections,而上面的内容似乎打破了这种良好的模式。 以上是在 Prism 中执行此操作的惯用方法吗?
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?
发布评论
评论(2)
更新:
我为您创建了一个示例。 它在这里:示例 (现已失效链接)
它有一些你可能还没有想到的东西,比如允许你的模块控制你的 shell 的合约(这样你就可以做像打开窗口之类的事情)。 它的设计考虑了 MVVM。 我不知道你是否使用它,但我会考虑它。
我尝试了几分钟来使选项卡标题正确,但最终我放弃了“A Tab”。 如果您使用选项卡式 UI,则可以将其作为练习。 我将其设计为外观简洁,因此您可以替换 Shell.xaml 中的 XAML,而不会破坏任何内容。 如果使用得当,这就是 RegionManager 的优点之一。
无论如何,祝你好运!
我从未见过这样的例子,但你必须自己实现。
您必须创建自己的界面,如下所示:
然后您的模块将声明对 IMenuRegistry 的依赖项并注册其视图。
您的 IMenuRegistry 实现(您可能会在托管 Bootstrapper 的同一项目中实现和注册)会将这些菜单项添加到您的菜单或树视图或您用于菜单的任何内容中。
当用户单击某个项目时,您必须使用
Bootstrapper.Container.Resolve(viewType)
方法创建视图的实例并将其填充到您想要显示它的任何占位符中。Update:
I created a sample for you. It's here: Sample (now dead link)
It's got a few things you have 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 is designed with MVVM in mind. I don't know 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:
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 will 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.1。为 Leafmenu 创建一个 Menuviewmodel 类,为顶级菜单创建一个 TopLevel MenuViewmodel 类。 Menuviewmodel 类将具有您想要绑定菜单的所有属性。 实现此接口的 Moduleui 必须具有这样的属性
[Export(typeof(IMenu))]
4.实现MefBootstrapper
5.重写配置聚合目录方法
6.在目录中添加包含所有模块dll、IMenu接口dll的目录目录。代码如下
public ObservableCollection ClientMenuViewModels
{ 得到; 私人套装; }
在主窗口或 shell 构造函数中
}
1.Create a Menuviewmodel class for Leafmenu and TopLevel MenuViewmodel class for Toplevel menu. Menuviewmodel class will have all the properties you want to bind your menu with. Moduleui implementing this interafce must have an attribute like this
[Export(typeof(IMenu))]
4.Implement MefBootstrapper
5.Override Configure aggregate catalog method
6.To the catalog add diretory catalog containing all your module dlls, IMenu interface dll.Code is below
public ObservableCollection ClientMenuViewModels
{ get; private set; }
In your mainwindow or shell constructor
}