NOTE: This can be a breaking change if you were overriding RaisePropertyChanged
in your code before.

Issue 7564: The RaisePropertyChanged Overloads Contain Duplicate Code.
This is solved now in ViewModelBase and ObservableObject. All calls are routed
through ObservableObject.RaisePropertyChanged(string). If you override this method only in your VMs,
it will always be called if you call any of the Set methods, RaisePropertyChanged(string)
or RaisePropertyChanged(Expression).
This commit is contained in:
Laurent Bugnion 2016-01-17 21:18:56 +01:00
Родитель 7d8ac1ec1e
Коммит d3a603de38
16 изменённых файлов: 743 добавлений и 27 удалений

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

@ -129,7 +129,7 @@ namespace GalaSoft.MvvmLight
"Microsoft.Design",
"CA1030:UseEventsWhereAppropriate",
Justification = "This cannot be an event")]
protected virtual void RaisePropertyChanging(
public virtual void RaisePropertyChanging(
[CallerMemberName] string propertyName = null)
#else
/// <summary>
@ -144,7 +144,7 @@ namespace GalaSoft.MvvmLight
"Microsoft.Design",
"CA1030:UseEventsWhereAppropriate",
Justification = "This cannot be an event")]
protected virtual void RaisePropertyChanging(
public virtual void RaisePropertyChanging(
string propertyName)
#endif
{
@ -171,7 +171,7 @@ namespace GalaSoft.MvvmLight
"Microsoft.Design",
"CA1030:UseEventsWhereAppropriate",
Justification = "This cannot be an event")]
protected virtual void RaisePropertyChanged(
public virtual void RaisePropertyChanged(
[CallerMemberName] string propertyName = null)
#else
/// <summary>
@ -186,7 +186,7 @@ namespace GalaSoft.MvvmLight
"Microsoft.Design",
"CA1030:UseEventsWhereAppropriate",
Justification = "This cannot be an event")]
protected virtual void RaisePropertyChanged(
public virtual void RaisePropertyChanged(
string propertyName)
#endif
{
@ -215,7 +215,7 @@ namespace GalaSoft.MvvmLight
"Microsoft.Design",
"CA1006:GenericMethodsShouldProvideTypeParameter",
Justification = "This syntax is more convenient than other alternatives.")]
protected virtual void RaisePropertyChanging<T>(Expression<Func<T>> propertyExpression)
public virtual void RaisePropertyChanging<T>(Expression<Func<T>> propertyExpression)
{
var handler = PropertyChanging;
if (handler != null)
@ -241,13 +241,19 @@ namespace GalaSoft.MvvmLight
"Microsoft.Design",
"CA1006:GenericMethodsShouldProvideTypeParameter",
Justification = "This syntax is more convenient than other alternatives.")]
protected virtual void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
public virtual void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
{
var handler = PropertyChanged;
if (handler != null)
{
var propertyName = GetPropertyName(propertyExpression);
handler(this, new PropertyChangedEventArgs(propertyName));
if (!string.IsNullOrEmpty(propertyName))
{
// ReSharper disable once ExplicitCallerInfoArgument
RaisePropertyChanged(propertyName);
}
}
}

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

@ -122,7 +122,6 @@ namespace GalaSoft.MvvmLight
#elif NETFX_CORE
_isInDesignMode = DesignMode.DesignModeEnabled;
#elif XAMARIN
// TODO XAMARIN Is there such a thing as design mode? How to detect it?
_isInDesignMode = false;
#else
var prop = DesignerProperties.IsInDesignModeProperty;
@ -169,8 +168,6 @@ namespace GalaSoft.MvvmLight
return false;
}
private static bool IsInDesignModeSilverlight()
{
try
@ -368,7 +365,7 @@ namespace GalaSoft.MvvmLight
SuppressMessage(
"Microsoft.Design", "CA1030:UseEventsWhereAppropriate",
Justification = "This cannot be an event")]
protected virtual void RaisePropertyChanged<T>(
public virtual void RaisePropertyChanged<T>(
#if CMNATTR
[CallerMemberName] string propertyName = null,
#else
@ -416,26 +413,17 @@ namespace GalaSoft.MvvmLight
"Microsoft.Design",
"CA1006:GenericMethodsShouldProvideTypeParameter",
Justification = "This syntax is more convenient than the alternatives.")]
protected virtual void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression, T oldValue, T newValue, bool broadcast)
public virtual void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression, T oldValue, T newValue, bool broadcast)
{
var handler = PropertyChangedHandler;
if (handler != null
|| broadcast)
{
var propertyName = GetPropertyName(propertyExpression);
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
RaisePropertyChanged(propertyExpression);
if (broadcast)
{
// Unfortunately I don't see a reliable way to not call GetPropertyName twice.
var propertyName = GetPropertyName(propertyExpression);
Broadcast(oldValue, newValue, propertyName);
}
}
}
/// <summary>
/// Assigns a new value to the property. Then, raises the

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

