diff --git a/ReactiveXaml.Sample/AddPersonDialog.xaml.cs b/ReactiveXaml.Sample/AddPersonDialog.xaml.cs index 07464eac87..7e3b033288 100644 --- a/ReactiveXaml.Sample/AddPersonDialog.xaml.cs +++ b/ReactiveXaml.Sample/AddPersonDialog.xaml.cs @@ -185,6 +185,7 @@ protected void setupCommands() results.Subscribe(s => Person.Image = new BitmapImage(new Uri(s)), ex => this.Log().Error("Oh no!", ex)); + /* COOLSTUFF: The Reactive Extensions * * Reactive Extensions (Rx) allow us to take existing IObservables @@ -198,9 +199,9 @@ protected void setupCommands() * that we change a property since it's a ReactiveObservableObject. */ - _SpinnerVisibility = new ObservableAsPropertyHelper( + _SpinnerVisibility = this.ObservableToProperty( SetImageViaFlickr.CanExecuteObservable.Select(x => x ? Visibility.Collapsed : Visibility.Visible), - _ => RaisePropertyChanged("SpinnerVisibility"), Visibility.Collapsed); + x => x.SpinnerVisibility); OkCommand = new ReactiveCommand( SetImageViaFlickr.CanExecuteObservable.CombineLatest(Person.Select(x => Person.IsValid()), diff --git a/ReactiveXaml.Sample/App.xaml.cs b/ReactiveXaml.Sample/App.xaml.cs index 3f9627691e..40dfc3530b 100644 --- a/ReactiveXaml.Sample/App.xaml.cs +++ b/ReactiveXaml.Sample/App.xaml.cs @@ -88,11 +88,6 @@ public AppViewModel() setupCommands(); } - - /* - * Dependency Properties - */ - PersonEntry _SelectedPerson; public PersonEntry SelectedPerson { get { return _SelectedPerson; } @@ -120,14 +115,11 @@ public AppViewModel() * with the host application. */ - // FIXME: This syntax sucks, is there any way to hide it? - ReactiveCommand _AddPerson; [Export("AddPerson", typeof(IReactiveCommand))] - public ReactiveCommand AddPerson { get { return _AddPerson; } } + public ReactiveCommand AddPerson { get; protected set; } - ReactiveCommand _RemovePerson; [Export("RemovePerson", typeof(IReactiveCommand))] - public ReactiveCommand RemovePerson { get { return _RemovePerson; } } + public ReactiveCommand RemovePerson { get; protected set; } /* COOLSTUFF: Setting up Commands @@ -150,7 +142,7 @@ public AppViewModel() protected void setupCommands() { - _AddPerson = new ReactiveCommand(item => { + AddPerson = new ReactiveCommand(item => { var to_add = (item as PersonEntry) ?? addPersonDialog.Prompt(this, null); if (to_add == null) @@ -162,7 +154,7 @@ protected void setupCommands() People.Add(to_add); }); - _RemovePerson = new ReactiveCommand(param => (param ?? SelectedPerson) != null && People.Count > 0, item => { + RemovePerson = new ReactiveCommand(param => (param ?? SelectedPerson) != null && People.Count > 0, item => { var to_remove = (PersonEntry)item ?? SelectedPerson; this.Log().DebugFormat("Removing '{0}'", to_remove.Name); People.Remove(to_remove); diff --git a/ReactiveXaml/ObservableAsPropertyHelper.cs b/ReactiveXaml/ObservableAsPropertyHelper.cs index 87217da637..5e6bf3de8b 100644 --- a/ReactiveXaml/ObservableAsPropertyHelper.cs +++ b/ReactiveXaml/ObservableAsPropertyHelper.cs @@ -5,6 +5,7 @@ using System.Concurrency; using System.Windows.Threading; using System.Diagnostics.Contracts; +using System.Linq.Expressions; namespace ReactiveXaml { @@ -21,8 +22,8 @@ public ObservableAsPropertyHelper(IObservable observable, Action on_change scheduler = scheduler ?? RxApp.DeferredScheduler; lastValue = initial_value; - observable.ObserveOn(scheduler) - .DistinctUntilChanged() + observable.DistinctUntilChanged() + .ObserveOn(scheduler) .Subscribe(x => { this.Log().DebugFormat("Property helper {0:X} changed", this.GetHashCode()); lastValue = x; @@ -40,4 +41,23 @@ public ObservableAsPropertyHelper(IObservable observable, Action on_change } } } + + public static class OAPHCreationHelperMixin + { + public static ObservableAsPropertyHelper ObservableToProperty( + this TObj This, + IObservable Observable, + Expression> Property, + TRet InitialValue = default(TRet), + IScheduler Scheduler = null) + where TObj : ReactiveObject + { + Contract.Requires(This != null); + Contract.Requires(Observable != null); + Contract.Requires(Property != null); + + string prop_name = RxApp.expressionToPropertyName(Property); + return new ObservableAsPropertyHelper(Observable, _ => This.RaisePropertyChanged(prop_name), InitialValue, Scheduler); + } + } } \ No newline at end of file