Skip to content
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

Add RaiseEvent generic extension method #8999

Open
mwpowellhtx opened this issue Apr 7, 2024 · 5 comments
Open

Add RaiseEvent generic extension method #8999

mwpowellhtx opened this issue Apr 7, 2024 · 5 comments
Labels
API suggestion Early API idea and discussion, it is NOT ready for implementation

Comments

@mwpowellhtx
Copy link

Add a convenience extension method allowing for contemporary and planned language level features to be more prominently used.

namspace System.Windows
{
    // EventManager is already static, so this seems like the best placement for it
    public static class EventManager
    {
        public static void RaiseEvent<TArguments>(this DependencyObject source, TArguments e)
            where TArguments : RoutedEventArgs => source.RaiseEvent(e);

        // Then if folks wish to be a little more strict about the arguments, they can be
        public static void RaiseEvent<TSource, TArguments>(this TSource source, TArguments e)
            where TSource : DependencyObject
            where TArguments : RoutedEventArgs => source.RaiseEvent(e);
    }
}

And in practical usage, assuming appropriate event declarations, etc:

public class BubblesManager
{
    public static void RaiseEvent(DependencyObject source, int bubbles)
        => source.RaiseEvent<BubblesRoutedEventArgs>(new(BubblingEvent, source, bubbles));
        //                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}

For sake of example:

public class BubblesRoutedEventArgs : RoutedEventArgs
{
    public int Bubbles { get; }

    internal BubblesRoutedEventArgs(RoutedEvent routedEvent, object source, int bubbles)
        : base(routedEvent, source)
        //     ^^^^^^^^^^^  ^^^^^^
    {
        Bubbles = bubbles;
    }
}

Tangentially, although at first glance, it seems counterintuitive why we would need to relay the RoutedEventArgs.RoutedEvent, especially given the event mechanism. Might seem better to do something like:

source.RaiseEvent(BubblesEvent, e);

And then since DependencyObject is handling the decoupled bookkeeping for us, we can focus more on the arguments properties themselves without having to also concern ourselves with the bookkeeping, even in raising the event.

@mwpowellhtx
Copy link
Author

Maybe not an extension method, more akin to a UIElement method:

namespace System.Windows;

public class UIElement
{
    // ...
    private void OnRaisedEvent(RoutedEventArgs e) { /* ... */ }
    public void RaiseEvent(RoutedEventArgs e) => OnRaisedEvent(e);
    public void RaiseEvent<TArguments>(TArguments e) where TArguments : RoutedEventArgs => OnRaisedEvent(e);
    // ...
}

Anyway, you get the idea.

@miloush
Copy link
Contributor

miloush commented Apr 7, 2024

allowing for contemporary and planned language level features to be more prominently used.

What do you mean by this? Which language features?

Why do you need a generic method, what benefit does it have?

@mwpowellhtx
Copy link
Author

allowing for contemporary and planned language level features to be more prominently used.

What do you mean by this? Which language features?

Why do you need a generic method, what benefit does it have?

I illustrated the benefit in the RaiseEvent example, i.e. new(BubblingEvent, source, bubbles), or more specifically, source.RaiseEvent<BubblesRoutedEventArgs>(new(BubblingEvent, source, bubbles)).

@lindexi lindexi added API suggestion Early API idea and discussion, it is NOT ready for implementation and removed Untriaged labels Apr 8, 2024
@lindexi
Copy link
Contributor

lindexi commented Apr 8, 2024

It seems like an intriguing proposition, but the improvements appear to be minimal and it doesn’t seem to offer much convenience. Nonetheless, I appreciate your suggestion as it can spark inspiration.

@mwpowellhtx
Copy link
Author

No problem. Admittedly, the scope is small, yes; that is my particular focus right at the moment, as I am evaluating attached routed events, whether it might help in an opportunity I have at the moment. I did not assess other functions, whether generic versions might benefit there as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API suggestion Early API idea and discussion, it is NOT ready for implementation
Projects
None yet
Development

No branches or pull requests

3 participants