@ -15,7 +15,7 @@
<AndroidResgenFile>Resources\Resource.Designer.cs</AndroidResgenFile>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<AndroidUseLatestPlatformSdk>True</AndroidUseLatestPlatformSdk>
<TargetFrameworkVersion>v5.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v6.0</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>

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

@ -230,6 +230,9 @@
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test %28PNET45%29\ObservableObjectPropertyChangedTest.cs">
<Link>ObservableObjectPropertyChangedTest.cs</Link>
</Compile>
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test %28PNET45%29\ObservableObjectTest.cs">
<Link>ObservableObjectTest.cs</Link>
</Compile>
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test %28PNET45%29\Properties\AssemblyInfo.cs">
<Link>Properties\AssemblyInfo.cs</Link>
</Compile>
@ -275,15 +278,24 @@
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test %28PNET45%29\Threading\DispatcherHelperTest.cs">
<Link>Threading\DispatcherHelperTest.cs</Link>
</Compile>
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModelBaseAndPropertyChangedTest.cs">
<Link>ViewModelBaseAndPropertyChangedTest.cs</Link>
</Compile>
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModelBaseTest.cs">
<Link>ViewModelBaseTest.cs</Link>
</Compile>
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestClassWithObservableObject.cs">
<Link>ViewModel\TestClassWithObservableObject.cs</Link>
</Compile>
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestObservableObject.cs">
<Link>ViewModel\TestObservableObject.cs</Link>
</Compile>
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestViewModel.cs">
<Link>ViewModel\TestViewModel.cs</Link>
</Compile>
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestViewModelForPropertyChanged.cs">
<Link>ViewModel\TestViewModelForPropertyChanged.cs</Link>
</Compile>
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestViewModelNoMagicString.cs">
<Link>ViewModel\TestViewModelNoMagicString.cs</Link>
</Compile>

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

@ -221,6 +221,9 @@
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test %28PNET45%29\ObservableObjectPropertyChangedTest.cs">
<Link>ObservableObjectPropertyChangedTest.cs</Link>
</Compile>
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test %28PNET45%29\ObservableObjectTest.cs">
<Link>ObservableObjectTest.cs</Link>
</Compile>
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test %28PNET45%29\Properties\AssemblyInfo.cs">
<Link>Properties\AssemblyInfo.cs</Link>
</Compile>
@ -266,15 +269,24 @@
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test %28PNET45%29\Threading\DispatcherHelperTest.cs">
<Link>Threading\DispatcherHelperTest.cs</Link>
</Compile>
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModelBaseAndPropertyChangedTest.cs">
<Link>ViewModelBaseAndPropertyChangedTest.cs</Link>
</Compile>
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModelBaseTest.cs">
<Link>ViewModelBaseTest.cs</Link>
</Compile>
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestClassWithObservableObject.cs">
<Link>ViewModel\TestClassWithObservableObject.cs</Link>
</Compile>
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestObservableObject.cs">
<Link>ViewModel\TestObservableObject.cs</Link>
</Compile>
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestViewModel.cs">
<Link>ViewModel\TestViewModel.cs</Link>
</Compile>
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestViewModelForPropertyChanged.cs">
<Link>ViewModel\TestViewModelForPropertyChanged.cs</Link>
</Compile>
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestViewModelNoMagicString.cs">
<Link>ViewModel\TestViewModelNoMagicString.cs</Link>
</Compile>

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

