2

所以我刚刚创建了一个有事件的基本画布。然而,当我运行此代码时,该事件从未真正命中。我用 C# 编写 Metro 应用程序。我做错了什么?

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

这是我的 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");
    }
4

1 回答 1

5

您发现了 Canvases 的一个令人困惑的怪癖。您必须设置背景颜色才能对其进行命中测试。

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

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

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

于 2013-07-11T20:04:04.237 回答