This commit is contained in:
Laurent Bugnion 2009-10-25 16:17:21 +00:00
Родитель dda20bcf4b
Коммит dee991da38
35 изменённых файлов: 1781 добавлений и 557 удалений

Просмотреть файл

@ -86,9 +86,6 @@
<Compile Include="..\GalaSoft.MvvmLight\Messaging\GenericMessage.cs">
<Link>Messaging\GenericMessage.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight\Messaging\IMessageRecipient.cs">
<Link>Messaging\IMessageRecipient.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight\Messaging\IMessenger.cs">
<Link>Messaging\IMessenger.cs</Link>
</Compile>
@ -98,8 +95,20 @@
<Compile Include="..\GalaSoft.MvvmLight\Messaging\Messenger.cs">
<Link>Messaging\Messenger.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight\Messaging\OldMessenger.cs">
<Link>Messaging\OldMessenger.cs</Link>
<Compile Include="..\GalaSoft.MvvmLight\Messaging\NotificationMessage.cs">
<Link>Messaging\NotificationMessage.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight\Messaging\NotificationMessageAction.cs">
<Link>Messaging\NotificationMessageAction.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight\Messaging\NotificationMessageFunc.cs">
<Link>Messaging\NotificationMessageFunc.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight\Messaging\NotificationMessageGeneric.cs">
<Link>Messaging\NotificationMessageGeneric.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight\Messaging\NotificationMessageWithCallback.cs">
<Link>Messaging\NotificationMessageWithCallback.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight\Messaging\PropertyChangedMessage.cs">
<Link>Messaging\PropertyChangedMessage.cs</Link>

Просмотреть файл

@ -11,7 +11,7 @@
// <license>
// See license.txt in this project or http://www.galasoft.ch/license_MIT.txt
// </license>
// <LastBaseLevel>BL0008</LastBaseLevel>
// <LastBaseLevel>BL0009</LastBaseLevel>
// ****************************************************************************
using System;
@ -50,8 +50,8 @@ using System.Runtime.InteropServices;
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("2.0.0.0")]
////[assembly: AssemblyFileVersion("2.0.0.0")]
////[assembly: AssemblyVersion("2.0.0.*")]
[assembly: AssemblyFileVersion("3.0.0.0/alpha1")]
// FxCop
[module: SuppressMessage(

Просмотреть файл

@ -100,47 +100,38 @@
<Compile Include="..\GalaSoft.MvvmLight.Test\Messaging\DialogMessageTest.cs">
<Link>Messaging\DialogMessageTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test\Messaging\MessengerBroadcastToAllTest.cs">
<Link>Messaging\MessengerBroadcastToAllTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test\Messaging\MessengerBroadcastToTypeTest.cs">
<Link>Messaging\MessengerBroadcastToTypeTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test\Messaging\MessengerCreationDeletionTest.cs">
<Link>Messaging\MessengerCreationDeletionTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test\Messaging\MessengerMultipleInstancesTest.cs">
<Link>Messaging\MessengerMultipleInstancesTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test\Messaging\MessengerOverrideDefaultTest.cs">
<Link>Messaging\MessengerOverrideDefaultTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test\Messaging\MessengerRegisterUnregisterTest.cs">
<Link>Messaging\MessengerRegisterUnregisterTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test\Messaging\MessengerRegisterWhileSendingTest.cs">
<Link>Messaging\MessengerRegisterWhileSendingTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test\Messaging\OldExceptionMessage.cs">
<Link>Messaging\OldExceptionMessage.cs</Link>
<Compile Include="..\GalaSoft.MvvmLight.Test\Messaging\MessengerSendToAllTest.cs">
<Link>Messaging\MessengerSendToAllTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test\Messaging\OldInvalidOperationExceptionMessage.cs">
<Link>Messaging\OldInvalidOperationExceptionMessage.cs</Link>
<Compile Include="..\GalaSoft.MvvmLight.Test\Messaging\MessengerSendToTypeTest.cs">
<Link>Messaging\MessengerSendToTypeTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test\Messaging\OldMessengerTest.cs">
<Link>Messaging\OldMessengerTest.cs</Link>
<Compile Include="..\GalaSoft.MvvmLight.Test\Messaging\MessengerSendWithTokenTest.cs">
<Link>Messaging\MessengerSendWithTokenTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test\Messaging\OldTestCommandHost.cs">
<Link>Messaging\OldTestCommandHost.cs</Link>
<Compile Include="..\GalaSoft.MvvmLight.Test\Messaging\NotificationMessageActionTest.cs">
<Link>Messaging\NotificationMessageActionTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test\Messaging\OldTestMessage.cs">
<Link>Messaging\OldTestMessage.cs</Link>
<Compile Include="..\GalaSoft.MvvmLight.Test\Messaging\NotificationMessageFuncTest.cs">
<Link>Messaging\NotificationMessageFuncTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test\Messaging\OldTestRecipient.cs">
<Link>Messaging\OldTestRecipient.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test\Messaging\OldTestRecipient2.cs">
<Link>Messaging\OldTestRecipient2.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test\Messaging\OldTestViewModel.cs">
<Link>Messaging\OldTestViewModel.cs</Link>
<Compile Include="..\GalaSoft.MvvmLight.Test\Messaging\NotificationMessageTest.cs">
<Link>Messaging\NotificationMessageTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test\Messaging\PropertyChangedMessageTest.cs">
<Link>Messaging\PropertyChangedMessageTest.cs</Link>

Просмотреть файл

@ -21,7 +21,7 @@ namespace GalaSoft.MvvmLight.Test.Command
command.CanExecuteChanged += canExecuteChangedEventHandler;
command.RaiseCanExecuteChanged(true);
command.RaiseCanExecuteChanged();
#if SILVERLIGHT
Assert.AreEqual(1, canExecuteChangedCalled);
@ -32,7 +32,7 @@ namespace GalaSoft.MvvmLight.Test.Command
command.CanExecuteChanged -= canExecuteChangedEventHandler;
command.RaiseCanExecuteChanged(true);
command.RaiseCanExecuteChanged();
#if SILVERLIGHT
Assert.AreEqual(1, canExecuteChangedCalled);

Просмотреть файл

@ -21,7 +21,7 @@ namespace GalaSoft.MvvmLight.Test.Command
command.CanExecuteChanged += canExecuteChangedEventHandler;
command.RaiseCanExecuteChanged(true);
command.RaiseCanExecuteChanged();
#if SILVERLIGHT
Assert.AreEqual(1, canExecuteChangedCalled);
@ -31,7 +31,7 @@ namespace GalaSoft.MvvmLight.Test.Command
#endif
command.CanExecuteChanged -= canExecuteChangedEventHandler;
command.RaiseCanExecuteChanged(true);
command.RaiseCanExecuteChanged();
#if SILVERLIGHT
Assert.AreEqual(1, canExecuteChangedCalled);

Просмотреть файл

@ -57,22 +57,19 @@
<Compile Include="Command\RelayCommandGenericTest.cs" />
<Compile Include="Command\RelayCommandTest.cs" />
<Compile Include="Messaging\CommandMessageTest.cs" />
<Compile Include="Messaging\MessengerOverrideDefaultTest.cs" />
<Compile Include="Messaging\MessengerRegisterWhileSendingTest.cs" />
<Compile Include="Messaging\MessengerSendToAllTest.cs" />
<Compile Include="Messaging\MessengerSendToTypeTest.cs" />
<Compile Include="Messaging\MessengerSendWithTokenTest.cs" />
<Compile Include="Messaging\NotificationMessageActionTest.cs" />
<Compile Include="Messaging\NotificationMessageFuncTest.cs" />
<Compile Include="Messaging\NotificationMessageTest.cs" />
<Compile Include="Messaging\PropertyChangedMessageTest.cs" />
<Compile Include="Messaging\DialogMessageTest.cs" />
<Compile Include="Messaging\MessengerBroadcastToAllTest.cs" />
<Compile Include="Messaging\MessengerBroadcastToTypeTest.cs" />
<Compile Include="Messaging\MessengerCreationDeletionTest.cs" />
<Compile Include="Messaging\MessengerMultipleInstancesTest.cs" />
<Compile Include="Messaging\MessengerRegisterUnregisterTest.cs" />
<Compile Include="Messaging\OldExceptionMessage.cs" />
<Compile Include="Messaging\OldInvalidOperationExceptionMessage.cs" />
<Compile Include="Messaging\OldMessengerTest.cs" />
<Compile Include="Messaging\OldTestCommandHost.cs" />
<Compile Include="Messaging\OldTestMessage.cs" />
<Compile Include="Messaging\OldTestRecipient.cs" />
<Compile Include="Messaging\OldTestRecipient2.cs" />
<Compile Include="Messaging\OldTestViewModel.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ViewModelBaseTest.cs" />
</ItemGroup>

Просмотреть файл

@ -24,24 +24,25 @@ namespace GalaSoft.MvvmLight.Test.Messaging
Messenger.Reset();
Messenger.Default.Register<CommandMessage<Exception>>(this,
m =>
{
receivedSender = m.Sender;
receivedTarget = m.Target;
Messenger.Default.Register<CommandMessage<Exception>>(
this,
m =>
{
receivedSender = m.Sender;
receivedTarget = m.Target;
if (m.Command == DummyCommand1)
{
receivedContent1 = testContent1;
return;
}
if (m.Command == DummyCommand1)
{
receivedContent1 = testContent1;
return;
}
if (m.Command == DummyCommand2)
{
receivedContent2 = testContent2;
return;
}
});
if (m.Command == DummyCommand2)
{
receivedContent2 = testContent2;
return;
}
});
Assert.AreEqual(null, receivedContent1);
Assert.AreEqual(null, receivedContent2);
@ -135,24 +136,25 @@ namespace GalaSoft.MvvmLight.Test.Messaging
Messenger.Reset();
Messenger.Default.Register<CommandMessage>(this,
m =>
{
receivedSender = m.Sender;
receivedTarget = m.Target;
Messenger.Default.Register<CommandMessage>(
this,
m =>
{
receivedSender = m.Sender;
receivedTarget = m.Target;
if (m.Command == DummyCommand1)
{
receivedContent1 = TestContent1;
return;
}
if (m.Command == DummyCommand1)
{
receivedContent1 = TestContent1;
return;
}
if (m.Command == DummyCommand2)
{
receivedContent2 = TestContent2;
return;
}
});
if (m.Command == DummyCommand2)
{
receivedContent2 = TestContent2;
return;
}
});
Assert.AreEqual(null, receivedContent1);
Assert.AreEqual(null, receivedContent2);

Просмотреть файл

@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using GalaSoft.MvvmLight.Messaging;
namespace GalaSoft.MvvmLight.Test.Messaging
{
[TestClass]
public class MessengerOverrideDefaultTest
{
private string ReceivedContent
{
get;
set;
}
[TestMethod]
public void TestOverrideDefault()
{
const string TestContent1 = "Test content 1";
const string TestContent2 = "Test content 2";
const string TestContent3 = "Test content 3";
Messenger.Reset();
Messenger.Default.Register<string>(this, m => ReceivedContent = m);
Assert.IsNull(ReceivedContent);
Messenger.Default.Send(TestContent1);
Assert.AreEqual(TestContent1, ReceivedContent);
Messenger.Reset();
Messenger.OverrideDefault(new TestMessenger());
Messenger.Default.Send(TestContent2);
Assert.AreEqual(1, ((TestMessenger)Messenger.Default).MessagesTransmitted);
Assert.AreEqual(TestContent1, ReceivedContent);
Messenger.Default.Register<string>(this, m => ReceivedContent = m);
Assert.AreEqual(1, ((TestMessenger)Messenger.Default).MessagesTransmitted);
Messenger.Default.Send(TestContent3);
Assert.AreEqual(TestContent3, ReceivedContent);
Assert.AreEqual(2, ((TestMessenger)Messenger.Default).MessagesTransmitted);
}
// Helpers
private class TestMessenger : Messenger
{
private readonly List<Delegate> _recipients = new List<Delegate>();
public int MessagesTransmitted
{
get;
private set;
}
public override void Send<TMessage>(TMessage message)
{
MessagesTransmitted++;
base.Send(message);
}
}
}
}

Просмотреть файл

@ -14,7 +14,7 @@ namespace GalaSoft.MvvmLight.Test.Messaging
private const string TestContentStringNested = "Hello earth";
[TestMethod]
public void TestNewMessengerRegisteringWhileSending()
public void TestMessengerRegisteringWhileSending()
{
Messenger.Reset();
TestRecipient.Reset();
@ -38,7 +38,7 @@ namespace GalaSoft.MvvmLight.Test.Messaging
}
[TestMethod]
public void TestNewMessengerRegisteringForMessageBaseWhileSending()
public void TestMessengerRegisteringForMessageBaseWhileSending()
{
Messenger.Reset();
TestRecipient.Reset();
@ -62,51 +62,70 @@ namespace GalaSoft.MvvmLight.Test.Messaging
}
[TestMethod]
public void TestOldMessengerRegisteringWhileSending()
public void TestMessengerRegisteringInlineWhileReceiving()
{
Messenger.Reset();
TestRecipient.Reset();
var receivedContent = string.Empty;
Messenger.Default.Register<string>(
this,
m => Messenger.Default.Register<PropertyChangedMessage<string>>(this, m2 => receivedContent = m2.NewValue));
var list = new List<TestRecipient1>();
for (var index = 0; index < 10; index++)
{
list.Add(new TestRecipient3());
}
Messenger.Default.Broadcast(new GenericMessage<string>(TestContentString));
Assert.AreEqual(null, TestRecipient.LastReceivedString);
Assert.AreEqual(0, TestRecipient.ReceivedStringMessages);
Messenger.Default.Broadcast(new GenericMessage<string>(TestContentStringNested));
Assert.AreEqual(TestContentStringNested, TestRecipient.LastReceivedString);
Assert.AreEqual(20, TestRecipient.ReceivedStringMessages);
Messenger.Default.Send("Hello world");
}
[TestMethod]
public void TestOldMessengerRegisteringForMessageBaseWhileSending()
public void TestMessengerRegisteringMessageBaseInlineWhileReceiving()
{
Messenger.Reset();
TestRecipient.Reset();
var receivedContent = string.Empty;
Messenger.Default.Register<string>(
this,
m => Messenger.Default.Register<PropertyChangedMessage<string>>(this, true, m2 => receivedContent = m2.NewValue));
var list = new List<TestRecipient2>();
Messenger.Default.Send("Hello world");
}
for (var index = 0; index < 10; index++)
{
list.Add(new TestRecipient4());
}
[TestMethod]
public void TestMessengerRegisteringInlineWhileReceivingMessageBase()
{
var receivedContent = string.Empty;
Messenger.Default.Register<string>(
this,
true,
m => Messenger.Default.Register<PropertyChangedMessage<string>>(this, m2 => receivedContent = m2.NewValue));
Messenger.Default.Broadcast(new GenericMessage<string>(TestContentString));
Messenger.Default.Send("Hello world");
}
Assert.AreEqual(null, TestRecipient.LastReceivedString);
Assert.AreEqual(0, TestRecipient.ReceivedStringMessages);
[TestMethod]
public void TestMessengerRegisteringMessageBaseInlineWhileReceivingMessageBase()
{
var receivedContent = string.Empty;
Messenger.Default.Register<string>(
this,
true,
m => Messenger.Default.Register<PropertyChangedMessage<string>>(this, true, m2 => receivedContent = m2.NewValue));
Messenger.Default.Broadcast(new GenericMessage<string>(TestContentStringNested));
Messenger.Default.Send("Hello world");
}
Assert.AreEqual(TestContentStringNested, TestRecipient.LastReceivedString);
Assert.AreEqual(20, TestRecipient.ReceivedStringMessages);
[TestMethod]
public void TestMessengerUnregisteringWhileReceiving()
{
Messenger.Default.Register<string>(
this,
m => Messenger.Default.Unregister(this));
Messenger.Default.Send("Hello world");
}
[TestMethod]
public void TestMessengerUnregisteringFromMessageBaseWhileReceiving()
{
Messenger.Default.Register<string>(
this,
true,
m => Messenger.Default.Unregister(this));
Messenger.Default.Send("Hello world");
}
public abstract class TestRecipient
@ -129,7 +148,7 @@ namespace GalaSoft.MvvmLight.Test.Messaging
ReceivedStringMessages = 0;
}
}
public class TestRecipient1 : TestRecipient
{
public TestRecipient1(bool register)
@ -181,77 +200,5 @@ namespace GalaSoft.MvvmLight.Test.Messaging
}
}
}
public class TestRecipient3 : TestRecipient1, IMessageRecipient
{
private TestRecipient3 _innerRecipient;
public TestRecipient3()
: base(false)
{
Messenger.Default.Register(this, typeof(GenericMessage<string>));
}
protected override void ReceiveString(GenericMessage<string> m)
{
_innerRecipient = new TestRecipient3();
}
public void ReceiveMessage(MessageBase message)
{
var casted = message as GenericMessage<string>;
if (casted != null)
{
if (casted.Content == TestContentString)
{
ReceiveString(casted);
return;
}
if (casted.Content == TestContentStringNested)
{
ReceiveStringNested(casted);
}
}
}
}
public class TestRecipient4 : TestRecipient2, IMessageRecipient
{
private TestRecipient4 _innerRecipient;
public TestRecipient4()
: base(false)
{
Messenger.Default.Register(this, typeof(MessageBase), true);
}
protected override void ReceiveString(MessageBase m)
{
var message = m as GenericMessage<string>;
if (message != null)
{
_innerRecipient = new TestRecipient4();
}
}
public void ReceiveMessage(MessageBase message)
{
var casted = message as GenericMessage<string>;
if (casted != null)
{
if (casted.Content == TestContentString)
{
ReceiveString(casted);
return;
}
if (casted.Content == TestContentStringNested)
{
ReceiveStringNested(casted);
}
}
}
}
}
}
}