@ -95,9 +95,18 @@
<Reference Include="System.Windows.Browser" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test %28PNET45%29\ObservableObjectTest.cs">
<Link>ObservableObjectTest.cs</Link>
</Compile>
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test %28PNET45%29\Stubs\TestClass1.cs">
<Link>Stubs\TestClass1.cs</Link>
</Compile>
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestObservableObject.cs">
<Link>ViewModels\TestObservableObject.cs</Link>
</Compile>
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestViewModelForPropertyChanged.cs">
<Link>ViewModels\TestViewModelForPropertyChanged.cs</Link>
</Compile>
<Compile Include="..\PCL\GalaSoft.MvvmLight.Test (PNET45)\Command\EventArgsConverter.cs">
<Link>Command\EventArgsConverter.cs</Link>
</Compile>

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

@ -112,6 +112,7 @@
<Compile Include="Messaging\TestMessageBase.cs" />
<Compile Include="Messaging\TestMessageImpl.cs" />
<Compile Include="ObservableObjectPropertyChangedTest.cs" />
<Compile Include="ObservableObjectTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Stubs\ITestClass.cs" />
<Compile Include="Stubs\TestBaseClass.cs" />
@ -127,9 +128,12 @@
<Compile Include="Stubs\TestClassForCreationTime.cs" />
<Compile Include="Stubs\TestClassWithMultiConstructors.cs" />
<Compile Include="Threading\DispatcherHelperTest.cs" />
<Compile Include="ViewModelBaseAndPropertyChangedTest.cs" />
<Compile Include="ViewModelBaseTest.cs" />
<Compile Include="ViewModel\TestClassWithObservableObject.cs" />
<Compile Include="ViewModel\TestObservableObject.cs" />
<Compile Include="ViewModel\TestViewModel.cs" />
<Compile Include="ViewModel\TestViewModelForPropertyChanged.cs" />
<Compile Include="ViewModel\TestViewModelNoMagicString.cs" />
<Compile Include="ViewModel\ViewModelStub.cs" />
</ItemGroup>

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

@ -0,0 +1,105 @@
using System.Diagnostics.CodeAnalysis;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Test.ViewModel;
#if NETFX_CORE || (WINDOWS_PHONE && !WINDOWS_PHONE7)
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endif
namespace GalaSoft.MvvmLight.Test
{
[TestClass]
[SuppressMessage("ReSharper", "InconsistentNaming")]
public class ObservableObjectTest
{
[TestMethod]
public void RaisePropertyChanged_WithString_ShouldCallRaisePropertyChangedWithStringOnly()
{
var testObject = new TestObservableObject();
var eventWasRaised = false;
testObject.PropertyChanged += (s, e) =>
{
if (e.PropertyName == "BoolPropertyWithString")
{
eventWasRaised = true;
}
};
Assert.IsFalse(testObject.BoolPropertyWithString);
testObject.BoolPropertyWithString = true;
Assert.IsTrue(testObject.BoolPropertyWithString);
Assert.IsTrue(eventWasRaised);
Assert.IsTrue(testObject.RaisePropertyChangedWithPropertyNameWasCalled);
Assert.IsFalse(testObject.RaisePropertyChangedWithExpressionWasCalled);
}
[TestMethod]
public void RaisePropertyChanged_WithExpression_ShouldCallRaisePropertyChangedWithStringAndWithExpression()
{
var testObject = new TestObservableObject();
var eventWasRaised = false;
testObject.PropertyChanged += (s, e) =>
{
if (e.PropertyName == "BoolPropertyWithExpression")
{
eventWasRaised = true;
}
};
Assert.IsFalse(testObject.BoolPropertyWithExpression);
testObject.BoolPropertyWithExpression = true;
Assert.IsTrue(testObject.BoolPropertyWithExpression);
Assert.IsTrue(eventWasRaised);
Assert.IsTrue(testObject.RaisePropertyChangedWithPropertyNameWasCalled);
Assert.IsTrue(testObject.RaisePropertyChangedWithExpressionWasCalled);
}
[TestMethod]
public void RaisePropertyChanged_WithSetAndExpression_ShouldCallRaisePropertyChangedWithStringAndWithExpression()
{
var testObject = new TestObservableObject();
var eventWasRaised = false;
testObject.PropertyChanged += (s, e) =>
{
if (e.PropertyName == "BoolPropertyWithSetAndExpression")
{
eventWasRaised = true;
}
};
Assert.IsFalse(testObject.BoolPropertyWithSetAndExpression);
testObject.BoolPropertyWithSetAndExpression = true;
Assert.IsTrue(testObject.BoolPropertyWithSetAndExpression);
Assert.IsTrue(eventWasRaised);
Assert.IsTrue(testObject.RaisePropertyChangedWithPropertyNameWasCalled);
Assert.IsTrue(testObject.RaisePropertyChangedWithExpressionWasCalled);
}
[TestMethod]
public void RaisePropertyChanged_WithSetAndString_ShouldCallRaisePropertyChangedWithStringOnly()
{
var testObject = new TestObservableObject();
var eventWasRaised = false;
testObject.PropertyChanged += (s, e) =>
{
if (e.PropertyName == "BoolPropertyWithSetAndString")
{
eventWasRaised = true;
}
};
Assert.IsFalse(testObject.BoolPropertyWithSetAndString);
testObject.BoolPropertyWithSetAndString = true;
Assert.IsTrue(testObject.BoolPropertyWithSetAndString);
Assert.IsTrue(eventWasRaised);
Assert.IsTrue(testObject.RaisePropertyChangedWithPropertyNameWasCalled);
Assert.IsFalse(testObject.RaisePropertyChangedWithExpressionWasCalled);
}
}
}

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

