本文整理汇总了C#中EventAggregator.Subscribe方法的典型用法代码示例。如果您正苦于以下问题:C# EventAggregator.Subscribe方法的具体用法?C# EventAggregator.Subscribe怎么用?C# EventAggregator.Subscribe使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类EventAggregator
的用法示例。
在下文中一共展示了EventAggregator.Subscribe方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ObserverShouldOnlyReceiveSubscribedNotifications
public void ObserverShouldOnlyReceiveSubscribedNotifications()
{
// Arrange
var eventAggregator = new EventAggregator();
var observer1 = new Mock<IObserve<TestEvent>>();
var observer2 = new Mock<IObserve<OtherEvent>>();
eventAggregator.Subscribe(observer1.Object);
eventAggregator.Subscribe(observer2.Object);
var testEvent = new TestEvent();
var otherEvent = new OtherEvent();
// Act
eventAggregator.Publish(testEvent);
eventAggregator.Publish(otherEvent);
// Assert
observer1.Verify(x => x.OnNext(It.IsAny<IEvent>()), Times.Once);
observer1.Verify(x => x.OnNext(testEvent), Times.Once);
observer2.Verify(x => x.OnNext(otherEvent), Times.Once);
}
示例2: MainViewModel
public MainViewModel(EventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
_eventAggregator.Subscribe<SelectionViewModel>(HandleSelectionViewAdded);
_eventAggregator.Subscribe<ContentViewModel>(HandleContentViewModelAdded);
_selectionViewModels = new ObservableCollection<SelectionViewModel>();
_contentViewModels = new ObservableCollection<ContentViewModel>();
}
示例3: A_valid_message_is_published_to_all_handlers
public void A_valid_message_is_published_to_all_handlers() {
var eventAggregator = new EventAggregator();
var handlerMockA = new Mock<IHandle<object>>();
var handlerMockB = new Mock<IHandle<object>>();
eventAggregator.Subscribe(handlerMockA.Object);
eventAggregator.Subscribe(handlerMockB.Object);
eventAggregator.Publish(new object());
handlerMockA.Verify(handlerStub => handlerStub.Handle(It.IsAny<object>()),
Times.AtLeastOnce());
handlerMockB.Verify(handlerStub => handlerStub.Handle(It.IsAny<object>()),
Times.AtLeastOnce());
}
示例4: TryGetEventHandler_should_return_true_when_one_subscriber_exists
public void TryGetEventHandler_should_return_true_when_one_subscriber_exists()
{
var subject = new EventAggregator();
subject.Subscribe<int>(x => { });
Action<int> handler;
subject.TryGetEventHandler(out handler).Should().BeTrue();
}
示例5: SearchPlayersController
public SearchPlayersController(EventAggregator eventAggregator, SearchPlayersProducerBoundary producerBoundary, SearchPlayersConsumerBoundary consumerBoundary)
{
_eventAggregator = eventAggregator;
_producerBoundary = producerBoundary;
_consumerBoundary = consumerBoundary;
_eventAggregator.Subscribe<SearchPlayersRequest>(HandleSearchPlayersRequest);
}
示例6: Handler_should_invoke_multiple_subscribers
public void Handler_should_invoke_multiple_subscribers()
{
var subject = new EventAggregator();
bool called1 = false;
bool called2 = false;
bool called3 = false;
subject.Subscribe<int>(x => called1 = true);
subject.Subscribe<int>(x => called2 = true);
subject.Subscribe<int>(x => called3 = true);
Action<int> handler;
subject.TryGetEventHandler(out handler);
handler(42);
called1.Should().BeTrue();
called2.Should().BeTrue();
called3.Should().BeTrue();
}
示例7: A_valid_subscriber_gets_removed_from_the_handler_list
public void A_valid_subscriber_gets_removed_from_the_handler_list() {
var eventAggregator = new EventAggregator();
var handlerMock = new Mock<IHandle<object>>();
eventAggregator.Subscribe(handlerMock.Object);
Assert.True(eventAggregator.HandlerExistsFor(typeof (object)));
eventAggregator.Unsubscribe(handlerMock.Object);
Assert.False(eventAggregator.HandlerExistsFor(typeof (object)));
}
示例8: A_valid_subscriber_is_assigned_as_a_handler_its_message_type
public void A_valid_subscriber_is_assigned_as_a_handler_its_message_type() {
var handlerStub = new Mock<IHandle<object>>().Object;
var aggregator = new EventAggregator();
Assert.False(aggregator.HandlerExistsFor(typeof (object)));
aggregator.Subscribe(handlerStub);
Assert.True(aggregator.HandlerExistsFor(typeof (object)));
}
示例9: SubscriberShouldReceiveMessage_WhenMessagePublished
public void SubscriberShouldReceiveMessage_WhenMessagePublished()
{
var mock = new Mock<ISubscriber<Message1>>();
var eventAggregator = new EventAggregator();
eventAggregator.Subscribe(mock.Object);
var msg = new Message1();
eventAggregator.Publish(msg);
mock.Verify(s => s.Handle(msg));
}
示例10: SubscriberShouldNotReceiveMessageWhichItIsNotSubscribedTo
public void SubscriberShouldNotReceiveMessageWhichItIsNotSubscribedTo()
{
var mock = new Mock<ISubscriber<Message1>>();
var eventAggregator = new EventAggregator();
eventAggregator.Subscribe(mock.Object);
var msg = new Message2();
eventAggregator.Publish(msg);
mock.Verify(s => s.Handle(It.IsAny<Message1>()), Times.Never());
}
示例11: Handler_should_invoke_a_single_subscriber
public void Handler_should_invoke_a_single_subscriber()
{
var subject = new EventAggregator();
bool called = false;
subject.Subscribe<int>(x => called = true);
Action<int> handler;
subject.TryGetEventHandler(out handler);
handler(42);
called.Should().BeTrue();
}
示例12: BaseObserverReceivesAllEvents
public void BaseObserverReceivesAllEvents()
{
// Arrange
var eventAggregator = new EventAggregator();
var observer1 = new Mock<IObserve<IEvent>>();
var observer2 = new Mock<IObserver<IEvent>>();
eventAggregator.Subscribe(observer1.Object);
eventAggregator.Subscribe(observer2.Object);
var testEvent = new TestEvent();
var otherEvent = new OtherEvent();
// Act
eventAggregator.Publish(testEvent);
eventAggregator.Publish(otherEvent);
// Assert
observer1.Verify(x => x.OnNext(It.IsAny<IEvent>()), Times.Exactly(2));
observer2.Verify(x => x.OnNext(It.IsAny<IEvent>()), Times.Exactly(2));
}
示例13: ObserverStopsReceivingEventsWhenUnsubscribed
public void ObserverStopsReceivingEventsWhenUnsubscribed()
{
// Arrange
var eventAggregator = new EventAggregator();
var observer1 = new Mock<IObserve<TestEvent>>();
var unsubscribe = eventAggregator.Subscribe(observer1.Object);
var testEvent1 = new TestEvent();
var testEvent2 = new TestEvent();
// Act
eventAggregator.Publish(testEvent1);
unsubscribe.Dispose();
eventAggregator.Publish(testEvent2);
// Assert
observer1.Verify(x => x.OnNext(It.IsAny<IEvent>()), Times.Once);
observer1.Verify(x => x.OnNext(testEvent1), Times.Once);
observer1.Verify(x => x.OnNext(testEvent2), Times.Never);
}
示例14: RegisterEventListenersInEventAggregator
public void RegisterEventListenersInEventAggregator(EventAggregator eventAggregator)
{
eventAggregator.Subscribe<LoginFailed>(LoginFailedSubscriber.Receive);
}
示例15: A_valid_message_invokes_HandlerResultProcessing_when_expected_to
public void A_valid_message_invokes_HandlerResultProcessing_when_expected_to() {
var eventAggregator = new EventAggregator();
var coroutineHandlerMock = new Mock<IHandleWithCoroutine<object>>();
var taskHandlerMock = new Mock<IHandleWithTask<object>>();
var coroutineHandlerProcessed = false;
var taskHandlerProcessed = false;
coroutineHandlerMock.Setup(handlerStub => handlerStub.Handle(It.IsAny<object>()))
.Returns(() => new List<IResult>());
taskHandlerMock.Setup(handlerStub => handlerStub.Handle(It.IsAny<object>()))
.Returns(() => new Task(() => { }));
EventAggregator.HandlerResultProcessing = (target, result) => {
if (target is IHandleWithCoroutine<object>) {
coroutineHandlerProcessed = true;
}
else if (target is IHandleWithTask<object>) {
taskHandlerProcessed = true;
}
};
eventAggregator.Subscribe(coroutineHandlerMock.Object);
eventAggregator.Subscribe(taskHandlerMock.Object);
eventAggregator.Publish(new object());
Assert.True(coroutineHandlerProcessed);
Assert.True(taskHandlerProcessed);
}