Просмотреть файл

@ -0,0 +1,81 @@
using GalaSoft.MvvmLight.Messaging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace GalaSoft.MvvmLight.Test.Messaging
{
[TestClass]
public class MessengerSendToAllTest
{
public string StringContent1
{
get;
private set;
}
public string StringContent2
{
get;
private set;
}
[TestMethod]
public void TestSendingToAllRecipients()
{
const string TestContent = "abcd";
Reset();
Messenger.Reset();
Messenger.Default.Register<TestMessage>(this, m => StringContent1 = m.Content);
Messenger.Default.Register<TestMessage>(this, m => StringContent2 = m.Content);
var externalRecipient = new TestRecipient();
externalRecipient.RegisterWith(Messenger.Default);
Assert.AreEqual(null, StringContent1);
Assert.AreEqual(null, StringContent2);
Assert.AreEqual(null, externalRecipient.StringContent);
Messenger.Default.Send(new TestMessage
{
Content = TestContent
});
Assert.AreEqual(TestContent, StringContent1);
Assert.AreEqual(TestContent, StringContent2);
Assert.AreEqual(TestContent, externalRecipient.StringContent);
}
//// Helpers
private void Reset()
{
StringContent1 = null;
StringContent2 = null;
}
public class TestMessage
{
public string Content
{
get;
set;
}
}
private class TestRecipient
{
public string StringContent
{
get;
private set;
}
internal void RegisterWith(Messenger messenger)
{
messenger.Register<TestMessage>(this, m => StringContent = m.Content);
}
}
}
}

Просмотреть файл

@ -0,0 +1,139 @@
using GalaSoft.MvvmLight.Messaging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace GalaSoft.MvvmLight.Test.Messaging
{
[TestClass]
public class MessengerSendToTypeTest
{
[TestMethod]
public void TestBroadcastToOneType()
{
Messenger.Reset();
var recipient11 = new TestRecipient1();
var recipient12 = new TestRecipient1();
var recipient21 = new TestRecipient2();
var recipient22 = new TestRecipient2();
Messenger.Default.Register<string>(recipient11, recipient11.ReceiveMessage);
Messenger.Default.Register<string>(recipient12, recipient12.ReceiveMessage);
Messenger.Default.Register<string>(recipient21, recipient21.ReceiveMessage);
Messenger.Default.Register<string>(recipient22, recipient22.ReceiveMessage);
const string TestContent1 = "abcd";
const string TestContent2 = "efgh";
Assert.AreEqual(null, recipient11.ReceivedContentString);
Assert.AreEqual(null, recipient12.ReceivedContentString);
Assert.AreEqual(null, recipient21.ReceivedContentString);
Assert.AreEqual(null, recipient22.ReceivedContentString);
Messenger.Default.Send<string, TestRecipient1>(TestContent1);
Assert.AreEqual(TestContent1, recipient11.ReceivedContentString);
Assert.AreEqual(TestContent1, recipient12.ReceivedContentString);
Assert.AreEqual(null, recipient21.ReceivedContentString);
Assert.AreEqual(null, recipient22.ReceivedContentString);
Messenger.Default.Send<string, TestRecipient2>(TestContent2);
Assert.AreEqual(TestContent1, recipient11.ReceivedContentString);
Assert.AreEqual(TestContent1, recipient12.ReceivedContentString);
Assert.AreEqual(TestContent2, recipient21.ReceivedContentString);
Assert.AreEqual(TestContent2, recipient22.ReceivedContentString);
}
[TestMethod]
public void TestBroadcastToOneInterface()
{
Messenger.Reset();
var recipient11 = new TestRecipient1();
var recipient12 = new TestRecipient1();
var recipient21 = new TestRecipient2();
var recipient22 = new TestRecipient2();
var recipient31 = new TestRecipient3();
var recipient32 = new TestRecipient3();
Messenger.Default.Register<string>(recipient11, recipient11.ReceiveMessage);
Messenger.Default.Register<string>(recipient12, recipient12.ReceiveMessage);
Messenger.Default.Register<string>(recipient21, recipient21.DoSomething);
Messenger.Default.Register<string>(recipient22, recipient22.DoSomething);
Messenger.Default.Register<string>(recipient31, recipient31.DoSomething);
Messenger.Default.Register<string>(recipient32, recipient32.DoSomething);
const string TestContent1 = "abcd";
Assert.AreEqual(null, recipient11.ReceivedContentString);
Assert.AreEqual(null, recipient12.ReceivedContentString);
Assert.AreEqual(null, recipient21.ReceivedContentString);
Assert.AreEqual(null, recipient22.ReceivedContentString);
Assert.AreEqual(null, recipient31.ReceivedContentString);
Assert.AreEqual(null, recipient32.ReceivedContentString);
Messenger.Default.Send<string, ITestRecipient>(TestContent1);
Assert.AreEqual(null, recipient11.ReceivedContentString);
Assert.AreEqual(null, recipient12.ReceivedContentString);
Assert.AreEqual(TestContent1, recipient21.ReceivedContentString);
Assert.AreEqual(TestContent1, recipient22.ReceivedContentString);
Assert.AreEqual(TestContent1, recipient31.ReceivedContentString);
Assert.AreEqual(TestContent1, recipient32.ReceivedContentString);
}
//// Helpers
private class TestRecipient1
{
public string ReceivedContentString
{
get;
private set;
}
public void ReceiveMessage(string message)
{
ReceivedContentString = message;
}
}
private class TestRecipient2 : ITestRecipient
{
public string ReceivedContentString
{
get;
private set;
}
public void ReceiveMessage(string message)
{
ReceivedContentString = message;
}
public void DoSomething(string message)
{
ReceivedContentString = message;
}
}
private class TestRecipient3 : ITestRecipient
{
public string ReceivedContentString
{
get;
private set;
}
public void DoSomething(string message)
{
ReceivedContentString = message;
}
}
private interface ITestRecipient
{
void DoSomething(string message);
}
}
}