@ -0,0 +1,104 @@
using System;
using System.Linq.Expressions;
namespace GalaSoft.MvvmLight.Test.ViewModel
{
public class TestObservableObject : ObservableObject
{
public bool RaisePropertyChangedWithExpressionWasCalled
{
get;
private set;
}
public bool RaisePropertyChangedWithPropertyNameWasCalled
{
get;
private set;
}
public override void RaisePropertyChanged(string propertyName = null)
{
// ReSharper disable once ExplicitCallerInfoArgument
base.RaisePropertyChanged(propertyName);
RaisePropertyChangedWithPropertyNameWasCalled = true;
}
public override void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
{
base.RaisePropertyChanged(propertyExpression);
RaisePropertyChangedWithExpressionWasCalled = true;
}
private bool _boolPropertyWithString;
public bool BoolPropertyWithString
{
get
{
return _boolPropertyWithString;
}
set
{
if (_boolPropertyWithString == value)
{
return;
}
_boolPropertyWithString = value;
RaisePropertyChanged("BoolPropertyWithString");
}
}
private bool _boolPropertyWithExpression;
public bool BoolPropertyWithExpression
{
get
{
return _boolPropertyWithExpression;
}
set
{
if (_boolPropertyWithExpression == value)
{
return;
}
_boolPropertyWithExpression = value;
RaisePropertyChanged(() => BoolPropertyWithExpression);
}
}
private bool _boolPropertyWithSetAndString;
public bool BoolPropertyWithSetAndString
{
get
{
return _boolPropertyWithSetAndString;
}
set
{
Set("BoolPropertyWithSetAndString", ref _boolPropertyWithSetAndString, value);
}
}
private bool _boolPropertyWithSetAndExpression;
public bool BoolPropertyWithSetAndExpression
{
get
{
return _boolPropertyWithSetAndExpression;
}
set
{
Set(() => BoolPropertyWithSetAndExpression, ref _boolPropertyWithSetAndExpression, value);
}
}
}
}

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

@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;
using GalaSoft.MvvmLight.Messaging;
namespace GalaSoft.MvvmLight.Test.ViewModel

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

