Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private static void VerifyType(DependencyObject item)
{
if (!(item is IAction))
{
throw new InvalidOperationException(ResourceHelper.GetString("NonActionAddedToActionCollectionExceptionMessage"));
throw new InvalidOperationException(ResourceHelper.NonActionAddedToActionCollectionExceptionMessage);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void Attach(DependencyObject associatedObject)

if (this.AssociatedObject != null)
{
throw new InvalidOperationException(ResourceHelper.GetString("CannotAttachBehaviorMultipleTimesExceptionMessage"));
throw new InvalidOperationException(ResourceHelper.CannotAttachBehaviorMultipleTimesExceptionMessage);
}

Debug.Assert(associatedObject != null, "The previous checks should keep us from ever setting null here.");
Expand Down Expand Up @@ -162,12 +162,12 @@ private IBehavior VerifiedAttach(DependencyObject item)
IBehavior behavior = item as IBehavior;
if (behavior == null)
{
throw new InvalidOperationException(ResourceHelper.GetString("NonBehaviorAddedToBehaviorCollectionExceptionMessage"));
throw new InvalidOperationException(ResourceHelper.NonBehaviorAddedToBehaviorCollectionExceptionMessage);
}

if (this._oldCollection.Contains(behavior))
{
throw new InvalidOperationException(ResourceHelper.GetString("DuplicateBehaviorInCollectionExceptionMessage"));
throw new InvalidOperationException(ResourceHelper.DuplicateBehaviorInCollectionExceptionMessage);
}

if (this.AssociatedObject != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected override void OnAttached()
{
string actualType = base.AssociatedObject.GetType().FullName;
string expectedType = typeof (T).FullName;
string message = string.Format(ResourceHelper.GetString("InvalidAssociatedObjectExceptionMessage"), actualType, expectedType);
string message = string.Format(ResourceHelper.InvalidAssociatedObjectExceptionMessage, actualType, expectedType);
throw new InvalidOperationException(message);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Microsoft.Xaml.Interactivity;
/// <summary>
/// An action that calls a method on a specified object when invoked.
/// </summary>
#if NET8_0_OR_GREATER
#if NET5_0_OR_GREATER
[RequiresUnreferencedCode("This action is not trim-safe.")]
#endif
public sealed class CallMethodAction : DependencyObject, IAction
Expand Down
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.

Copy link
Copy Markdown
Author

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...

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Author

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.

/// </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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@

#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 property to a specified value when invoked.
/// </summary>
#if NET8_0_OR_GREATER
[RequiresUnreferencedCode("This action is not trim-safe.")]
#if NET5_0_OR_GREATER
[RequiresUnreferencedCode("This action is not trim-safe, as it uses reflection to set the property. Use ChangeDependencyPropertyAction for dependency properties, or write your own purpose-specific action.")]
#endif
[ContentProperty(Name = "Value")]
public sealed class ChangePropertyAction : DependencyObject, IAction
{
/// <summary>
Expand Down
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using System;
using System.Collections.Generic;
#if NET8_0_OR_GREATER
#if NET5_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif
using System.Reflection;
Expand All @@ -30,7 +30,7 @@ internal static class DataBindingHelper
/// bindings on the action may not be up-to-date. This routine is called before the action
/// is executed in order to guarantee that all bindings are refreshed with the most current data.
/// </remarks>
#if NET8_0_OR_GREATER
#if NET5_0_OR_GREATER
[RequiresUnreferencedCode("This method accesses all fields of input action objects.")]
#endif
public static void RefreshDataBindingsOnActions(ActionCollection actions)
Expand All @@ -45,7 +45,7 @@ public static void RefreshDataBindingsOnActions(ActionCollection actions)
}

private static IEnumerable<DependencyProperty> GetDependencyProperties(
#if NET8_0_OR_GREATER
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
#endif
Type type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Microsoft.Xaml.Interactivity;
/// <summary>
/// A behavior that performs actions when the bound data meets a specified condition.
/// </summary>
#if NET8_0_OR_GREATER
#if NET5_0_OR_GREATER
[RequiresUnreferencedCode("This behavior is not trim-safe.")]
#endif
public sealed class DataTriggerBehavior : Trigger
Expand Down
Loading