Skip to content

Commit

Permalink
Fix thread related InvalidOperationException's
Browse files Browse the repository at this point in the history
-Could not get RxApp.MainThreadScheduler to work with GoToElementState
so added a manual call to Dispatcher.BeginInvoke
- Adding observe on MainThreadScheduler fixed an exception related to
setting reminder opacity property
  • Loading branch information
Dave McKeown authored and Dave McKeown committed Dec 16, 2013
1 parent 8d69635 commit 92cb04f
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions SaveAllTheTime/Views/CommitHintView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@

namespace SaveAllTheTime.Views
{
using System.Reactive.Concurrency;

/// <summary>
/// Interaction logic for CommitHintView.xaml
/// </summary>
public partial class CommitHintView : UserControl, IViewFor<CommitHintViewModel>
public partial class CommitHintView : UserControl, IViewFor<CommitHintViewModel>, IEnableLogger
{
public CommitHintView()
{
Expand All @@ -38,12 +40,12 @@ public CommitHintView()

this.WhenAny(x => x.ViewModel.HintState, x => x.Value.ToString())
.Throttle(TimeSpan.FromMilliseconds(500), RxApp.MainThreadScheduler)
.Subscribe(x => VisualStateManager.GoToElementState(visualRoot, x, true));
.Subscribe(applyElementVisualState);

this.WhenAnyObservable(x => x.ViewModel.RefreshStatus.ItemsInflight)
.Select(x => x != 0 ? "Loading" : "NotLoading")
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(x => VisualStateManager.GoToElementState(visualRoot, x, true));
.Subscribe(applyElementVisualState);

this.BindCommand(ViewModel, x => x.Open, x => x.Open);

Expand All @@ -59,17 +61,18 @@ public CommitHintView()

this.WhenAny(x => x.ViewModel.SuggestedOpacity, x => x.Value)
.Select(x => x + 0.25)
.ObserveOn(RxApp.MainThreadScheduler)
.BindTo(this, x => x.visualRoot.Opacity);

this.WhenAny(x => x.IsMouseOver, x => x.Value ? "Hover" : "NoHover")
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(x => VisualStateManager.GoToElementState(visualRoot, x, true));
.Subscribe(applyElementVisualState);

this.WhenAnyObservable(
x => x.ViewModel.RefreshLastCommitTime.ThrownExceptions,
x => x.ViewModel.RefreshStatus.ThrownExceptions)
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(_ => VisualStateManager.GoToElementState(visualRoot, "Error", true));
.Subscribe(_ => applyElementVisualState("Error"));

Observable.FromEventPattern<MouseButtonEventHandler, MouseButtonEventArgs>(x => visualRoot.PreviewMouseUp += x, x => visualRoot.PreviewMouseUp += x)
.Where(x => x.EventArgs.ChangedButton == MouseButton.Right)
Expand Down Expand Up @@ -125,5 +128,10 @@ object IViewFor.ViewModel {
get { return ViewModel; }
set { ViewModel = (CommitHintViewModel)value; }
}

void applyElementVisualState(string state)
{
Dispatcher.BeginInvoke(new Action(() => VisualStateManager.GoToElementState(visualRoot, state, true)));
}
}
}

0 comments on commit 92cb04f

Please sign in to comment.