@ -0,0 +1,179 @@
using System;
using System.Linq.Expressions;
namespace GalaSoft.MvvmLight.Test.ViewModel
{
public class TestViewModelForPropertyChanged : ViewModelBase
{
public bool RaisePropertyChangedWithExpressionWasCalled
{
get;
private set;
}
public bool RaisePropertyChangedWithPropertyNameWasCalled
{
get;
private set;
}
public override void RaisePropertyChanged(string propertyName = null)
{
// ReSharper disable once ExplicitCallerInfoArgument
base.RaisePropertyChanged(propertyName);
RaisePropertyChangedWithPropertyNameWasCalled = true;
}
public override void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
{
base.RaisePropertyChanged(propertyExpression);
RaisePropertyChangedWithExpressionWasCalled = true;
}
private bool _boolPropertyWithString;
public bool BoolPropertyWithString
{
get
{
return _boolPropertyWithString;
}
set
{
if (_boolPropertyWithString == value)
{
return;
}
_boolPropertyWithString = value;
RaisePropertyChanged("BoolPropertyWithString");
}
}
private bool _boolPropertyWithExpression;
public bool BoolPropertyWithExpression
{
get
{
return _boolPropertyWithExpression;
}
set
{
if (_boolPropertyWithExpression == value)
{
return;
}
_boolPropertyWithExpression = value;
RaisePropertyChanged(() => BoolPropertyWithExpression);
}
}
private bool _boolPropertyWithExpressionAndMessage;
public bool BoolPropertyWithExpressionAndMessage
{
get
{
return _boolPropertyWithExpressionAndMessage;
}
set
{
if (_boolPropertyWithExpressionAndMessage == value)
{
return;
}
var oldValue = _boolPropertyWithExpressionAndMessage;
_boolPropertyWithExpressionAndMessage = value;
RaisePropertyChanged(() => BoolPropertyWithExpressionAndMessage, oldValue, value, true);
}
}
private bool _boolPropertyWithStringAndMessage;
public bool BoolPropertyWithStringAndMessage
{
get
{
return _boolPropertyWithStringAndMessage;
}
set
{
if (_boolPropertyWithStringAndMessage == value)
{
return;
}
var oldValue = _boolPropertyWithStringAndMessage;
_boolPropertyWithStringAndMessage = value;
// ReSharper disable once RedundantArgumentDefaultValue
RaisePropertyChanged("BoolPropertyWithStringAndMessage", oldValue, value, true);
}
}
private bool _boolPropertyWithSetAndString;
public bool BoolPropertyWithSetAndString
{
get
{
return _boolPropertyWithSetAndString;
}
set
{
Set("BoolPropertyWithSetAndString", ref _boolPropertyWithSetAndString, value);
}
}
private bool _boolPropertyWithSetAndExpression;
public bool BoolPropertyWithSetAndExpression
{
get
{
return _boolPropertyWithSetAndExpression;
}
set
{
Set(() => BoolPropertyWithSetAndExpression, ref _boolPropertyWithSetAndExpression, value);
}
}
private bool _boolPropertyWithSetAndExpressionAndMessage;
public bool BoolPropertyWithSetAndExpressionAndMessage
{
get
{
return _boolPropertyWithSetAndExpressionAndMessage;
}
set
{
Set(() => BoolPropertyWithSetAndExpressionAndMessage, ref _boolPropertyWithSetAndExpressionAndMessage, value, true);
}
}
private bool _boolPropertyWithSetAndStringAndMessage;
public bool BoolPropertyWithSetAndStringAndMessage
{
get
{
return _boolPropertyWithSetAndStringAndMessage;
}
set
{
Set("BoolPropertyWithSetAndStringAndMessage", ref _boolPropertyWithSetAndStringAndMessage, value, true);
}
}
}
}

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

