I've stumbled on this particular scenario in a modular Silverlight application.

I need to populate a context menu from one of the application screen, the problem is that multiple modules might want to add menu entries here.

The 2 solution we've come up with is:

  • Use a event aggregator and publish the ContextMenuLoadEvent, modules listen for that event and add menu entries in the context menu
  • Use a IOC container and register IPopulateContextMenu services from modules, then in the screen get all IPopulateContextMenu services and call Populate on them.

What do you think is the best approach and why?

Thanks,

有帮助吗?

解决方案

I think the second approach is nearer to the way it should be done. You don't want the application shell telling the modules of the existence of a context menu. What if you wish to re-imagine the UI and use a different control? You would have to change the message type, and all of the modules that subscribe to that message.

Instead, using MEF for example, you could have an IContextOptions interface or something similar which has a method to retrieve the collection of context options. After MEF has discovered each module that implements this interface, you can call the method on each in turn to populate your context menu.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top