Skip to content

Commit 116ea64

Browse files
committed
Implement ObservableTrigger as a Behavior for Win 8.1
1 parent 2509107 commit 116ea64

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reactive.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Microsoft.Xaml.Interactivity;
8+
using Windows.UI.Xaml;
9+
using Windows.UI.Xaml.Markup;
10+
11+
namespace ReactiveUI.Blend
12+
{
13+
[ContentProperty(Name = "Actions")]
14+
public sealed class ObservableTriggerBehavior : Behavior<DependencyObject>
15+
{
16+
private object resolvedSource;
17+
18+
public ActionCollection Actions
19+
{
20+
get {
21+
ActionCollection actionCollection = (ActionCollection) this.GetValue(ObservableTriggerBehavior.ActionsProperty);
22+
if (actionCollection == null) {
23+
actionCollection = new ActionCollection();
24+
this.SetValue(ObservableTriggerBehavior.ActionsProperty, actionCollection);
25+
}
26+
return actionCollection;
27+
}
28+
}
29+
public static readonly DependencyProperty ActionsProperty =
30+
DependencyProperty.Register("Actions", typeof(ActionCollection), typeof(ObservableTriggerBehavior), new PropertyMetadata(null));
31+
32+
public object SourceObject
33+
{
34+
get { return this.GetValue(ObservableTriggerBehavior.SourceObjectProperty); }
35+
set { this.SetValue(ObservableTriggerBehavior.SourceObjectProperty, value); }
36+
}
37+
public static readonly DependencyProperty SourceObjectProperty =
38+
DependencyProperty.Register("SourceObject", typeof(object), typeof(ObservableTriggerBehavior), new PropertyMetadata(null, OnSourceObjectChanged));
39+
40+
private static void OnSourceObjectChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
41+
{
42+
ObservableTriggerBehavior observableTriggerBehavior = (ObservableTriggerBehavior)dependencyObject;
43+
observableTriggerBehavior.SetResolvedSource(observableTriggerBehavior.ComputeResolvedSource());
44+
}
45+
46+
public IObservable<object> Observable
47+
{
48+
get { return (IObservable<object>)GetValue(ObservableProperty); }
49+
set { SetValue(ObservableProperty, value); }
50+
}
51+
public static readonly DependencyProperty ObservableProperty =
52+
DependencyProperty.Register("Observable", typeof(IObservable<object>), typeof(ObservableTriggerBehavior), new PropertyMetadata(null, onObservableChanged));
53+
54+
private void SetResolvedSource(object newSource)
55+
{
56+
if (this.AssociatedObject == null || this.resolvedSource == newSource)
57+
{
58+
return;
59+
}
60+
this.resolvedSource = newSource;
61+
}
62+
63+
private object ComputeResolvedSource()
64+
{
65+
if (this.ReadLocalValue(ObservableTriggerBehavior.SourceObjectProperty) != DependencyProperty.UnsetValue) {
66+
return this.SourceObject;
67+
} else {
68+
return this.AssociatedObject;
69+
}
70+
}
71+
72+
public bool AutoResubscribeOnError { get; set; }
73+
74+
IDisposable watcher;
75+
protected static void onObservableChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
76+
{
77+
ObservableTriggerBehavior This = (ObservableTriggerBehavior)sender;
78+
if (This.watcher != null)
79+
{
80+
This.watcher.Dispose();
81+
This.watcher = null;
82+
}
83+
84+
This.watcher = ((IObservable<object>)e.NewValue).ObserveOn(RxApp.MainThreadScheduler).Subscribe(
85+
x => Interaction.ExecuteActions(This.resolvedSource, This.Actions, x),
86+
ex =>
87+
{
88+
if (!This.AutoResubscribeOnError)
89+
return;
90+
onObservableChanged(This, e);
91+
});
92+
}
93+
94+
protected override void OnAttached()
95+
{
96+
base.OnAttached();
97+
this.SetResolvedSource(this.ComputeResolvedSource());
98+
}
99+
100+
protected override void OnDetaching()
101+
{
102+
this.SetResolvedSource(null);
103+
base.OnDetaching();
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)