@ -0,0 +1,249 @@
using System.Diagnostics.CodeAnalysis;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Test.ViewModel;
#if NETFX_CORE || (WINDOWS_PHONE && !WINDOWS_PHONE7)
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endif
namespace GalaSoft.MvvmLight.Test
{
[TestClass]
[SuppressMessage("ReSharper", "InconsistentNaming")]
public class ViewModelBaseAndPropertyChangedTest
{
[TestMethod]
public void RaisePropertyChanged_WithString_ShouldCallRaisePropertyChangedWithStringOnly()
{
var testObject = new TestViewModelForPropertyChanged();
var eventWasRaised = false;
testObject.PropertyChanged += (s, e) =>
{
if (e.PropertyName == "BoolPropertyWithString")
{
eventWasRaised = true;
}
};
Assert.IsFalse(testObject.BoolPropertyWithString);
testObject.BoolPropertyWithString = true;
Assert.IsTrue(testObject.BoolPropertyWithString);
Assert.IsTrue(eventWasRaised);
Assert.IsTrue(testObject.RaisePropertyChangedWithPropertyNameWasCalled);
Assert.IsFalse(testObject.RaisePropertyChangedWithExpressionWasCalled);
}
[TestMethod]
public void RaisePropertyChanged_WithExpression_ShouldCallRaisePropertyChangedWithStringAndWithExpression()
{
var testObject = new TestViewModelForPropertyChanged();
var eventWasRaised = false;
testObject.PropertyChanged += (s, e) =>
{
if (e.PropertyName == "BoolPropertyWithExpression")
{
eventWasRaised = true;
}
};
Assert.IsFalse(testObject.BoolPropertyWithExpression);
testObject.BoolPropertyWithExpression = true;
Assert.IsTrue(testObject.BoolPropertyWithExpression);
Assert.IsTrue(eventWasRaised);
Assert.IsTrue(testObject.RaisePropertyChangedWithPropertyNameWasCalled);
Assert.IsTrue(testObject.RaisePropertyChangedWithExpressionWasCalled);
}
[TestMethod]
public void RaisePropertyChanged_WithExpressionAndMessage_ShouldCallRaisePropertyChangedWithStringAndWithExpression()
{
var testObject = new TestViewModelForPropertyChanged();
var eventWasRaised = false;
var messageWasReceived = false;
testObject.PropertyChanged += (s, e) =>
{
if (e.PropertyName == "BoolPropertyWithExpressionAndMessage")
{
eventWasRaised = true;
}
};
Messenger.Default.Register<PropertyChangedMessage<bool>>(
this,
message =>
{
if (message.PropertyName == "BoolPropertyWithExpressionAndMessage"
&& message.Sender == testObject)
{
messageWasReceived = true;
}
});
Assert.IsFalse(testObject.BoolPropertyWithExpressionAndMessage);
testObject.BoolPropertyWithExpressionAndMessage = true;
Assert.IsTrue(testObject.BoolPropertyWithExpressionAndMessage);
Assert.IsTrue(eventWasRaised);
Assert.IsTrue(messageWasReceived);
Assert.IsTrue(testObject.RaisePropertyChangedWithPropertyNameWasCalled);
Assert.IsTrue(testObject.RaisePropertyChangedWithExpressionWasCalled);
}
[TestMethod]
public void RaisePropertyChanged_WithSetAndExpression_ShouldCallRaisePropertyChangedWithStringAndWithExpression()
{
var testObject = new TestViewModelForPropertyChanged();
var eventWasRaised = false;
testObject.PropertyChanged += (s, e) =>
{
if (e.PropertyName == "BoolPropertyWithSetAndExpression")
{
eventWasRaised = true;
}
};
Assert.IsFalse(testObject.BoolPropertyWithSetAndExpression);
testObject.BoolPropertyWithSetAndExpression = true;
Assert.IsTrue(testObject.BoolPropertyWithSetAndExpression);
Assert.IsTrue(eventWasRaised);
Assert.IsTrue(testObject.RaisePropertyChangedWithPropertyNameWasCalled);
Assert.IsTrue(testObject.RaisePropertyChangedWithExpressionWasCalled);
}
[TestMethod]
public void RaisePropertyChanged_WithSetAndExpressionAndMessage_ShouldCallRaisePropertyChangedWithStringAndWithExpression()
{
var testObject = new TestViewModelForPropertyChanged();
var eventWasRaised = false;
var messageWasReceived = false;
testObject.PropertyChanged += (s, e) =>
{
if (e.PropertyName == "BoolPropertyWithSetAndExpressionAndMessage")
{
eventWasRaised = true;
}
};
Messenger.Default.Register<PropertyChangedMessage<bool>>(
this,
message =>
{
if (message.PropertyName == "BoolPropertyWithSetAndExpressionAndMessage"
&& message.Sender == testObject)
{
messageWasReceived = true;
}
});
Assert.IsFalse(testObject.BoolPropertyWithSetAndExpressionAndMessage);
testObject.BoolPropertyWithSetAndExpressionAndMessage = true;
Assert.IsTrue(testObject.BoolPropertyWithSetAndExpressionAndMessage);
Assert.IsTrue(eventWasRaised);
Assert.IsTrue(messageWasReceived);
Assert.IsTrue(testObject.RaisePropertyChangedWithPropertyNameWasCalled);
Assert.IsTrue(testObject.RaisePropertyChangedWithExpressionWasCalled);
}
[TestMethod]
public void RaisePropertyChanged_WithSetAndString_ShouldCallRaisePropertyChangedWithStringOnly()
{
var testObject = new TestViewModelForPropertyChanged();
var eventWasRaised = false;
testObject.PropertyChanged += (s, e) =>
{
if (e.PropertyName == "BoolPropertyWithSetAndString")
{
eventWasRaised = true;
}
};
Assert.IsFalse(testObject.BoolPropertyWithSetAndString);
testObject.BoolPropertyWithSetAndString = true;
Assert.IsTrue(testObject.BoolPropertyWithSetAndString);
Assert.IsTrue(eventWasRaised);
Assert.IsTrue(testObject.RaisePropertyChangedWithPropertyNameWasCalled);
Assert.IsFalse(testObject.RaisePropertyChangedWithExpressionWasCalled);
}
[TestMethod]
public void RaisePropertyChanged_WithSetAndStringAndMessage_ShouldCallRaisePropertyChangedWithStringOnly()
{
var testObject = new TestViewModelForPropertyChanged();
var eventWasRaised = false;
var messageWasReceived = false;
testObject.PropertyChanged += (s, e) =>
{
if (e.PropertyName == "BoolPropertyWithSetAndStringAndMessage")
{
eventWasRaised = true;
}
};
Messenger.Default.Register<PropertyChangedMessage<bool>>(
this,
message =>
{
if (message.PropertyName == "BoolPropertyWithSetAndStringAndMessage"
&& message.Sender == testObject)
{
messageWasReceived = true;
}
});
Assert.IsFalse(testObject.BoolPropertyWithSetAndStringAndMessage);
testObject.BoolPropertyWithSetAndStringAndMessage = true;
Assert.IsTrue(testObject.BoolPropertyWithSetAndStringAndMessage);
Assert.IsTrue(eventWasRaised);
Assert.IsTrue(messageWasReceived);
Assert.IsTrue(testObject.RaisePropertyChangedWithPropertyNameWasCalled);
Assert.IsFalse(testObject.RaisePropertyChangedWithExpressionWasCalled);
}
[TestMethod]
public void RaisePropertyChanged_WithStringAndMessage_ShouldCallRaisePropertyChangedWithStringOnly()
{
var testObject = new TestViewModelForPropertyChanged();
var eventWasRaised = false;
var messageWasReceived = false;
testObject.PropertyChanged += (s, e) =>
{
if (e.PropertyName == "BoolPropertyWithStringAndMessage")
{
eventWasRaised = true;
}
};
Messenger.Default.Register<PropertyChangedMessage<bool>>(
this,
message =>
{
if (message.PropertyName == "BoolPropertyWithStringAndMessage"
&& message.Sender == testObject)
{
messageWasReceived = true;
}
});
Assert.IsFalse(testObject.BoolPropertyWithStringAndMessage);
testObject.BoolPropertyWithStringAndMessage = true;
Assert.IsTrue(testObject.BoolPropertyWithStringAndMessage);
Assert.IsTrue(eventWasRaised);
Assert.IsTrue(messageWasReceived);
Assert.IsTrue(testObject.RaisePropertyChangedWithPropertyNameWasCalled);
Assert.IsFalse(testObject.RaisePropertyChangedWithExpressionWasCalled);
}
}
}

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

