Xaml和事件不起作用

Arm*_*yan 2 c# xaml

所以我刚刚创建了一个有事件的基本画布.然而,当我运行此代码时,事件永远不会被击中.我正在用c#写地铁应用程序.我做错了什么?

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <Canvas HorizontalAlignment="Left" Height="673" VerticalAlignment="Top" Width="1346" Margin="10,85,0,0" PointerMoved="Canvas_PointerMoved"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)

继承人我的c#代码

    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");
    }
Run Code Online (Sandbox Code Playgroud)

Rob*_*b H 5

你发现了一个令人困惑的画布怪癖.您必须设置背景颜色才能对其进行测试.

因此,例如,将您的代码更改为此,它将触及该事件:

<Grid>
    <Canvas Background="Blue" HorizontalAlignment="Left" Height="673" VerticalAlignment="Top" Width="1346" Margin="10,85,0,0" PointerMove="Canvas_PointerMoved"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)

但是,您需要考虑的一件事是Canvas是否是适合使用的面板.它非常原始,除非您需要严格定义布局或对性能进行微优化,否则通常不会使用它.