0

I have composite application with toolbar and I want to make my modules possible to add some buttons to toolbar. As I have understood, a RegionManager should be used to provide this possibility.

I wrote a code like this:

public class MyModule : IModule
{
    private readonly IUnityContainer _container;
    public MyModule(IUnityContainer Container) { _container = Container; }

    public void Initialize()
    {
        var regionManager = _container.Resolve<RegionManager>();
        regionManager.RegisterViewWithRegion("MainToolbar",
                                             () => new Button
                                                   {
                                                       Content = "My Button",
                                                       Command = new DelegateCommand(/*  */)
                                                   });
    }
}

But it seems like creating a buttons from code, especially inside of Module class is not a good idea, according the MVVM pattern. And the second problem is that the button is being created before other modules would be initialized, so I can't refer to services registred by other modules.

What exactly I'm doing wrong? What is a propper way to collect actions from multiple modules into one toolbar?

Paboka
  • 370
  • 3
  • 12
  • Use a model to encapsulate the action taken for that button. Use a DataTemplate to render the button, and place that button model (along with all the other necessary button models) into a collection that is bound against the toolbar, which is an ItemsControl. The toolbar will take each button model in the collection, find the template, and render the button. That's MVVM. *edit* in fact, if all the models share the same property names, you only need to use the ToolBar.ItemTemplate to bind a button to each of the models. –  Jun 10 '16 at 14:13

1 Answers1

0

Your idea is correct, just swap out the button for a view that contains a button. Then make the toolbar a region and inject the "button"-view into the "toolbar"-region.

If your module depends on services that come from other modules, make your module dependent on those modules, so that prism makes sure that the services are initialized first:

[ModuleDependency("ServiceModule")]
public class ModuleA : IModule
{
    ...
}

public class ServiceModule : IModule
{
    ...
}
Haukinger
  • 8,906
  • 2
  • 12
  • 27