@ -260,6 +260,9 @@
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ObservableObjectPropertyChangedTest.cs">
<Link>ObservableObjectPropertyChangedTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ObservableObjectTest.cs">
<Link>ObservableObjectTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\Properties\AssemblyInfo.cs">
<Link>Properties\AssemblyInfo.cs</Link>
</Compile>
@ -305,15 +308,24 @@
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\Threading\DispatcherHelperTest.cs">
<Link>Threading\DispatcherHelperTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModelBaseAndPropertyChangedTest.cs">
<Link>ViewModelBaseAndPropertyChangedTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModelBaseTest.cs">
<Link>ViewModelBaseTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestClassWithObservableObject.cs">
<Link>ViewModel\TestClassWithObservableObject.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestObservableObject.cs">
<Link>ViewModel\TestObservableObject.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestViewModel.cs">
<Link>ViewModel\TestViewModel.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestViewModelForPropertyChanged.cs">
<Link>ViewModel\TestViewModelForPropertyChanged.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestViewModelNoMagicString.cs">
<Link>ViewModel\TestViewModelNoMagicString.cs</Link>
</Compile>

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

@ -220,6 +220,9 @@
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ObservableObjectPropertyChangedTest.cs">
<Link>ObservableObjectPropertyChangedTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ObservableObjectTest.cs">
<Link>ObservableObjectTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\Properties\AssemblyInfo.cs">
<Link>Properties\AssemblyInfo.cs</Link>
</Compile>
@ -265,15 +268,24 @@
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\Threading\DispatcherHelperTest.cs">
<Link>Threading\DispatcherHelperTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModelBaseAndPropertyChangedTest.cs">
<Link>ViewModelBaseAndPropertyChangedTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModelBaseTest.cs">
<Link>ViewModelBaseTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestClassWithObservableObject.cs">
<Link>ViewModel\TestClassWithObservableObject.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestObservableObject.cs">
<Link>ViewModel\TestObservableObject.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestViewModel.cs">
<Link>ViewModel\TestViewModel.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestViewModelForPropertyChanged.cs">
<Link>ViewModel\TestViewModelForPropertyChanged.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestViewModelNoMagicString.cs">
<Link>ViewModel\TestViewModelNoMagicString.cs</Link>
</Compile>

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