Просмотреть файл

@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using GalaSoft.MvvmLight.Messaging;
namespace GalaSoft.MvvmLight.Test.Messaging
{
[TestClass]
public class MessengerSendWithTokenTest
{
[TestMethod]
public void SendWithTokenTest()
{
string receivedContent1 = null;
string receivedContent2 = null;
string receivedContent3 = null;
Messenger.Reset();
var token1 = new object();
var token2 = new object();
Messenger.Default.Register<string>(this, m => receivedContent1 = m);
Messenger.Default.Register<string>(this, token1, m => receivedContent2 = m);
Messenger.Default.Register<string>(this, token2, m => receivedContent3 = m);
var message1 = "Hello world";
var message2 = "And again";
var message3 = "Third one";
Messenger.Default.Send(message1, token1);
Assert.IsNull(receivedContent1);
Assert.AreEqual(message1, receivedContent2);
Assert.IsNull(receivedContent3);
Messenger.Default.Send(message2, token2);
Assert.IsNull(receivedContent1);
Assert.AreEqual(message1, receivedContent2);
Assert.AreEqual(message2, receivedContent3);
Messenger.Default.Send(message3);
Assert.AreEqual(message3, receivedContent1);
Assert.AreEqual(message1, receivedContent2);
Assert.AreEqual(message2, receivedContent3);
}
[TestMethod]
public void SendMessageBaseWithTokenTest()
{
Exception receivedContent1 = null;
Exception receivedContent2 = null;
Exception receivedContent3 = null;
Messenger.Reset();
var token1 = new object();
var token2 = new object();
Messenger.Default.Register<Exception>(this, true, m => receivedContent1 = m);
Messenger.Default.Register<Exception>(this, token1, true, m => receivedContent2 = m);
Messenger.Default.Register<Exception>(this, token2, true, m => receivedContent3 = m);
var message = new InvalidOperationException();
Messenger.Default.Send(message, token1);
Assert.IsNull(receivedContent1);
Assert.AreEqual(message, receivedContent2);
Assert.IsNull(receivedContent3);
}
}
}

Просмотреть файл

