0

I'm having difficulty finding how to register a RoutedEventHandler in UWP. I'm attempting to code a template control that has event properties similar to ContentDialog's:

PrimaryButtonClick="ClickEvent"

Where ClickEvent is defined in the cs file. I'm only just getting the hang of templates, but I believe I want to do something that looks like this:

<Button Content="{TemplateBinding PrimaryButtonText}" Click="{TemplateBinding PrimaryButtonClick}"/>

Currently, all I can find is references to WPF versions of this type of code:

 public static readonly RoutedEvent ValueChangedEvent =
        EventManager.RegisterRoutedEvent("ValueChanged",
RoutingStrategy.Direct, typeof(RoutedPropertyChangedEventHandler<double>),
typeof(NumericBox));

    public event RoutedPropertyChangedEventHandler<double> ValueChanged
    {
        add { AddHandler(ValueChangedEvent, value); }
        remove { RemoveHandler(ValueChangedEvent, value); }
    }

    private void OnValueChanged(double oldValue, double newValue)
    {
        RoutedPropertyChangedEventArgs<double> args =
    new RoutedPropertyChangedEventArgs<double>(oldValue, newValue);
        args.RoutedEvent = NumericBox.ValueChangedEvent;
        RaiseEvent(args);
    }

But of course the types have changed. Can someone point me in the right direction?

jchristof
  • 2,794
  • 7
  • 32
  • 47

1 Answers1

5

Unfortunately, the concept of RoutedEvent (bubbling, tunneling) is not available in UWP currently. You can just create a classic event however instead:

public event EventHandler PrimaryButtonClick;

protected void InnerButton_Click(object sender, EventArgs e)
{
    PrimaryButtonClick?.Invoke( sender, e );              
}

Bubbling of events is possible for some predefined events, but it is not yet possible to allow bubbling for custom events in current version of UWP.

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
  • 1
    Routed Events do exist in UWP. But for now, they are not as complete as in WPF. For example, bubbling is available but tunneling is not available https://learn.microsoft.com/en-us/windows/uwp/xaml-platform/events-and-routed-events-overview – SERWare Feb 02 '18 at 11:43
  • True, it is in form of bubbling. But I meant more specifically that there is no `RoutedEvent`-like type available – Martin Zikmund Feb 02 '18 at 11:45
  • 1
    There's no RoutedEvent type, Ok. And making it clear that predefined UWP UI events bubble from child to parents, your answer is correct and should be marked as the solution. – SERWare Feb 02 '18 at 13:30
  • I have updated my answer to have a mention about bubbling too :-) – Martin Zikmund Feb 02 '18 at 13:32
  • Thank you sir :-) ! – Martin Zikmund Feb 02 '18 at 13:39