【问题标题】:Xaml and Events not WorkingXaml 和事件不起作用
【发布时间】:2023-03-20 16:56:02
【问题描述】:

所以我刚刚创建了一个包含事件的基本画布。然而,当我运行此代码时,该事件从未真正命中。我用 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");
    }

【问题讨论】:

    标签: c# xaml


    【解决方案1】:

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

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

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-06
    • 2023-03-12
    • 1970-01-01
    • 2014-01-19
    • 2020-05-08
    • 2015-12-30
    • 1970-01-01
    • 2012-07-13
    相关资源
    最近更新 更多