Created
November 17, 2012 19:49
-
-
Save patroza/4099399 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestViewModel : ReactiveObject | |
{ | |
const int _intVal = 1; | |
public int IntVal { get { return _intVal; } } | |
ObservableAsPropertyHelper<int> _MyReactiveVal; | |
public int MyReactiveVal | |
{ | |
get { return _MyReactiveVal.Value; } | |
} | |
ObservableAsPropertyHelper<int> _MyReactiveVal2; | |
public int MyReactiveVal2 | |
{ | |
get { return _MyReactiveVal2.Value; } | |
} | |
public TestViewModel() | |
{ | |
// Test 1 - No initial value set explicitly | |
// This will raise a propertyChanged event in the 'future', IMO shouldn't | |
this.WhenAny(x => x.IntVal, x => x.Value) | |
.Select(x => x) | |
.ToProperty(this, x => x.MyReactiveVal); | |
// Expecting MyReactiveVal == 1 here, but getting 0 instead | |
if (MyReactiveVal != IntVal) | |
ShowMessage(String.Format("MyReactiveVal {0} != Intval {1}", MyReactiveVal, IntVal)); | |
Task.Factory.StartNew( | |
() => { | |
// Here it works - we are getting 1 now | |
Thread.Sleep(5000); | |
if (MyReactiveVal != IntVal) | |
ShowMessage(String.Format("MyReactiveVal {0} != Intval {1}. I shouldn't be raised", MyReactiveVal, IntVal)); | |
}); | |
// Test 2 - An initial value explicitly set | |
// This will raise a propertyChanged event in the 'future', IMO definitely shouldn't, because explicitly set initialvalue is equal to calculatedvalue | |
this.WhenAny(x => x.IntVal, x => x.Value) | |
.Select(x => x) | |
.ToProperty(this, x => x.MyReactiveVal2, IntVal); | |
// Expecting MyReactiveVal2 == 1 here, and indeed getting 1 because of initialvalue | |
if (MyReactiveVal2 != IntVal) ShowMessage(String.Format("MyReactiveVal2 {0} != Intval {1}", MyReactiveVal2, IntVal)); | |
// However, now even though both the initial and calculated value are equal, | |
// a PropertyChanged event will be raised. And not once, but twice! | |
// Attach to PropertyChanged events and show MessageBox when raised | |
PropertyChanged += | |
(sender, args) => ShowMessage(args.PropertyName + ": I shouldn't be raised! Especially not the second round"); | |
} | |
private void ShowMessage(string message) | |
{ | |
Dispatcher.CurrentDispatcher.BeginInvoke( | |
() => MessageBox.Show(message)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment