2

so I just created a basic canvas that has a event. Yet when I run this code the event is never actually hit. Im writing in c# for metro apps. What did I do wrong?

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <Canvas HorizontalAlignment="Left" Height="673" VerticalAlignment="Top" Width="1346" Margin="10,85,0,0" PointerMoved="Canvas_PointerMoved"/>
</Grid>

Heres my c# code

    public MainPage()
    {
        this.InitializeComponent();
    }

    /// <summary>
    /// Invoked when this page is about to be displayed in a Frame.
    /// </summary>
    /// <param name="e">Event data that describes how this page was reached.  The Parameter
    /// property is typically used to configure the page.</param>
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }

    private void Canvas_PointerMoved(object sender, PointerRoutedEventArgs e)
    {
        Debug.WriteLine("hit");
    }
Armen Aghajanyan
  • 368
  • 3
  • 15

1 Answers1

5

You've discovered a confusing quirk of Canvases. You have to set a background color in order for it to be hit tested.

So, for example, change your code to this and it will hit the event:

<Grid>
    <Canvas Background="Blue" HorizontalAlignment="Left" Height="673" VerticalAlignment="Top" Width="1346" Margin="10,85,0,0" PointerMove="Canvas_PointerMoved"/>
</Grid>

But, one thing you'll need to consider is whether Canvas is the right kind of panel to use. It is extremely primitive and generally not used unless you need to rigidly define the layout or are micro-optimizing for performance.

Rob H
  • 1,840
  • 16
  • 25