Prism 2.1 Post / Subscribe with weak link?

I am creating a demo version of Prism 2.1 to speed up with technology. I am having a problem with CompositePresentationEvents published and signed through an event aggregation service. Subscribing to a subscription works fine if I set a strong link (KeepSubscriberReferenceAlive = true), but it fails if I set a weak link (KeepSubscriberReferenceAlive omitted).

I would like to subscribe with a weak link, so I do not need to manage the unsubscribing from the event. Is there any way to do this? Why is a strong link required here? Thank you for your help!

Here are the details: My demo application is single-threaded and has two regions: Navigator and Workspace, as well as three modules NavigatorModule, WorkspaceAModule and WorkspaceBModule. NavigatorModule has two buttons: "Show workspace A" and "Show workspace B". When one of these buttons is pressed, ICommand is called, which publishes a CompositePresentationEvent named ViewRequested. The event contains a payload string that indicates which workspace module should be displayed.

Here is the announcement of the event from the infrastructure infrastructure project:

using Microsoft.Practices.Composite.Presentation.Events;

namespace Prism2Demo.Common.Events
{
    public class ViewRequestedEvent : CompositePresentationEvent<string>
    {
    }
}

Here is the code for publishing events from the Navigator module:

// Publish ViewRequestedEvent
var eventAggregator = viewModel.Container.Resolve<IEventAggregator>();
var viewRequestedEvent = eventAggregator.GetEvent<ViewRequestedEvent>();
viewRequestedEvent.Publish(workspaceName);

Here is the event subscription code that each Workspace module includes in its Initialize () method:

// Subscribe to ViewRequestedEvent
var eventAggregator = m_Container.Resolve<IEventAggregator>();
var viewRequestedEvent = eventAggregator.GetEvent<ViewRequestedEvent>();
viewRequestedEvent.Subscribe(this.ViewRequestedEventHandler, ThreadOption.PublisherThread, true);

The Subscribe () operator is displayed with a strong link.

Thanks again for your help.

+3
1

, :

, EventAggregator , :

container.RegisterType<IEventAggregator, EventAggregator>(new ContainerControlledLifetimeManager());

, - (this ).

+1

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


All Articles