|
| 1 | +using Microsoft.Xaml.Interactivity; |
| 2 | +using System; |
| 3 | +using Windows.UI.Xaml; |
| 4 | +using Windows.UI.Xaml.Controls; |
| 5 | +using Windows.UI.Xaml.Markup; |
| 6 | + |
| 7 | +namespace Template10.Behaviors |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Trigger which runs off a timer |
| 11 | + /// </summary> |
| 12 | + [ContentProperty(Name = "Actions")] |
| 13 | + public sealed class TimerTriggerBehavior : DependencyObject, IBehavior |
| 14 | + { |
| 15 | + private int _tickCount; |
| 16 | + private DispatcherTimer _timer; |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Backing storage for Actions collection |
| 20 | + /// </summary> |
| 21 | + public static readonly DependencyProperty ActionsProperty = |
| 22 | + DependencyProperty.Register("Actions", typeof(ActionCollection), typeof(TimerTriggerBehavior), new PropertyMetadata(null)); |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Actions collection |
| 26 | + /// </summary> |
| 27 | + public ActionCollection Actions |
| 28 | + { |
| 29 | + get |
| 30 | + { |
| 31 | + ActionCollection actions = (ActionCollection)base.GetValue(ActionsProperty); |
| 32 | + if (actions == null) |
| 33 | + { |
| 34 | + actions = new ActionCollection(); |
| 35 | + base.SetValue(ActionsProperty, actions); |
| 36 | + } |
| 37 | + return actions; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Backing storage for the counter |
| 43 | + /// </summary> |
| 44 | + public static readonly DependencyProperty MillisecondsPerTickProperty = |
| 45 | + DependencyProperty.Register("MillisecondsPerTick", typeof(double), typeof(TimerTriggerBehavior), new PropertyMetadata(1000.0)); |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Milliseconds |
| 49 | + /// </summary> |
| 50 | + public double MillisecondsPerTick |
| 51 | + { |
| 52 | + get |
| 53 | + { |
| 54 | + return (double)base.GetValue(MillisecondsPerTickProperty); |
| 55 | + } |
| 56 | + set |
| 57 | + { |
| 58 | + base.SetValue(MillisecondsPerTickProperty, value); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Backing storage for the total ticks counter |
| 64 | + /// </summary> |
| 65 | + public static readonly DependencyProperty TotalTicksProperty = |
| 66 | + DependencyProperty.Register("TotalTicks", typeof(int), typeof(TimerTriggerBehavior), new PropertyMetadata(-1)); |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Total ticks elapsed |
| 70 | + /// </summary> |
| 71 | + public int TotalTicks |
| 72 | + { |
| 73 | + get |
| 74 | + { |
| 75 | + return (int)base.GetValue(TotalTicksProperty); |
| 76 | + } |
| 77 | + set |
| 78 | + { |
| 79 | + base.SetValue(TotalTicksProperty, value); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Called when the timer elapses |
| 85 | + /// </summary> |
| 86 | + /// <param name="sender"></param> |
| 87 | + /// <param name="e"></param> |
| 88 | + private void OnTimerTick(object sender, object e) |
| 89 | + { |
| 90 | + if (this.TotalTicks > 0 && ++this._tickCount >= this.TotalTicks) |
| 91 | + { |
| 92 | + this.StopTimer(); |
| 93 | + } |
| 94 | + |
| 95 | + // Raise the actions |
| 96 | + Interaction.ExecuteActions(AssociatedObject, this.Actions, null); |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Called to start the timer |
| 101 | + /// </summary> |
| 102 | + internal void StartTimer() |
| 103 | + { |
| 104 | + this._timer = new DispatcherTimer { Interval = (TimeSpan.FromMilliseconds(this.MillisecondsPerTick)) }; |
| 105 | + this._timer.Tick += this.OnTimerTick; |
| 106 | + this._timer.Start(); |
| 107 | + } |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Called to stop the timer |
| 111 | + /// </summary> |
| 112 | + internal void StopTimer() |
| 113 | + { |
| 114 | + if (this._timer != null) |
| 115 | + { |
| 116 | + this._timer.Stop(); |
| 117 | + this._timer = null; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// Attaches to the specified object. |
| 123 | + /// </summary> |
| 124 | + /// <param name="associatedObject">The <see cref="T:Windows.UI.Xaml.DependencyObject"/> to which the <seealso cref="T:Microsoft.Xaml.Interactivity.IBehavior"/> will be attached.</param> |
| 125 | + public void Attach(DependencyObject associatedObject) |
| 126 | + { |
| 127 | + if ((associatedObject != AssociatedObject) && !Windows.ApplicationModel.DesignMode.DesignModeEnabled) |
| 128 | + { |
| 129 | + if (AssociatedObject != null) |
| 130 | + throw new InvalidOperationException("Cannot attach behavior multiple times."); |
| 131 | + |
| 132 | + AssociatedObject = associatedObject; |
| 133 | + StartTimer(); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /// <summary> |
| 138 | + /// Detaches this instance from its associated object. |
| 139 | + /// </summary> |
| 140 | + public void Detach() |
| 141 | + { |
| 142 | + this.StopTimer(); |
| 143 | + AssociatedObject = null; |
| 144 | + } |
| 145 | + |
| 146 | + /// <summary> |
| 147 | + /// Gets the <see cref="T:Windows.UI.Xaml.DependencyObject"/> to which the <seealso cref="T:Microsoft.Xaml.Interactivity.IBehavior"/> is attached. |
| 148 | + /// </summary> |
| 149 | + public DependencyObject AssociatedObject { get; private set; } |
| 150 | + } |
| 151 | + |
| 152 | + |
| 153 | + [ContentProperty(Name = "Actions")] |
| 154 | + [TypeConstraint(typeof(TextBox))] |
| 155 | + public class TextBoxEnterBehavior : DependencyObject, IBehavior |
| 156 | + { |
| 157 | + private TextBox AssociatedTextBox { get { return AssociatedObject as TextBox; } } |
| 158 | + public DependencyObject AssociatedObject { get; private set; } |
| 159 | + |
| 160 | + public void Attach(DependencyObject associatedObject) |
| 161 | + { |
| 162 | + AssociatedObject = associatedObject; |
| 163 | + AssociatedTextBox.KeyDown += AssociatedTextBox_KeyDown; |
| 164 | + } |
| 165 | + |
| 166 | + public void Detach() |
| 167 | + { |
| 168 | + AssociatedTextBox.KeyDown -= AssociatedTextBox_KeyDown; |
| 169 | + } |
| 170 | + |
| 171 | + private void AssociatedTextBox_KeyDown(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e) |
| 172 | + { |
| 173 | + if (e.Key == Windows.System.VirtualKey.Enter) |
| 174 | + { |
| 175 | + Interaction.ExecuteActions(AssociatedObject, this.Actions, null); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + public ActionCollection Actions |
| 180 | + { |
| 181 | + get |
| 182 | + { |
| 183 | + var actions = (ActionCollection)base.GetValue(ActionsProperty); |
| 184 | + if (actions == null) |
| 185 | + { |
| 186 | + base.SetValue(ActionsProperty, actions = new ActionCollection()); |
| 187 | + } |
| 188 | + return actions; |
| 189 | + } |
| 190 | + } |
| 191 | + public static readonly DependencyProperty ActionsProperty = |
| 192 | + DependencyProperty.Register("Actions", typeof(ActionCollection), |
| 193 | + typeof(TextBoxEnterBehavior), new PropertyMetadata(null)); |
| 194 | + } |
| 195 | +} |
0 commit comments