@ -0,0 +1,191 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using GalaSoft.MvvmLight.Messaging;
namespace GalaSoft.MvvmLight.Test.Messaging
{
[TestClass]
public class NotificationMessageActionTest
{
private static readonly string DummyNotification1 = Guid.NewGuid().ToString();
private static readonly Random RandomGenerator = new Random();
[TestMethod]
public void TestNotificationMessageAction()
{
ExecuteTest(null, null);
}
[TestMethod]
public void TestNotificationMessageActionWithSender()
{
ExecuteTest(this, null);
}
[TestMethod]
public void TestNotificationMessageActionWithSenderAndTarget()
{
ExecuteTest(this, this);
}
[TestMethod]
public void TestNotificationMessageActionT1()
{
ExecuteTest(null, null, RandomGenerator.NextDouble());
}
[TestMethod]
public void TestNotificationMessageActionT1WithSender()
{
ExecuteTest(this, null, RandomGenerator.NextDouble());
}
[TestMethod]
public void TestNotificationMessageActionT1WithSenderAndTarget()
{
ExecuteTest(this, this, RandomGenerator.NextDouble());
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void TestNotificationMessageActionWithNullCallback1()
{
Messenger.Default.Register<NotificationMessageAction<DateTime>>(
this,
m => { });
Messenger.Default.Send(new NotificationMessageAction<DateTime>(DummyNotification1, null));
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void TestNotificationMessageActionWithNullCallback2()
{
Messenger.Default.Register<NotificationMessageAction<DateTime>>(
this,
m => { });
Messenger.Default.Send(new NotificationMessageAction<DateTime>(this, DummyNotification1, null));
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void TestNotificationMessageActionWithNullCallback3()
{
Messenger.Default.Register<NotificationMessageAction<DateTime>>(
this,
m => { });
Messenger.Default.Send(new NotificationMessageAction<DateTime>(this, this, DummyNotification1, null));
}
private void ExecuteTest(object sender, object target)
{
object receivedSender = null;
object receivedTarget = null;
string receivedNotification = null;
var callbackExecuted1 = false;
Messenger.Reset();
Messenger.Default.Register<NotificationMessageAction>(
this,
true,
m =>
{
receivedSender = m.Sender;
receivedTarget = m.Target;
receivedNotification = m.Notification;
m.Execute();
});
NotificationMessageAction message1 = null;
Action callback1 = () =>
{
callbackExecuted1 = true;
};
if (sender == null)
{
message1 = new NotificationMessageAction(DummyNotification1, callback1);
}
else
{
if (target == null)
{
message1 = new NotificationMessageAction(sender, DummyNotification1, callback1);
}
else
{
message1 = new NotificationMessageAction(sender, target, DummyNotification1, callback1);
}
}
Messenger.Default.Send(message1);
Assert.AreEqual(sender, receivedSender);
Assert.AreEqual(target, receivedTarget);
Assert.AreEqual(DummyNotification1, receivedNotification);
Assert.IsTrue(callbackExecuted1);
}
private void ExecuteTest<T>(object sender, object target, T argument1)
{
object receivedSender = null;
object receivedTarget = null;
string receivedNotification = null;
var receivedArgument1 = default(T);
var callbackExecuted1 = false;
Messenger.Reset();
Messenger.Default.Register<NotificationMessageAction<T>>(
this,
m =>
{
receivedSender = m.Sender;
receivedTarget = m.Target;
receivedNotification = m.Notification;
m.Execute(argument1);
});
NotificationMessageAction<T> message1 = null;
Action<T> callback1 = p =>
{
callbackExecuted1 = true;
receivedArgument1 = p;
};
if (sender == null)
{
message1 = new NotificationMessageAction<T>(DummyNotification1, callback1);
}
else
{
if (target == null)
{
message1 = new NotificationMessageAction<T>(sender, DummyNotification1, callback1);
}
else
{
message1 = new NotificationMessageAction<T>(sender, target, DummyNotification1, callback1);
}
}
Messenger.Default.Send(message1);
Assert.AreEqual(sender, receivedSender);
Assert.AreEqual(target, receivedTarget);
Assert.AreEqual(DummyNotification1, receivedNotification);
Assert.IsTrue(callbackExecuted1);
Assert.AreEqual(argument1, receivedArgument1);
}
}
}

Просмотреть файл

@ -0,0 +1,289 @@
using System;
using GalaSoft.MvvmLight.Messaging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace GalaSoft.MvvmLight.Test.Messaging
{
[TestClass]
public class NotificationMessageFuncTest
{
private static readonly string DummyNotification1 = Guid.NewGuid().ToString();
private static readonly string DummyNotification2 = Guid.NewGuid().ToString();
private static readonly Random RandomGenerator = new Random();
[TestMethod]
public void TestNotificationMessageFunc()
{
ExecuteTest(null, null);
}
[TestMethod]
public void TestNotificationMessageFuncWithSender()
{
ExecuteTest(this, null);
}
[TestMethod]
public void TestNotificationMessageFuncWithSenderAndTarget()
{
ExecuteTest(this, this);
}
[TestMethod]
public void TestNotificationMessageFuncT1()
{
ExecuteTest(null, null, RandomGenerator.NextDouble());
}
[TestMethod]
public void TestNotificationMessageFuncT1WithSender()
{
ExecuteTest(this, null, RandomGenerator.NextDouble());
}
[TestMethod]
public void TestNotificationMessageFuncT1WithSenderAndTarget()
{
ExecuteTest(this, this, RandomGenerator.NextDouble());
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void TestNotificationMessageFuncWithNullCallback1()
{
Messenger.Default.Register<NotificationMessageFunc<DateTime>>(
this,
m => {});
Messenger.Default.Send(new NotificationMessageFunc<DateTime>(DummyNotification1, null));
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void TestNotificationMessageFuncWithNullCallback2()
{
Messenger.Default.Register<NotificationMessageFunc<DateTime>>(
this,
m => { });
Messenger.Default.Send(new NotificationMessageFunc<DateTime>(this, DummyNotification1, null));
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void TestNotificationMessageFuncWithNullCallback3()
{
Messenger.Default.Register<NotificationMessageFunc<DateTime>>(
this,
m => { });
Messenger.Default.Send(new NotificationMessageFunc<DateTime>(this, this, DummyNotification1, null));
}
private void ExecuteTest(object sender, object target)
{
object receivedSender = null;
object receivedTarget = null;
string receivedNotification = null;
var callbackExecuted1 = false;
var callbackExecuted2 = false;
var returnedValue1 = DateTime.MinValue;
Exception returnedValue2 = null;
Messenger.Reset();
Messenger.Default.Register<NotificationMessageWithCallback>(
this,
true,
m =>
{
receivedSender = m.Sender;
receivedTarget = m.Target;
receivedNotification = m.Notification;
var casted1 = m as NotificationMessageFunc<DateTime>;
if (casted1 != null)
{
returnedValue1 = casted1.Execute();
return;
}
var casted2 = m as NotificationMessageFunc<Exception>;
if (casted2 != null)
{
returnedValue2 = casted2.Execute();
return;
}
});
NotificationMessageFunc<DateTime> message1 = null;
NotificationMessageFunc<Exception> message2 = null;
var returnedContent1 = DateTime.Now;
Exception returnedContent2 = new InvalidOperationException();
Func<DateTime> callback1 = () =>
{
callbackExecuted1 = true;
return returnedContent1;
};
Func<Exception> callback2 = () =>
{
callbackExecuted2 = true;
return returnedContent2;
};
if (sender == null)
{
message1 = new NotificationMessageFunc<DateTime>(DummyNotification1, callback1);
message2 = new NotificationMessageFunc<Exception>(DummyNotification2, callback2);
}
else
{
if (target == null)
{
message1 = new NotificationMessageFunc<DateTime>(sender, DummyNotification1, callback1);
message2 = new NotificationMessageFunc<Exception>(sender, DummyNotification2, callback2);
}
else
{
message1 = new NotificationMessageFunc<DateTime>(sender, target, DummyNotification1, callback1);
message2 = new NotificationMessageFunc<Exception>(sender, target, DummyNotification2, callback2);
}
}
Messenger.Default.Send(message1);
Assert.AreEqual(sender, receivedSender);
Assert.AreEqual(target, receivedTarget);
Assert.AreEqual(DummyNotification1, receivedNotification);
Assert.IsTrue(callbackExecuted1);
Assert.IsFalse(callbackExecuted2);
Assert.AreEqual(returnedContent1, returnedValue1);
Assert.AreEqual(null, returnedValue2);
receivedTarget = null;
receivedSender = null;
Messenger.Default.Send(message2);
Assert.AreEqual(sender, receivedSender);
Assert.AreEqual(target, receivedTarget);
Assert.AreEqual(DummyNotification2, receivedNotification);
Assert.IsTrue(callbackExecuted1);
Assert.IsTrue(callbackExecuted2);
Assert.AreEqual(returnedContent1, returnedValue1);
Assert.AreEqual(returnedContent2, returnedValue2);
}
private void ExecuteTest<T>(object sender, object target, T argument1)
{
object receivedSender = null;
object receivedTarget = null;
string receivedNotification = null;
var callbackExecuted1 = false;
var callbackExecuted2 = false;
T receivedArgument1 = default(T);
var returnedValue1 = DateTime.MinValue;
Exception returnedValue2 = null;
Messenger.Reset();
Messenger.Default.Register<NotificationMessageWithCallback>(
this,
true,
m =>
{
receivedSender = m.Sender;
receivedTarget = m.Target;
receivedNotification = m.Notification;
var casted1 = m as NotificationMessageFunc<T, DateTime>;
if (casted1 != null)
{
returnedValue1 = casted1.Execute(argument1);
return;
}
var casted2 = m as NotificationMessageFunc<T, Exception>;
if (casted2 != null)
{
returnedValue2 = casted2.Execute(argument1);
return;
}
});
NotificationMessageFunc<T, DateTime> message1 = null;
NotificationMessageFunc<T, Exception> message2 = null;
var returnedContent1 = DateTime.Now;
Exception returnedContent2 = new InvalidOperationException();
Func<T, DateTime> callback1 = p =>
{
callbackExecuted1 = true;
receivedArgument1 = p;
return returnedContent1;
};
Func<T, Exception> callback2 = p =>
{
callbackExecuted2 = true;
receivedArgument1 = p;
return returnedContent2;
};
if (sender == null)
{
message1 = new NotificationMessageFunc<T, DateTime>(DummyNotification1, callback1);
message2 = new NotificationMessageFunc<T, Exception>(DummyNotification2, callback2);
}
else
{
if (target == null)
{
message1 = new NotificationMessageFunc<T, DateTime>(sender, DummyNotification1, callback1);
message2 = new NotificationMessageFunc<T, Exception>(sender, DummyNotification2, callback2);
}
else
{
message1 = new NotificationMessageFunc<T, DateTime>(sender, target, DummyNotification1, callback1);
message2 = new NotificationMessageFunc<T, Exception>(sender, target, DummyNotification2, callback2);
}
}
Messenger.Default.Send(message1);
Assert.AreEqual(sender, receivedSender);
Assert.AreEqual(target, receivedTarget);
Assert.AreEqual(DummyNotification1, receivedNotification);
Assert.IsTrue(callbackExecuted1);
Assert.IsFalse(callbackExecuted2);
Assert.AreEqual(returnedContent1, returnedValue1);
Assert.AreEqual(null, returnedValue2);
Assert.AreEqual(argument1, receivedArgument1);
receivedTarget = null;
receivedSender = null;
receivedArgument1 = default(T);
Messenger.Default.Send(message2);
Assert.AreEqual(sender, receivedSender);
Assert.AreEqual(target, receivedTarget);
Assert.AreEqual(DummyNotification2, receivedNotification);
Assert.IsTrue(callbackExecuted1);
Assert.IsTrue(callbackExecuted2);
Assert.AreEqual(returnedContent1, returnedValue1);
Assert.AreEqual(returnedContent2, returnedValue2);
Assert.AreEqual(argument1, receivedArgument1);
}
}
}

Просмотреть файл

@ -0,0 +1,181 @@
using System;
using GalaSoft.MvvmLight.Messaging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace GalaSoft.MvvmLight.Test.Messaging
{
[TestClass]
public class NotificationMessageTest
{
private static readonly string DummyNotification1 = Guid.NewGuid().ToString();
private static readonly string DummyNotification2 = Guid.NewGuid().ToString();
public void ExecuteTestGeneric(object sender, object target)
{
var testContent1 = new InvalidOperationException();
var testContent2 = new InvalidCastException();
Exception receivedContent1 = null;
Exception receivedContent2 = null;
object receivedSender = null;
object receivedTarget = null;
Messenger.Reset();
Messenger.Default.Register<NotificationMessage<Exception>>(
this,
m =>
{
receivedSender = m.Sender;
receivedTarget = m.Target;
if (m.Notification == DummyNotification1)
{
receivedContent1 = testContent1;
return;
}
if (m.Notification == DummyNotification2)
{
receivedContent2 = testContent2;
return;
}
});
Assert.AreEqual(null, receivedContent1);
Assert.AreEqual(null, receivedContent2);
NotificationMessage<Exception> message1;
NotificationMessage<Exception> message2;
if (sender == null)
{
message1 = new NotificationMessage<Exception>(testContent1, DummyNotification1);
message2 = new NotificationMessage<Exception>(testContent2, DummyNotification2);
}
else
{
if (target == null)
{
message1 = new NotificationMessage<Exception>(sender, testContent1, DummyNotification1);
message2 = new NotificationMessage<Exception>(sender, testContent2, DummyNotification2);
}
else
{
message1 = new NotificationMessage<Exception>(sender, target, testContent1, DummyNotification1);
message2 = new NotificationMessage<Exception>(sender, target, testContent2, DummyNotification2);
}
}
Messenger.Default.Send(message1);
Assert.AreEqual(sender, receivedSender);
Assert.AreEqual(target, receivedTarget);
Assert.AreEqual(testContent1, receivedContent1);
Assert.AreEqual(null, receivedContent2);
receivedSender = null;
receivedTarget = null;
Messenger.Default.Send(message2);
Assert.AreEqual(sender, receivedSender);
Assert.AreEqual(target, receivedTarget);
Assert.AreEqual(testContent1, receivedContent1);
Assert.AreEqual(testContent2, receivedContent2);
}
[TestMethod]
public void TestNotificationGenericMessage()
{
ExecuteTestGeneric(null, null);
}
[TestMethod]
public void TestNotificationGenericMessageWithSender()
{
ExecuteTestGeneric(this, null);
}
[TestMethod]
public void TestNotificationGenericMessageWithTarget()
{
ExecuteTestGeneric(this, this);
}
[TestMethod]
public void TestNotificationMessage()
{
ExecuteTest(null, null);
}
[TestMethod]
public void TestNotificationMessageWithSender()
{
ExecuteTest(this, null);
}
[TestMethod]
public void TestNotificationMessageWithSenderAndTarget()
{
ExecuteTest(this, this);
}
private void ExecuteTest(object sender, object target)
{
object receivedSender = null;
object receivedTarget = null;
string receivedNotification = null;
Messenger.Reset();
Messenger.Default.Register<NotificationMessage>(
this,
m =>
{
receivedSender = m.Sender;
receivedTarget = m.Target;
receivedNotification = m.Notification;
});
NotificationMessage message1;
NotificationMessage message2;
if (sender == null)
{
message1 = new NotificationMessage(DummyNotification1);
message2 = new NotificationMessage(DummyNotification2);
}
else
{
if (target == null)
{
message1 = new NotificationMessage(sender, DummyNotification1);
message2 = new NotificationMessage(sender, DummyNotification2);
}
else
{
message1 = new NotificationMessage(sender, target, DummyNotification1);
message2 = new NotificationMessage(sender, target, DummyNotification2);
}
}
Messenger.Default.Send(message1);
Assert.AreEqual(sender, receivedSender);
Assert.AreEqual(target, receivedTarget);
Assert.AreEqual(DummyNotification1, receivedNotification);
receivedTarget = null;
receivedSender = null;
Messenger.Default.Send(message2);
Assert.AreEqual(sender, receivedSender);
Assert.AreEqual(target, receivedTarget);
Assert.AreEqual(DummyNotification2, receivedNotification);
}
}
}

Просмотреть файл

@ -35,46 +35,43 @@ namespace GalaSoft.MvvmLight.Test.Messaging
Messenger.Reset();
Messenger.Default.Register<PropertyChangedMessageBase>(this,
true,
m =>
{
receivedSender = m.Sender;
receivedTarget = m.Target;
messageWasReceived = true;
Messenger.Default.Register<PropertyChangedMessageBase>(
this,
true,
m =>
{
receivedSender = m.Sender;
receivedTarget = m.Target;
messageWasReceived = true;
var exceptionMessage =
m as
PropertyChangedMessage
<InvalidOperationException>;
var exceptionMessage =
m as PropertyChangedMessage<InvalidOperationException>;
if (exceptionMessage != null
&&
exceptionMessage.PropertyName
== TestViewModel.MyExceptionPropertyName)
{
receivedPreviousException =
exceptionMessage.OldValue;
receivedCurrentException =
exceptionMessage.NewValue;
return;
}
if (exceptionMessage != null
&&
exceptionMessage.PropertyName
== TestViewModel.MyExceptionPropertyName)
{
receivedPreviousException =
exceptionMessage.OldValue;
receivedCurrentException =
exceptionMessage.NewValue;
return;
}
var dateMessage =
m as PropertyChangedMessage<DateTime>;
var dateMessage =
m as PropertyChangedMessage<DateTime>;
if (dateMessage != null
&&
dateMessage.PropertyName
== TestViewModel.MyDatePropertyName)
{
receivedPreviousDateTime =
dateMessage.OldValue;
receivedCurrentDateTime =
dateMessage.NewValue;
return;
}
});
if (dateMessage != null
&& dateMessage.PropertyName == TestViewModel.MyDatePropertyName)
{
receivedPreviousDateTime =
dateMessage.OldValue;
receivedCurrentDateTime =
dateMessage.NewValue;
return;
}
});
Assert.AreEqual(DateTime.MinValue, receivedPreviousDateTime);
Assert.AreEqual(DateTime.MinValue, receivedCurrentDateTime);

Просмотреть файл

@ -12,138 +12,211 @@ namespace GalaSoft.MvvmLight.Test
[TestClass]
public class ViewModelBaseTest
{
private DateTime _receivedDateTime;
[TestMethod]
public void DisposeTest()
public void TestDispose()
{
Messenger.Reset();
var vm = new OldTestViewModel();
Messenger.Default.Register(vm, typeof(OldTestMessage));
var vm = new TestViewModel();
Messenger.Default.Register<string>(vm, vm.HandleStringMessage);
const string Content1 = "Hello world";
const string Content2 = "Another message";
var message1 = new OldTestMessage(this, Content1);
Messenger.Default.Broadcast(message1);
Messenger.Default.Send(Content1);
Assert.AreEqual(Content1, vm.ReceivedContent);
vm.Dispose();
var message2 = new OldTestMessage(this, Content2);
Messenger.Default.Broadcast(message2);
Messenger.Default.Send(Content2);
Assert.AreEqual(Content1, vm.ReceivedContent);
}
[TestMethod]
public void PropertyChangedTestBroadcast()
public void TestPropertyChangedSend()
{
Messenger.Reset();
_receivedDateTime = DateTime.MinValue;
var receivedDateTimeMessengerOld = DateTime.MaxValue;
var receivedDateTimeMessengerNew = DateTime.MinValue;
var receivedDateTimeLocal = DateTime.MinValue;
var recipient = new OldTestRecipient();
Messenger.Default.Register(recipient, typeof(PropertyChangedMessage<DateTime>));
Messenger.Default.Register<PropertyChangedMessage<DateTime>>(this, m =>
{
if (m.PropertyName == TestViewModel.LastChanged1PropertyName)
{
receivedDateTimeMessengerOld = m.OldValue;
receivedDateTimeMessengerNew = m.NewValue;
}
});
var vm = new OldTestViewModel();
vm.PropertyChanged += VMPropertyChanged;
Assert.AreEqual(_receivedDateTime, DateTime.MinValue);
var vm = new TestViewModel();
vm.PropertyChanged += (s, e) =>
{
if (e.PropertyName == TestViewModel.LastChanged1PropertyName)
{
receivedDateTimeLocal = vm.LastChanged1;
}
};
var now = DateTime.Now;
vm.LastChanged2 = now;
Assert.AreEqual(now, vm.LastChanged2);
Assert.AreEqual(_receivedDateTime, vm.LastChanged2);
Assert.AreEqual(now, recipient.DateTimeContent);
vm.LastChanged1 = now;
Assert.AreEqual(now, vm.LastChanged1);
Assert.AreEqual(DateTime.MinValue, receivedDateTimeMessengerOld);
Assert.AreEqual(now, receivedDateTimeMessengerNew);
Assert.AreEqual(now, receivedDateTimeLocal);
}
[TestMethod]
public void PropertyChangedTestBroadcastApplicationMessenger()
public void TestPropertyChangedSendWithCustomMessenger()
{
_receivedDateTime = DateTime.MinValue;
var receivedDateTime1 = DateTime.MinValue;
var receivedDateTime2 = DateTime.MinValue;
var messenger = new Messenger();
var recipient1 = new OldTestRecipient();
var recipient2 = new OldTestRecipient();
messenger.Register(recipient1, typeof(PropertyChangedMessage<DateTime>));
Messenger.Default.Register(recipient2, typeof(PropertyChangedMessage<DateTime>));
messenger.Register<PropertyChangedMessage<DateTime>>(this, m => receivedDateTime1 = m.NewValue);
Messenger.Default.Register<PropertyChangedMessage<DateTime>>(this, m => receivedDateTime2 = m.NewValue);
var vm = new OldTestViewModel(messenger);
vm.PropertyChanged += VMPropertyChanged;
Assert.AreEqual(_receivedDateTime, DateTime.MinValue);
var now = DateTime.Now;
vm.LastChanged2 = now;
Assert.AreEqual(now, vm.LastChanged2);
Assert.AreEqual(_receivedDateTime, vm.LastChanged2);
Assert.AreEqual(now, recipient1.DateTimeContent);
Assert.AreEqual(DateTime.MinValue, recipient2.DateTimeContent);
}
[TestMethod]
public void PropertyChangedTestBroadcastNonStaticMessenger()
{
_receivedDateTime = DateTime.MinValue;
var messenger = new Messenger();
var recipient1 = new OldTestRecipient();
var recipient2 = new OldTestRecipient();
messenger.Register(recipient1, typeof(PropertyChangedMessage<DateTime>));
Messenger.Default.Register(recipient2, typeof(PropertyChangedMessage<DateTime>));
var vm = new OldTestViewModel(messenger);
vm.PropertyChanged += VMPropertyChanged;
Assert.AreEqual(_receivedDateTime, DateTime.MinValue);
var now = DateTime.Now;
vm.LastChanged2 = now;
Assert.AreEqual(now, vm.LastChanged2);
Assert.AreEqual(_receivedDateTime, vm.LastChanged2);
Assert.AreEqual(now, recipient1.DateTimeContent);
Assert.AreEqual(DateTime.MinValue, recipient2.DateTimeContent);
}
[TestMethod]
public void PropertyChangedTestNoBroadcast()
{
Messenger.Reset();
_receivedDateTime = DateTime.MinValue;
var recipient = new OldTestRecipient();
Messenger.Default.Register(recipient, typeof(PropertyChangedMessage<DateTime>));
var vm = new OldTestViewModel();
vm.PropertyChanged += VMPropertyChanged;
Assert.AreEqual(_receivedDateTime, DateTime.MinValue);
var vm = new TestViewModel(messenger);
var now = DateTime.Now;
vm.LastChanged1 = now;
Assert.AreEqual(now, vm.LastChanged1);
Assert.AreEqual(_receivedDateTime, vm.LastChanged1);
Assert.AreEqual(DateTime.MinValue, recipient.DateTimeContent);
Assert.AreEqual(now, receivedDateTime1);
Assert.AreEqual(DateTime.MinValue, receivedDateTime2);
}
private void VMPropertyChanged(object sender, PropertyChangedEventArgs e)
[TestMethod]
public void TestPropertyChangeNoBroadcast()
{
if (e.PropertyName
== OldTestViewModel.LastChanged1PropertyName)
Messenger.Reset();
var receivedDateTimeLocal = DateTime.MinValue;
var receivedDateTimeMessenger = DateTime.MinValue;
Messenger.Default.Register<PropertyChangedMessage<DateTime>>(this, m =>
{
_receivedDateTime = ((OldTestViewModel) sender).LastChanged1;
return;
}
if (e.PropertyName
== OldTestViewModel.LastChanged2PropertyName)
if (m.PropertyName == TestViewModel.LastChanged2PropertyName)
{
receivedDateTimeMessenger = m.NewValue;
}
});
var vm = new TestViewModel();
vm.PropertyChanged += (s, e) =>
{
if (e.PropertyName == TestViewModel.LastChanged2PropertyName)
{
receivedDateTimeLocal = vm.LastChanged2;
}
};
var now = DateTime.Now;
vm.LastChanged2 = now;
Assert.AreEqual(now, vm.LastChanged2);
Assert.AreEqual(now, receivedDateTimeLocal);
Assert.AreEqual(DateTime.MinValue, receivedDateTimeMessenger);
}
}
public class TestViewModel : ViewModelBase
{
public const string ReceivedContentPropertyName = "ReceivedContent";
private string _receivedContent;
public string ReceivedContent
{
get
{
_receivedDateTime = ((OldTestViewModel) sender).LastChanged2;
return;
return _receivedContent;
}
private set
{
if (_receivedContent == value)
{
return;
}
_receivedContent = value;
RaisePropertyChanged(ReceivedContentPropertyName);
}
}
public const string LastChanged1PropertyName = "LastChanged1";
private DateTime _lastChanged1;
public DateTime LastChanged1
{
get
{
return _lastChanged1;
}
set
{
if (_lastChanged1 == value)
{
return;
}
var oldValue = _lastChanged1;
_lastChanged1 = value;
// Update bindings and broadcast change using GalaSoft.Utility.Messenging
RaisePropertyChanged(LastChanged1PropertyName, oldValue, value, true);
}
}
/// <summary>
/// The <see cref="LastChanged2" /> property's name.
/// </summary>
public const string LastChanged2PropertyName = "LastChanged2";
private DateTime _lastChanged2;
/// <summary>
/// Gets the LastChanged2 property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public DateTime LastChanged2
{
get
{
return _lastChanged2;
}
set
{
if (_lastChanged2 == value)
{
return;
}
_lastChanged2 = value;
// Update bindings, no broadcast
RaisePropertyChanged(LastChanged2PropertyName);
}
}
internal void HandleStringMessage(string message)
{
ReceivedContent = message;
}
public TestViewModel()
{
}
public TestViewModel(IMessenger messenger)
: base(messenger)
{
}
}
}

Просмотреть файл

@ -10,7 +10,7 @@
// <license>
// See license.txt in this project or http://www.galasoft.ch/license_MIT.txt
// </license>
// <LastBaseLevel>BL0007</LastBaseLevel>
// <LastBaseLevel>BL0008</LastBaseLevel>
// ****************************************************************************
// <credits>This class was developed by Josh Smith (http://joshsmithonwpf.wordpress.com) and
// slightly modified with his permission.</credits>
@ -32,8 +32,8 @@ namespace GalaSoft.MvvmLight.Command
/// Execute and CanExecute callback methods.
/// </summary>
////[ClassInfo(typeof(RelayCommand),
//// VersionString = "2.0.0.0",
//// DateString = "200909281610",
//// VersionString = "3.0.0.0/BL0008",
//// DateString = "200910251406",
//// Description = "A command whose sole purpose is to relay its functionality to other objects by invoking delegates.",
//// UrlContacts = "http://www.galasoft.ch/contact_en.html",
//// Email = "laurent@galasoft.ch")]
@ -100,22 +100,6 @@ namespace GalaSoft.MvvmLight.Command
}
#endif
/// <summary>
/// Raises the <see cref="CanExecuteChanged" /> event.
/// </summary>
/// <param name="force">This parameter has no effect and is always true.</param>
[SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic",
Justification = "The this keyword is used in the Silverlight version")]
[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate",
Justification = "This cannot be an event")]
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters",
Justification = "The parameter is used in the WPF version")]
[Obsolete("Use the method RaiseCanExecuteChanged() instead.")]
public void RaiseCanExecuteChanged(bool force)
{
RaiseCanExecuteChanged();
}
/// <summary>
/// Raises the <see cref="CanExecuteChanged" /> event.
/// </summary>

