0

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

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.