gpt4 book ai didi

c# - Xaml 和事件不起作用

转载 作者:行者123 更新时间:2023-12-02 04:58:05 25 4
gpt4 key购买 nike

所以我刚刚创建了一个包含事件的基本 Canvas 。然而,当我运行这段代码时,事件从未真正发生过。我用 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");
}

最佳答案

您发现了 Canvas 的一个令人困惑的怪癖。您必须设置背景颜色才能进行 HitTest 。

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

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

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

关于c# - Xaml 和事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17602368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com