D3FALT404
D3FALT404

Reputation: 23

C# UWP why can't I add a second event handler?

I am new to UWP, and c#. I have a problem with canvas object in XAML, I have added event handler, which is tapped, and I tried to add, also RightTapped, but the event handlers menu is empty, and I can't add anything. I was looking for answer, but I couldn't find any. How to add event to canvas?

enter image description here

Upvotes: 0

Views: 138

Answers (1)

Leander
Leander

Reputation: 859

You can also just add eventhandlers through the XAML:

<Page
    x:Class="Test.MainPage">

    <Canvas x:Name="MainCanvas" Tapped="MainCanvas_Tapped" RightTapped="MainCanvas_RightTapped"/>
</Page>

Then make sure your MainPage.cs contains a Canvas_Tapped and Canvas_RightTapped eventhandler, like this:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }

    private void MainCanvas_Tapped(object sender, TappedRoutedEventArgs e)
    {

    }

    private void MainCanvas_RightTapped(object sender, RightTappedRoutedEventArgs e)
    {

    }
}

If you don't want to type in the XAML you can also assign the eventhandlers to the Canvas after it has been initialized. In that case you need to change the MainPage constructor:

public MainPage()
{
    this.InitializeComponent();
    MainCanvas.Tapped += MainCanvas_Tapped;
    MainCanvas.RightTapped += MainCanvas_RightTapped;
}

Upvotes: 1

Related Questions