Skip to content

Conversation

@sylveon
Copy link

@sylveon sylveon commented Jun 5, 2025

#255 added trim annotation for various commonly used behaviors and actions. However, UWP .NET 9 requires NAOT (and by extension trimming-safe code), which meant that users migrating from UWP .NET Native to UWP .NET 9 where simply given "this behavior is not trim-safe" with no useful actionable info in the warnings. This is not helpful.

This PR adds a few behaviors and actions:

  • ChangeDependencyPropertyAction, a trim-safe alternative to ChangePropertyAction which specifically targets dependency properties. This should cover many use cases that ChangePropertyAction previously covered (in our app, we where able to replace all uses with this). Use like the following:
    <ChangeDependencyPropertyAction DependencyProperty="{x:Bind wux:FrameworkElement.MarginProperty}"
                                    TargetDependencyObject="{x:Bind MyControl}">
        <Thickness>0,0,36,0</Thickness>
    </ChangeDependencyPropertyAction>
  • EventTriggerBehaviorBase<T>, a trim-safe version of EventTriggerBehavior. Not useful by itself, it needs to be derived to add event registration and unregistration code. I've added a few subclasses covering the most common cases in our app, that hopefully are frequently used elsewhere as well. Additionally, I refactored the reflection-based EventTriggerBehavior to derive from EventTriggerBehaviorBase<T>, as an example of more complex subclasses (and to reduce code duplication). Use like the following:
    public class PointerWheelChangedTriggerBehavior : EventTriggerBehaviorBase<UIElement>
    {
        protected override bool RegisterEventCore(UIElement source)
        {
            source.PointerWheelChanged += OnEvent;
            return true;
        }
    
        protected override void UnregisterEventCore(UIElement source)
        {
            source.PointerWheelChanged -= OnEvent;
        }
    }
    <PointerWheelChangedTriggerBehavior>
        <InvokeCommandAction Command="{x:Bind ViewModel.OnRenderControlPointerWheelChangedCommand}" />
    </PointerWheelChangedTriggerBehavior>

These additions are backwards compatible with .NET Native, to allow progressive migration towards trim-safe patterns in an existing .NET Native UWP app, allowing easier upgrade to UWP .NET 9 or WinUI 3 down the line.

A few drive-by changes where made to improve code quality as well.

new PropertyMetadata(null));

/// <summary>
/// Gets or sets the dependency property to change. This is not a dependency property, due to framework restrictions.
Copy link
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
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
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.

@sylveon
Copy link
Author

sylveon commented Jun 14, 2025

@michael-hawker any update on this? would love to get it merged

@michael-hawker
Copy link
Contributor

@sylveon sorry for the delay, it may be a bit, but I too would like to get it merged. Would be good to get @Sergio0694 sign-off in the meantime. But I'll raise it up.

@sylveon
Copy link
Author

sylveon commented Jul 12, 2025

@Sergio0694 have you been able to look at this?

@sylveon
Copy link
Author

sylveon commented Aug 25, 2025

@michael-hawker @Sergio0694 any update?

@riverar
Copy link

riverar commented Jan 17, 2026

@Sergio0694 Can you sign off on this while you're online and screwing around on Discord chat? Thanks buddy. ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants