-
Notifications
You must be signed in to change notification settings - Fork 121
Add trim-safe replacements for ChangePropertyAction and EventTriggerBehavior #309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sylveon
wants to merge
6
commits into
microsoft:main
Choose a base branch
from
sylveon:user/sylveon/trim-safe
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
401966b
Simplify ResourceHelper logic to avoid needing #if
sylveon 8574e90
CsWinRT and trimming are .NET 5 features
sylveon ae4fe4f
New action: ChangeDependencyPropertyAction
sylveon 6570fde
Add new trim-safe EventTriggerBehaviorBase<T>, rewrite EventTriggerBe…
sylveon 52f678b
Remove extra newline
sylveon ae4da8a
seal the classes
sylveon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
88 changes: 88 additions & 0 deletions
88
...iorsSDKManaged/Microsoft.Xaml.Interactivity.Shared/Core/ChangeDependencyPropertyAction.cs
This file contains hidden or 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,88 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| #if WinUI | ||
| using Microsoft.UI.Xaml; | ||
| using Microsoft.UI.Xaml.Markup; | ||
| #else | ||
| using Windows.UI.Xaml; | ||
| using Windows.UI.Xaml.Markup; | ||
| #endif | ||
|
|
||
| namespace Microsoft.Xaml.Interactivity; | ||
|
|
||
| /// <summary> | ||
| /// An action that will change a specified dependency property to a specified value when invoked. | ||
| /// </summary> | ||
| [ContentProperty(Name = "Value")] | ||
| public sealed class ChangeDependencyPropertyAction : DependencyObject, IAction | ||
| { | ||
| /// <summary> | ||
| /// Identifies the <seealso cref="TargetObject"/> dependency property. | ||
| /// </summary> | ||
| public static readonly DependencyProperty TargetObjectProperty = DependencyProperty.Register( | ||
| "TargetObject", | ||
| typeof(DependencyObject), | ||
| typeof(ChangeDependencyPropertyAction), | ||
| new PropertyMetadata(null)); | ||
|
|
||
| /// <summary> | ||
| /// Identifies the <seealso cref="Value"/> dependency property. | ||
| /// </summary> | ||
| public static readonly DependencyProperty ValueProperty = DependencyProperty.Register( | ||
| "Value", | ||
| typeof(object), | ||
| typeof(ChangeDependencyPropertyAction), | ||
| new PropertyMetadata(null)); | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the dependency property to change. This is not a dependency property, due to framework restrictions. | ||
| /// </summary> | ||
| public DependencyProperty DependencyProperty { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the value to set. This is a dependency property. | ||
| /// </summary> | ||
| public object Value | ||
| { | ||
| get => GetValue(ValueProperty); | ||
| set => SetValue(ValueProperty, value); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the object whose property will be changed. | ||
| /// If <seealso cref="TargetObject"/> is not set or cannot be resolved, the sender of <seealso cref="Execute"/> will be used. This is a dependency property. | ||
| /// </summary> | ||
| public DependencyObject TargetObject | ||
| { | ||
| get => (DependencyObject)GetValue(TargetObjectProperty); | ||
| set => SetValue(TargetObjectProperty, value); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Executes the action. | ||
| /// </summary> | ||
| /// <param name="sender">The <see cref="object"/> that is passed to the action by the behavior. Generally this is <seealso cref="IBehavior.AssociatedObject"/> or a target object.</param> | ||
| /// <param name="parameter">Ignored</param> | ||
| /// <returns>True if updating the dependency property value succeeds; else false.</returns> | ||
| public object Execute(object sender, object parameter) | ||
| { | ||
| DependencyObject targetObject; | ||
| if (ReadLocalValue(TargetObjectProperty) != DependencyProperty.UnsetValue) | ||
| { | ||
| targetObject = TargetObject; | ||
| } | ||
| else | ||
| { | ||
| targetObject = sender as DependencyObject; | ||
| } | ||
|
|
||
| if (targetObject == null || DependencyProperty == null) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| targetObject.SetValue(DependencyProperty, Value); | ||
| return true; | ||
| } | ||
| } | ||
This file contains hidden or 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
29 changes: 29 additions & 0 deletions
29
src/BehaviorsSDKManaged/Microsoft.Xaml.Interactivity.Shared/Core/CheckedTriggerBehavior.cs
This file contains hidden or 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,29 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| #if WinUI | ||
| using Microsoft.UI.Xaml.Controls.Primitives; | ||
| #else | ||
| using Windows.UI.Xaml.Controls.Primitives; | ||
| #endif | ||
|
|
||
| namespace Microsoft.Xaml.Interactivity; | ||
|
|
||
| /// <summary> | ||
| /// A behavior that listens to an <see cref="ToggleButton"/> and executes its actions when the button is checked. | ||
| /// </summary> | ||
| public sealed class CheckedTriggerBehavior : EventTriggerBehaviorBase<ToggleButton> | ||
| { | ||
| /// <inheritdoc/> | ||
| protected override bool RegisterEventCore(ToggleButton source) | ||
| { | ||
| source.Checked += OnEvent; | ||
| return true; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| protected override void UnregisterEventCore(ToggleButton source) | ||
| { | ||
| source.Checked -= OnEvent; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All dependency properties for this class break when one of the DPs has DependencyProperty itself as its type. Meta-DPs are reserved to the framework itself apparently...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 Interesting, I know sometimes not having it as a DP could cause VS to bubble up other warnings when binding to things, but since it'd only ever be a OneTime binding sort of scenario, I can't imagine it being an issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Advanced binding scenarios which would trigger that warning probably requires you to make your own DP-of-DP, which you aren't able to, so I think this should be fine. As you've mentioned, this is rarely ever changed at runtime.