Просмотреть файл

@ -95,22 +95,6 @@ namespace GalaSoft.MvvmLight.Command
}
#endif
/// <summary>
/// Raises the <see cref="CanExecuteChanged" /> event.
/// </summary>
/// <param name="force">This parameter has no effect and is always true.</param>
[SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic",
Justification = "The this keyword is used in the Silverlight version")]
[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate",
Justification = "This cannot be an event")]
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters",
Justification = "The parameter is used in the WPF version")]
[Obsolete("Use the method RaiseCanExecuteChanged() instead.")]
public void RaiseCanExecuteChanged(bool force)
{
RaiseCanExecuteChanged();
}
/// <summary>
/// Raises the <see cref="CanExecuteChanged" /> event.
/// </summary>

Просмотреть файл

@ -62,13 +62,16 @@
<Compile Include="Messaging\CommandMessage.cs" />
<Compile Include="Messaging\CommandMessageGeneric.cs" />
<Compile Include="Messaging\IMessenger.cs" />
<Compile Include="Messaging\NotificationMessage.cs" />
<Compile Include="Messaging\NotificationMessageAction.cs" />
<Compile Include="Messaging\NotificationMessageFunc.cs" />
<Compile Include="Messaging\NotificationMessageGeneric.cs" />
<Compile Include="Messaging\NotificationMessageWithCallback.cs" />
<Compile Include="Messaging\PropertyChangedMessageBase.cs" />
<Compile Include="Messaging\DialogMessage.cs" />
<Compile Include="Messaging\GenericMessage.cs" />
<Compile Include="Messaging\IMessageRecipient.cs" />
<Compile Include="Messaging\MessageBase.cs" />
<Compile Include="Messaging\Messenger.cs" />
<Compile Include="Messaging\OldMessenger.cs" />
<Compile Include="Messaging\PropertyChangedMessage.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Command\RelayCommand.cs" />

