-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from nigel-sampson/feature/triggers
Adds an interface for triggers. Resolves #20
- Loading branch information
Showing
6 changed files
with
105 additions
and
58 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
15 changes: 15 additions & 0 deletions
15
src/BehaviorsSDKManaged/Microsoft.Xaml.Interactivity/ITrigger.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
namespace Microsoft.Xaml.Interactivity | ||
{ | ||
/// <summary> | ||
/// Interface implemented by all custom triggers. | ||
/// </summary> | ||
public interface ITrigger : IBehavior | ||
{ | ||
/// <summary> | ||
/// Gets the collection of actions associated with the behavior. | ||
/// </summary> | ||
ActionCollection Actions { get; } | ||
} | ||
} |
This file contains 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
43 changes: 43 additions & 0 deletions
43
src/BehaviorsSDKManaged/Microsoft.Xaml.Interactivity/Trigger.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using Windows.UI.Xaml; | ||
|
||
namespace Microsoft.Xaml.Interactivity | ||
{ | ||
/// <summary> | ||
/// A base class for behaviors, implementing the basic plumbing of ITrigger | ||
/// </summary> | ||
/// <typeparam name="T">The object type to attach to</typeparam> | ||
public abstract class Trigger : Behavior, ITrigger | ||
{ | ||
/// <summary> | ||
/// Identifies the <seealso cref="Actions"/> dependency property. | ||
/// </summary> | ||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")] | ||
public static readonly DependencyProperty ActionsProperty = DependencyProperty.Register( | ||
"Actions", | ||
typeof(ActionCollection), | ||
typeof(Trigger), | ||
new PropertyMetadata(null)); | ||
|
||
/// <summary> | ||
/// Gets the collection of actions associated with the behavior. This is a dependency property. | ||
/// </summary> | ||
public ActionCollection Actions | ||
{ | ||
get | ||
{ | ||
ActionCollection actionCollection = (ActionCollection)this.GetValue(Trigger.ActionsProperty); | ||
if (actionCollection == null) | ||
{ | ||
actionCollection = new ActionCollection(); | ||
this.SetValue(Trigger.ActionsProperty, actionCollection); | ||
} | ||
|
||
return actionCollection; | ||
} | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/BehaviorsSDKManaged/Microsoft.Xaml.Interactivity/TriggerOfT.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using Windows.UI.Xaml; | ||
|
||
namespace Microsoft.Xaml.Interactivity | ||
{ | ||
/// <summary> | ||
/// A base class for behaviors, implementing the basic plumbing of ITrigger | ||
/// </summary> | ||
public abstract class Trigger<T> : Trigger where T : DependencyObject | ||
{ | ||
/// <summary> | ||
/// Gets the object to which this behavior is attached. | ||
/// </summary> | ||
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] | ||
public new T AssociatedObject | ||
{ | ||
get { return base.AssociatedObject as T; } | ||
} | ||
|
||
/// <summary> | ||
/// Called after the behavior is attached to the <see cref="Microsoft.Xaml.Interactivity.Behavior.AssociatedObject"/>. | ||
/// </summary> | ||
/// <remarks> | ||
/// Override this to hook up functionality to the <see cref="Microsoft.Xaml.Interactivity.Behavior.AssociatedObject"/> | ||
/// </remarks> | ||
protected override void OnAttached() | ||
{ | ||
base.OnAttached(); | ||
|
||
if (this.AssociatedObject == null) | ||
{ | ||
string actualType = base.AssociatedObject.GetType().FullName; | ||
string expectedType = typeof(T).FullName; | ||
string message = string.Format(ResourceHelper.GetString("InvalidAssociatedObjectExceptionMessage"), actualType, expectedType); | ||
throw new InvalidOperationException(message); | ||
} | ||
} | ||
} | ||
} |