@ -229,6 +229,9 @@
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ObservableObjectPropertyChangedTest.cs">
<Link>ObservableObjectPropertyChangedTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ObservableObjectTest.cs">
<Link>ObservableObjectTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\Properties\AssemblyInfo.cs">
<Link>Properties\AssemblyInfo.cs</Link>
</Compile>
@ -274,15 +277,24 @@
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\Threading\DispatcherHelperTest.cs">
<Link>Threading\DispatcherHelperTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModelBaseAndPropertyChangedTest.cs">
<Link>ViewModelBaseAndPropertyChangedTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModelBaseTest.cs">
<Link>ViewModelBaseTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestClassWithObservableObject.cs">
<Link>ViewModel\TestClassWithObservableObject.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestObservableObject.cs">
<Link>ViewModel\TestObservableObject.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestViewModel.cs">
<Link>ViewModel\TestViewModel.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestViewModelForPropertyChanged.cs">
<Link>ViewModel\TestViewModelForPropertyChanged.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestViewModelNoMagicString.cs">
<Link>ViewModel\TestViewModelNoMagicString.cs</Link>
</Compile>

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

@ -230,6 +230,9 @@
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ObservableObjectPropertyChangedTest.cs">
<Link>ObservableObjectPropertyChangedTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ObservableObjectTest.cs">
<Link>ObservableObjectTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\Properties\AssemblyInfo.cs">
<Link>Properties\AssemblyInfo.cs</Link>
</Compile>
@ -281,9 +284,15 @@
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestClassWithObservableObject.cs">
<Link>ViewModel\TestClassWithObservableObject.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestObservableObject.cs">
<Link>ViewModel\TestObservableObject.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestViewModel.cs">
<Link>ViewModel\TestViewModel.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestViewModelForPropertyChanged.cs">
<Link>ViewModel\TestViewModelForPropertyChanged.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28PNET45%29\ViewModel\TestViewModelNoMagicString.cs">
<Link>ViewModel\TestViewModelNoMagicString.cs</Link>
</Compile>
@ -378,7 +387,9 @@
<Reference Include="Microsoft.Practices.ServiceLocation">
<HintPath>..\..\..\packages\CommonServiceLocator.1.3\lib\portable-net4+sl5+netcore45+wpa81+wp8\Microsoft.Practices.ServiceLocation.dll</HintPath>
</Reference>
<Reference Include="System.Windows.Interactivity, Version=3.9.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="System.Windows.Interactivity">
<HintPath>..\..\..\_Binaries\Release\WPSL81\System.Windows.Interactivity.dll</HintPath>
</Reference>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\$(TargetFrameworkIdentifier)\$(TargetFrameworkVersion)\Microsoft.$(TargetFrameworkIdentifier).$(TargetFrameworkVersion).Overrides.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\$(TargetFrameworkIdentifier)\$(TargetFrameworkVersion)\Microsoft.$(TargetFrameworkIdentifier).CSharp.targets" />