Просмотреть файл

@ -20,6 +20,7 @@ namespace GalaSoft.MvvmLight.Helpers
/// useful if you store multiple WeakAction{T} instances but don't know in advance
/// what type T represents.
/// </summary>
////[ClassInfo(typeof(Messenger))]
public interface IExecuteWithObject
{
/// <summary>

Просмотреть файл

@ -21,6 +21,7 @@ namespace GalaSoft.MvvmLight.Helpers
/// Stores an <see cref="Action" /> without causing a hard reference
/// to be created to the Action's owner. The owner can be garbage collected at any time.
/// </summary>
////[ClassInfo(typeof(Messenger))]
public class WeakAction
{
private readonly Action _action;

Просмотреть файл

@ -22,6 +22,7 @@ namespace GalaSoft.MvvmLight.Helpers
/// to be created to the Action's owner. The owner can be garbage collected at any time.
/// </summary>
/// <typeparam name="T">The type of the Action's parameter.</typeparam>
////[ClassInfo(typeof(Messenger))]
public class WeakAction<T> : WeakAction, IExecuteWithObject
{
private readonly Action<T> _action;

Просмотреть файл

@ -15,12 +15,14 @@
////using GalaSoft.Utilities.Attributes;
using System;
namespace GalaSoft.MvvmLight.Messaging
{
/// <summary>
/// Passes a string message (Command) to a recipient.
/// </summary>
////[ClassInfo(typeof(Messenger))]
[Obsolete("This class has been replaced by NotificationMessage. Only the name changed. Please use the new class from now on.")]
public class CommandMessage : MessageBase
{
/// <summary>

Просмотреть файл

@ -13,6 +13,8 @@
// </license>
// ****************************************************************************
using System;
////using GalaSoft.Utilities.Attributes;
namespace GalaSoft.MvvmLight.Messaging
@ -22,6 +24,7 @@ namespace GalaSoft.MvvmLight.Messaging
/// </summary>
/// <typeparam name="T">The type of the Content property.</typeparam>
////[ClassInfo(typeof(Messenger))]
[Obsolete("This class has been replaced by NotificationMessage<T>. Only the name changed, nothing else. Please use the new class from now on.")]
public class CommandMessage<T> : GenericMessage<T>
{
/// <summary>

Просмотреть файл

@ -11,7 +11,7 @@
// <license>
// See license.txt in this project or http://www.galasoft.ch/license_MIT.txt
// </license>
// <LastBaseLevel>BL0008</LastBaseLevel>
// <LastBaseLevel>BL0009</LastBaseLevel>
// ****************************************************************************
using System;
@ -28,24 +28,22 @@ namespace GalaSoft.MvvmLight.Messaging
/// The Messenger is a class allowing objects to exchange messages.
/// </summary>
////[ClassInfo(typeof(Messenger),
//// VersionString = "2.0.0.0",
//// DateString = "200909281606",
//// VersionString = "3.0.0.0/BL0009",
//// DateString = "200910251258",
//// Description = "A messenger class allowing a class to send a message to multiple recipients",
//// UrlContacts = "http://www.galasoft.ch/contact_en.html",
//// Email = "laurent@galasoft.ch")]
public class Messenger : IMessenger
{
private readonly OldMessenger _oldMessenger = new OldMessenger();
private static Messenger _defaultInstance;
private Dictionary<Type, List<WeakAction>> _recipientsOfSubclassesAction;
private Dictionary<Type, List<WeakActionAndToken>> _recipientsOfSubclassesAction;
private Dictionary<Type, List<WeakAction>> _recipientsStrictAction;
private Dictionary<Type, List<WeakActionAndToken>> _recipientsStrictAction;
/// <summary>
/// Gets the Messenger's default instance, allowing
/// to register and broadcast in a static manner.
/// to register and send messages in a static manner.
/// </summary>
public static Messenger Default
{
@ -60,6 +58,11 @@ namespace GalaSoft.MvvmLight.Messaging
}
}
public static void OverrideDefault(Messenger newMessenger)
{
_defaultInstance = newMessenger;
}
/// <summary>
/// Sets the Messenger's default (static) instance to null.
/// </summary>
@ -68,60 +71,6 @@ namespace GalaSoft.MvvmLight.Messaging
_defaultInstance = null;
}
//// OLD
/// <summary>
/// Broacasts a message to the recipients who registered for
/// this type of message.
/// </summary>
/// <param name="message">The message to broadcast.</param>
[Obsolete("Please use the method Send<TMessage> instead.")]
public void Broadcast(MessageBase message)
{
_oldMessenger.Broadcast(message);
}
/// <summary>
/// Registers a message recipient for a given type of messages, with
/// the Messenger's default instance. Registration is made in a way that
/// it is not necessary to unregister a recipient before it is disposed.
/// </summary>
/// <param name="recipient">The recipient.</param>
/// <param name="messageType">The message type that the recipient registers
/// for. The type must inherit MessageBase.</param>
/// <exception cref="ArgumentException">If the messageType does not
/// inherit <see cref="MessageBase"/></exception>
[Obsolete("Please register a recipient with the generic methods 'Messenger.Register<TMessage>'.")]
public void Register(IMessageRecipient recipient, Type messageType)
{
Register(recipient, messageType, false);
}
/// <summary>
/// Registers a message recipient for a given type of messages, with
/// the Messenger's default instance. Registration is made in a way that
/// it is not necessary to unregister a recipient before it is disposed.
/// </summary>
/// <param name="recipient">The recipient.</param>
/// <param name="messageType">The message type that the recipient registers
/// for. The type must inherit MessageBase.</param>
/// <param name="receiveDerivedMessagesToo">If true, the recipient will also receive
/// message types deriving from the registered message type. For example, registering
/// for <see cref="MessageBase" /> will pass all the messages to the recipient.
/// If the message type registered is a generic type without type arguments, all the
/// messages of this generic type will be received. For example, registering the message
/// type "GenericMessage&lt;&gt;" will get the message types "GenericMessage&lt;string&gt;",
/// "GenericMessage&lt;Exception&gt;", etc...</param>
/// <exception cref="ArgumentException">If the messageType does not
/// inherit <see cref="MessageBase"/></exception>
[Obsolete("Please register a recipient with the generic methods 'Messenger.Register<TMessage>'.")]
public void Register(IMessageRecipient recipient, Type messageType, bool receiveDerivedMessagesToo)
{
_oldMessenger.Register(recipient, messageType, receiveDerivedMessagesToo);
}
//// NEW
/// <summary>
/// Registers a recipient for a type of message TMessage. The <see cref="action" />
/// parameter will be executed when a corresponding message is sent.
@ -133,9 +82,19 @@ namespace GalaSoft.MvvmLight.Messaging
/// <param name="recipient">The recipient that will receive the messages.</param>
/// <param name="action">The action that will be executed when a message
/// of type TMessage is sent.</param>
public void Register<TMessage>(object recipient, Action<TMessage> action)
public virtual void Register<TMessage>(object recipient, Action<TMessage> action)
{
Register(recipient, false, action);
Register(recipient, null, false, action);
}
public virtual void Register<TMessage>(object recipient, bool receiveDerivedMessagesToo, Action<TMessage> action)
{
Register(recipient, null, receiveDerivedMessagesToo, action);
}
public virtual void Register<TMessage>(object recipient, object token, Action<TMessage> action)
{
Register(recipient, token, false, action);
}
/// <summary>
@ -163,17 +122,21 @@ namespace GalaSoft.MvvmLight.Messaging
/// </param>
/// <param name="action">The action that will be executed when a message
/// of type TMessage is sent.</param>
public void Register<TMessage>(object recipient, bool receiveDerivedMessagesToo, Action<TMessage> action)
public virtual void Register<TMessage>(
object recipient,
object token,
bool receiveDerivedMessagesToo,
Action<TMessage> action)
{
var messageType = typeof(TMessage);
Dictionary<Type, List<WeakAction>> recipients;
Dictionary<Type, List<WeakActionAndToken>> recipients;
if (receiveDerivedMessagesToo)
{
if (_recipientsOfSubclassesAction == null)
{
_recipientsOfSubclassesAction = new Dictionary<Type, List<WeakAction>>();
_recipientsOfSubclassesAction = new Dictionary<Type, List<WeakActionAndToken>>();
}
recipients = _recipientsOfSubclassesAction;
@ -182,17 +145,17 @@ namespace GalaSoft.MvvmLight.Messaging
{
if (_recipientsStrictAction == null)
{
_recipientsStrictAction = new Dictionary<Type, List<WeakAction>>();
_recipientsStrictAction = new Dictionary<Type, List<WeakActionAndToken>>();
}
recipients = _recipientsStrictAction;
}
List<WeakAction> list;
List<WeakActionAndToken> list;
if (!recipients.ContainsKey(messageType))
{
list = new List<WeakAction>();
list = new List<WeakActionAndToken>();
recipients.Add(messageType, list);
}
else
@ -201,11 +164,23 @@ namespace GalaSoft.MvvmLight.Messaging
}
var weakAction = new WeakAction<TMessage>(recipient, action);
list.Add(weakAction);
var item = new WeakActionAndToken
{
Action = weakAction,
Token = token
};
list.Add(item);
Cleanup();
}
private struct WeakActionAndToken
{
public WeakAction Action;
public object Token;
}
/// <summary>
/// Sends a message to registered recipients. The message will
/// reach all recipients that registered for this message type
@ -213,9 +188,9 @@ namespace GalaSoft.MvvmLight.Messaging
/// </summary>
/// <typeparam name="TMessage">The type of message that will be sent.</typeparam>
/// <param name="message">The message to send to registered recipients.</param>
public void Send<TMessage>(TMessage message)
public virtual void Send<TMessage>(TMessage message)
{
SendToTargetOrType(message, null);
SendToTargetOrType(message, null, null);
}
/// <summary>
@ -232,54 +207,25 @@ namespace GalaSoft.MvvmLight.Messaging
"Microsoft.Design",
"CA1004:GenericMethodsShouldProvideTypeParameter",
Justification = "This syntax is more convenient than other alternatives.")]
public void Send<TMessage, TTarget>(TMessage message)
public virtual void Send<TMessage, TTarget>(TMessage message)
{
SendToTargetOrType(message, typeof(TTarget));
SendToTargetOrType(message, typeof(TTarget), null);
}
//// OLD
/// <summary>
/// Unregisters a previously registered message recipient. After it is unregistered,
/// the recipient will not get any messages anymore.
/// </summary>
/// <param name="recipient">The recipient to unregister.</param>
[Obsolete("Please unregister a recipient with the generic methods 'Messenger.Unregister<TMessage>'.")]
public void Unregister(IMessageRecipient recipient)
public virtual void Send<TMessage>(TMessage message, object token)
{
_oldMessenger.Unregister(recipient);
SendToTargetOrType(message, null, token);
}
/// <summary>
/// Unregisters a previously registered message recipient. After it is unregistered,
/// the recipient will not get any messages of the given message type anymore..
/// </summary>
/// <param name="recipient">The recipient to unregister.</param>
/// <param name="messageType">The message type for which the
/// recipient wants to unregister.</param>
[Obsolete("Please unregister a recipient with the generic methods 'Messenger.Unregister<TMessage>'.")]
public void Unregister(IMessageRecipient recipient, Type messageType)
{
_oldMessenger.Unregister(recipient, messageType);
}
//// NEW
/// <summary>
/// Unregisters a messager recipient completely. After this method
/// is executed, the recipient will not receive any messages anymore.
/// </summary>
/// <param name="recipient">The recipient that must be unregistered.</param>
public void Unregister(object recipient)
public virtual void Unregister(object recipient)
{
UnregisterFromLists(recipient, _recipientsOfSubclassesAction);
UnregisterFromLists(recipient, _recipientsStrictAction);
var oldRecipient = recipient as IMessageRecipient;
if (oldRecipient != null)
{
_oldMessenger.Unregister(oldRecipient);
}
}
/// <summary>
@ -296,7 +242,7 @@ namespace GalaSoft.MvvmLight.Messaging
"CA1004:GenericMethodsShouldProvideTypeParameter",
Justification =
"The type parameter TMessage identifies the message type that the recipient wants to unregister for.")]
public void Unregister<TMessage>(object recipient)
public virtual void Unregister<TMessage>(object recipient)
{
Unregister<TMessage>(recipient, null);
}
@ -313,14 +259,14 @@ namespace GalaSoft.MvvmLight.Messaging
/// <param name="recipient">The recipient that must be unregistered.</param>
/// <param name="action">The action that must be unregistered for
/// the recipient and for the message type TMessage.</param>
public void Unregister<TMessage>(object recipient, Action<TMessage> action)
public virtual void Unregister<TMessage>(object recipient, Action<TMessage> action)
{
UnregisterFromLists(recipient, action, _recipientsStrictAction);
UnregisterFromLists(recipient, action, _recipientsOfSubclassesAction);
Cleanup();
}
private static void CleanupList(IDictionary<Type, List<WeakAction>> lists)
private static void CleanupList(IDictionary<Type, List<WeakActionAndToken>> lists)
{
if (lists == null)
{
@ -330,13 +276,13 @@ namespace GalaSoft.MvvmLight.Messaging
var listsToRemove = new List<Type>();
foreach (var list in lists)
{
var recipientsToRemove = new List<WeakAction>();
foreach (var recipient in list.Value)
var recipientsToRemove = new List<WeakActionAndToken>();
foreach (var item in list.Value)
{
if (recipient == null
|| !recipient.IsAlive)
if (item.Action == null
|| !item.Action.IsAlive)
{
recipientsToRemove.Add(recipient);
recipientsToRemove.Add(item);
}
}
@ -357,18 +303,18 @@ namespace GalaSoft.MvvmLight.Messaging
}
}
private static bool Implements(Type messageType, Type type)
private static bool Implements(Type instanceType, Type interfaceType)
{
if (type == null
|| messageType == null)
if (interfaceType == null
|| instanceType == null)
{
return false;
}
var interfaces = messageType.GetInterfaces();
var interfaces = instanceType.GetInterfaces();
foreach (var currentInterface in interfaces)
{
if (currentInterface == type)
if (currentInterface == interfaceType)
{
return true;
}
@ -379,8 +325,9 @@ namespace GalaSoft.MvvmLight.Messaging
private static void SendToList<TMessage>(
TMessage message,
IEnumerable<WeakAction> list,
Type messageTargetType)
IEnumerable<WeakActionAndToken> list,
Type messageTargetType,
object token)
{
if (list != null)
{
@ -388,16 +335,19 @@ namespace GalaSoft.MvvmLight.Messaging
// Bug correction Messaging BL0004.007
var listClone = list.Take(list.Count()).ToList();
foreach (var action in listClone)
foreach (var item in listClone)
{
var executeAction = action as IExecuteWithObject;
var executeAction = item.Action as IExecuteWithObject;
if (executeAction != null
&& item.Action.IsAlive
&& item.Action.Target != null
if (action != null
&& executeAction != null
&& action.IsAlive
&& action.Target != null
&& (messageTargetType == null
|| action.Target.GetType() == messageTargetType))
|| item.Action.Target.GetType() == messageTargetType
|| Implements(item.Action.Target.GetType(), messageTargetType))
&& item.Token == token)
{
executeAction.ExecuteWithObject(message);
}
@ -405,7 +355,7 @@ namespace GalaSoft.MvvmLight.Messaging
}
}
private static void UnregisterFromLists(object recipient, Dictionary<Type, List<WeakAction>> lists)
private static void UnregisterFromLists(object recipient, Dictionary<Type, List<WeakActionAndToken>> lists)
{
if (recipient == null
|| lists == null
@ -418,9 +368,12 @@ namespace GalaSoft.MvvmLight.Messaging
{
foreach (var messageType in lists.Keys)
{
foreach (var weakAction in lists[messageType])
foreach (var item in lists[messageType])
{
if (recipient == weakAction.Target)
var weakAction = item.Action;
if (weakAction != null
&& recipient == weakAction.Target)
{
weakAction.MarkForDeletion();
}
@ -432,7 +385,7 @@ namespace GalaSoft.MvvmLight.Messaging
private static void UnregisterFromLists<TMessage>(
object recipient,
Action<TMessage> action,
Dictionary<Type, List<WeakAction>> lists)
Dictionary<Type, List<WeakActionAndToken>> lists)
{
var messageType = typeof(TMessage);
@ -446,16 +399,16 @@ namespace GalaSoft.MvvmLight.Messaging
lock (lists)
{
foreach (var weakAction in lists[messageType])
foreach (var item in lists[messageType])
{
var weakActionCasted = weakAction as WeakAction<TMessage>;
var weakActionCasted = item.Action as WeakAction<TMessage>;
if (weakActionCasted != null
&& recipient == weakActionCasted.Target
&& (action == null
|| action == weakActionCasted.Action))
{
weakAction.MarkForDeletion();
item.Action.MarkForDeletion();
}
}
}
@ -467,15 +420,19 @@ namespace GalaSoft.MvvmLight.Messaging
CleanupList(_recipientsStrictAction);
}
private void SendToTargetOrType<TMessage>(TMessage message, Type messageTargetType)
private void SendToTargetOrType<TMessage>(TMessage message, Type messageTargetType, object token)
{
var messageType = typeof(TMessage);
if (_recipientsOfSubclassesAction != null)
{
foreach (var type in _recipientsOfSubclassesAction.Keys)
// Clone to protect from people registering in a "receive message" method
// Bug correction Messaging BL0008.002
var listClone = _recipientsOfSubclassesAction.Keys.Take(_recipientsOfSubclassesAction.Count()).ToList();
foreach (var type in listClone)
{
List<WeakAction> list = null;
List<WeakActionAndToken> list = null;
if (messageType == type
|| messageType.IsSubclassOf(type)
@ -484,7 +441,7 @@ namespace GalaSoft.MvvmLight.Messaging
list = _recipientsOfSubclassesAction[type];
}
SendToList(message, list, messageTargetType);
SendToList(message, list, messageTargetType, token);
}
}
@ -493,7 +450,7 @@ namespace GalaSoft.MvvmLight.Messaging
if (_recipientsStrictAction.ContainsKey(messageType))
{
var list = _recipientsStrictAction[messageType];
SendToList(message, list, messageTargetType);
SendToList(message, list, messageTargetType, token);
}
}

Просмотреть файл

@ -0,0 +1,76 @@
// ****************************************************************************
// <copyright file="NotificationMessage.cs" company="GalaSoft Laurent Bugnion">
// Copyright © GalaSoft Laurent Bugnion 2009
// </copyright>
// ****************************************************************************
// <author>Laurent Bugnion</author>
// <email>laurent@galasoft.ch</email>
// <date>21.4.2009</date>
// <project>GalaSoft.MvvmLight.Messaging</project>
// <web>http://www.galasoft.ch</web>
// <license>
// See license.txt in this project or http://www.galasoft.ch/license_MIT.txt
// </license>
// ****************************************************************************
////using GalaSoft.Utilities.Attributes;
namespace GalaSoft.MvvmLight.Messaging
{
/// <summary>
/// Passes a string message (Notification) to a recipient.
/// <para>Typically, notifications are defined as unique strings in a static class. To define
/// a unique string, you can use Guid.NewGuid().ToString() or any other unique
/// identifier.</para>
/// </summary>
////[ClassInfo(typeof(Messenger))]
public class NotificationMessage : MessageBase
{
/// <summary>
/// Initializes a new instance of the NotificationMessage class.
/// </summary>
/// <param name="notification">A string containing any arbitrary message to be
/// passed to recipient(s)</param>
public NotificationMessage(string notification)
{
Notification = notification;
}
/// <summary>
/// Initializes a new instance of the NotificationMessage class.
/// </summary>
/// <param name="sender">The message's sender.</param>
/// <param name="notification">A string containing any arbitrary message to be
/// passed to recipient(s)</param>
public NotificationMessage(object sender, string notification)
: base(sender)
{
Notification = notification;
}
/// <summary>
/// Initializes a new instance of the NotificationMessage class.
/// </summary>
/// <param name="sender">The message's sender.</param>
/// <param name="target">The message's intended target. This parameter can be used
/// to give an indication as to whom the message was intended for. Of course
/// this is only an indication, amd may be null.</param>
/// <param name="notification">A string containing any arbitrary message to be
/// passed to recipient(s)</param>
public NotificationMessage(object sender, object target, string notification)
: base(sender, target)
{
Notification = notification;
}
/// <summary>
/// Gets a string containing any arbitrary message to be
/// passed to recipient(s).
/// </summary>
public string Notification
{
get;
private set;
}
}
}

Просмотреть файл

@ -0,0 +1,52 @@
using System;
////using GalaSoft.Utilities.Attributes;
namespace GalaSoft.MvvmLight.Messaging
{
public class NotificationMessageAction : NotificationMessageWithCallback
{
public NotificationMessageAction(string notification, Action callback)
: base(notification, callback)
{
}
public NotificationMessageAction(object sender, string notification, Action callback)
: base(sender, notification, callback)
{
}
public NotificationMessageAction(object sender, object target, string notification, Action callback)
: base(sender, target, notification, callback)
{
}
public void Execute()
{
base.Execute();
}
}
public class NotificationMessageAction<TCallbackParameter> : NotificationMessageWithCallback
{
public NotificationMessageAction(string notification, Action<TCallbackParameter> callback)
: base(notification, callback)
{
}
public NotificationMessageAction(object sender, string notification, Action<TCallbackParameter> callback)
: base(sender, notification, callback)
{
}
public NotificationMessageAction(object sender, object target, string notification, Action<TCallbackParameter> callback)
: base(sender, target, notification, callback)
{
}
public void Execute(TCallbackParameter parameter)
{
base.Execute(parameter);
}
}
}

Просмотреть файл

@ -0,0 +1,52 @@
using System;
////using GalaSoft.Utilities.Attributes;
namespace GalaSoft.MvvmLight.Messaging
{
public class NotificationMessageFunc<TResult> : NotificationMessageWithCallback
{
public NotificationMessageFunc(string notification, Func<TResult> callback)
: base(notification, callback)
{
}
public NotificationMessageFunc(object sender, string notification, Func<TResult> callback)
: base(sender, notification, callback)
{
}
public NotificationMessageFunc(object sender, object target, string notification, Func<TResult> callback)
: base(sender, target, notification, callback)
{
}
public TResult Execute()
{
return (TResult) base.Execute();
}
}
public class NotificationMessageFunc<TCallbackParameter, TResult> : NotificationMessageWithCallback
{
public NotificationMessageFunc(string notification, Func<TCallbackParameter, TResult> callback)
: base(notification, callback)
{
}
public NotificationMessageFunc(object sender, string notification, Func<TCallbackParameter, TResult> callback)
: base(sender, notification, callback)
{
}
public NotificationMessageFunc(object sender, object target, string notification, Func<TCallbackParameter, TResult> callback)
: base(sender, target, notification, callback)
{
}
public TResult Execute(TCallbackParameter parameter)
{
return (TResult) base.Execute(parameter);
}
}
}

Просмотреть файл

@ -0,0 +1,78 @@
// ****************************************************************************
// <copyright file="NotificationMessageGeneric.cs" company="GalaSoft Laurent Bugnion">
// Copyright © GalaSoft Laurent Bugnion 2009
// </copyright>
// ****************************************************************************
// <author>Laurent Bugnion</author>
// <email>laurent@galasoft.ch</email>
// <date>13.4.2009</date>
// <project>GalaSoft.MvvmLight.Messaging</project>
// <web>http://www.galasoft.ch</web>
// <license>
// See license.txt in this project or http://www.galasoft.ch/license_MIT.txt
// </license>
// ****************************************************************************
////using GalaSoft.Utilities.Attributes;
namespace GalaSoft.MvvmLight.Messaging
{
/// <summary>
/// Passes a string message (Notification) and a generic value (Content) to a recipient.
/// </summary>
/// <typeparam name="T">The type of the Content property.</typeparam>
////[ClassInfo(typeof(Messenger))]
public class NotificationMessage<T> : GenericMessage<T>
{
/// <summary>
/// Initializes a new instance of the NotificationMessage class.
/// </summary>
/// <param name="content">A value to be passed to recipient(s).</param>
/// <param name="command">A string containing any arbitrary message to be
/// passed to recipient(s)</param>
public NotificationMessage(T content, string notification)
: base(content)
{
Notification = notification;
}
/// <summary>
/// Initializes a new instance of the NotificationMessage class.
/// </summary>
/// <param name="sender">The message's sender.</param>
/// <param name="content">A value to be passed to recipient(s).</param>
/// <param name="notification">A string containing any arbitrary message to be
/// passed to recipient(s)</param>
public NotificationMessage(object sender, T content, string notification)
: base(sender, content)
{
Notification = notification;
}
/// <summary>
/// Initializes a new instance of the NotificationMessage class.
/// </summary>
/// <param name="sender">The message's sender.</param>
/// <param name="target">The message's intended target. This parameter can be used
/// to give an indication as to whom the message was intended for. Of course
/// this is only an indication, amd may be null.</param>
/// <param name="content">A value to be passed to recipient(s).</param>
/// <param name="notification">A string containing any arbitrary message to be
/// passed to recipient(s)</param>
public NotificationMessage(object sender, object target, T content, string notification)
: base(sender, target, content)
{
Notification = notification;
}
/// <summary>
/// Gets a string containing any arbitrary message to be
/// passed to recipient(s).
/// </summary>
public string Notification
{
get;
private set;
}
}
}

Просмотреть файл

@ -0,0 +1,45 @@
using System;
////using GalaSoft.Utilities.Attributes;
namespace GalaSoft.MvvmLight.Messaging
{
public class NotificationMessageWithCallback : NotificationMessage
{
private readonly Delegate _callback;
public NotificationMessageWithCallback(string notification, Delegate callback)
: base(notification)
{
CheckCallback(callback);
_callback = callback;
}
private static void CheckCallback(Delegate callback)
{
if (callback == null)
{
throw new ArgumentNullException("callback", "Callback may not be null");
}
}
public NotificationMessageWithCallback(object sender, string notification, Delegate callback)
: base(sender, notification)
{
CheckCallback(callback);
_callback = callback;
}
public NotificationMessageWithCallback(object sender, object target, string notification, Delegate callback)
: base(sender, target, notification)
{
CheckCallback(callback);
_callback = callback;
}
public virtual object Execute(params object[] arguments)
{
return _callback.DynamicInvoke(arguments);
}
}
}

Просмотреть файл

@ -29,18 +29,6 @@ namespace GalaSoft.MvvmLight.Messaging
////[ClassInfo(typeof(Messenger))]
public class PropertyChangedMessage<T> : PropertyChangedMessageBase
{
/// <summary>
/// Initializes a new instance of the <see cref="PropertyChangedMessage{T}" /> class.
/// </summary>
/// <param name="sender">The message's sender.</param>
/// <param name="content">The property's value after the change occurred.</param>
/// <param name="propertyName">The name of the property that changed.</param>
[Obsolete("Please use a constructor with oldValue and newValue instead.")]
public PropertyChangedMessage(object sender, T content, string propertyName)
: this(sender, default(T), content, propertyName)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="PropertyChangedMessage{T}" /> class.
/// </summary>
@ -85,18 +73,6 @@ namespace GalaSoft.MvvmLight.Messaging
NewValue = newValue;
}
/// <summary>
/// Gets the value that the property has after the change.
/// </summary>
[Obsolete("Please use the NewValue property instead.")]
public T Content
{
get
{
return NewValue;
}
}
/// <summary>
/// Gets the value that the property has after the change.
/// </summary>

Просмотреть файл

@ -22,6 +22,7 @@ namespace GalaSoft.MvvmLight.Messaging
/// class allows a recipient to register for all PropertyChangedMessages without
/// having to specify the type T.
/// </summary>
////[ClassInfo(typeof(Messenger))]
public abstract class PropertyChangedMessageBase : MessageBase
{
/// <summary>

Просмотреть файл

@ -11,7 +11,7 @@
// <license>
// See license.txt in this project or http://www.galasoft.ch/license_MIT.txt
// </license>
// <LastBaseLevel>BL0008</LastBaseLevel>
// <LastBaseLevel>BL0009</LastBaseLevel>
// ****************************************************************************
using System;
@ -47,8 +47,8 @@ using System.Runtime.InteropServices;
// Minor Version
// Build Number
// Revision
[assembly: AssemblyVersion("2.0.0.0")]
////[assembly: AssemblyFileVersion("2.0.0.0")]
////[assembly: AssemblyVersion("2.0.0.*")]
[assembly: AssemblyFileVersion("3.0.0.0/alpha1")]
// FxCop
[module: SuppressMessage("Microsoft.Naming",

Просмотреть файл

@ -11,7 +11,7 @@
// <license>
// See license.txt in this project or http://www.galasoft.ch/license_MIT.txt
// </license>
// <LastBaseLevel>BL0006</LastBaseLevel>
// <LastBaseLevel>BL0007</LastBaseLevel>
// ****************************************************************************
using System;
@ -29,8 +29,8 @@ namespace GalaSoft.MvvmLight
/// A base class for the ViewModel classes in the MVVM pattern.
/// </summary>
//// [ClassInfo(typeof(ViewModelBase),
//// VersionString = "2.0.0.0",
//// DateString = "200909281609",
//// VersionString = "3.0.0.0/BL0007",
//// DateString = "200910251522",
//// Description = "A base class for the ViewModel classes in the MVVM pattern.",
//// UrlContacts = "http://www.galasoft.ch/contact_en.html",
//// Email = "laurent@galasoft.ch")]
@ -52,7 +52,7 @@ namespace GalaSoft.MvvmLight
/// <param name="messenger">An instance of a <see cref="Messenger" /> used to broadcast
/// messages to other objects. If null, this class will attempt
/// to broadcast using the Messenger's default instance.</param>
protected ViewModelBase(Messenger messenger)
protected ViewModelBase(IMessenger messenger)
{
MessengerInstance = messenger;
}
@ -115,11 +115,11 @@ namespace GalaSoft.MvvmLight
}
/// <summary>
/// Gets or sets an instance of a <see cref="Messenger" /> used to broadcast
/// Gets or sets an instance of a <see cref="IMessenger" /> used to broadcast
/// messages to other objects. If null, this class will attempt
/// to broadcast using the Messenger's default instance.
/// </summary>
protected Messenger MessengerInstance
protected IMessenger MessengerInstance
{
get;
set;
@ -150,16 +150,10 @@ namespace GalaSoft.MvvmLight
if (MessengerInstance != null)
{
MessengerInstance.Send(message);
// OLD
MessengerInstance.Broadcast(message);
}
else
{
Messenger.Default.Send(message);
// OLD
Messenger.Default.Broadcast(message);
}
}
@ -199,33 +193,6 @@ namespace GalaSoft.MvvmLight
}
}
/// <summary>
/// Raises the PropertyChanged event if needed, and broadcasts a
/// PropertyChangedMessage using the Messenger instance (or the
/// static default instance if no Messenger instance is available).
/// This method is obsolete and should be replaced by a call to
/// RaisePropertyChanged(string, T, T, bool). Calling
/// this obsolete method will use default(T) as the OldValue
/// of the PropertyChangedMessage.
/// </summary>
/// <typeparam name="T">The type of the property that changed.</typeparam>
/// <param name="propertyName">The name of the property that changed.</param>
/// <param name="newValue">The property's value after the change occurred.</param>
/// <param name="broadcast">If true, a PropertyChangedMessage will
/// be broadcasted. If false, only the event will be raised.</param>
[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate",
Justification = "This cannot be an event")]
[Obsolete("Use the method RaisePropertyChanged<T>(string, T, T, bool) instead.")]
protected virtual void RaisePropertyChanged<T>(string propertyName, T newValue, bool broadcast)
{
RaisePropertyChanged(propertyName);
if (broadcast)
{
Broadcast(default(T), newValue, propertyName);
}
}
/// <summary>
/// Raises the PropertyChanged event if needed.
/// </summary>