Listing loaded modules from PRISM to WPF

I have a WPF application built on top of PRISM.

When a user tries to close the application, I need to check the dirty status of any downloaded views.

I was hoping to list the downloadable modules and ask them, in turn, whether it would be ok to exit and save any changes, but it’s hard for me to find a list of links to the downloaded modules.

The closest I could find was IModuleCatalog, which gives me a list of modules, but not the actual object referencing these modules

Any suggestions on how I can do this?

Thank you in advance

Yang

+3
source share
4 answers

, ? , , .

( Commanding Sample, , , , ... ) " ", " " ( , ). , , , : http://msdn.microsoft.com/en-us/library/dd458890.aspx

+3

.

" ", .

public interface IModuleCleanupService
{
    void RegisterCleanupAction(Action action);
}

public class ModuleCleanupService: IModuleCleanupService
{
    private readonly List<Action> m_cleanupActions = new List<Action>();

    public void RegisterCleanupAction(Action action)
    {
        m_cleanupActions.Add(action);
    }

    public void Cleanup()
    {
        List<Exception> exceptions = null;
        foreach (Action action in m_cleanupActions)
        {
            try
            {
                action();
            }
            catch (Exception ex)
            {
                if (exceptions==null)
                    exceptions = new List<Exception>();
                exceptions.Add(ex);
            }
        }
        if (exceptions != null)
            throw new AggregateException(exceptions);
    }
}

IModuleCleanupService. - ( MEF/Unity ServiceLocalor.Current)

Bootstapper ConfigureContainer ( MefBootstapper devosed, ):

protected override void ConfigureContainer()
{
    base.ConfigureContainer();

    m_moduleCleanupService = new ModuleCleanupService();
    Container.ComposeExportedValue<IModuleCleanupService>(m_moduleCleanupService);
}

GetDisposable bootstapper, IDisposable-object. IDisposable-object ModuleCleanupService:

public IDisposable GetDisposable()
{
    return new DisposableDelegate(() => m_moduleInitializationService.Cleanup());
}

class DisposableDelegate: IDisposable
{
    private readonly Action m_action;

    public DisposableDelegate(Action action)
    {
        m_action = action;
    }

    public void Dispose()
    {
        m_action();
    }
}
+2

Shrike, IEventAggregator.

-, ModulesDisposeRequested:

public class ModulesDisposeRequested : CompositePresentationEvent<object> { }

, , :

// Module constructor
public ModuleA(IEventAggregator eventAggregator) {
   var evnt = eventAggregator.GetEvent<ModulesDisposeRequested>();
   if (evnt != null)
      evnt.Subscribe(OnModulesDisposeRequested);
}

private void OnModulesDisposeRequested(object payload) {
   // cleanup current module
}

- , bootstrapper :

// Overring in App.xaml.cs
protected override void OnExit(ExitEventArgs e) {
   var eventAggregator = _bootstrapper.Container.Resolve<IEventAggregator>();
   var evnt = eventAggregator.GetEvent<ModulesDisposeRequested>();
   if (evnt != null)
      evnt.Publish(null);

   base.OnExit(e);
}
+1

If you want to get an existing instance of all loaded modules, you can do the following:

  • Get an existing instance IServiceLocatorfrom a Unity container.
  • For each instance ModuleInfoin the module directory where state is Initialized, get an instance of the module usingserviceLocator.GetInstance(moduleInfo.ModuleType)
0
source

Source: https://habr.com/ru/post/1724585/


All Articles