簡體   English   中英

Xaml和事件不起作用

[英]Xaml and Events not Working

所以我剛剛創建了一個帶有事件的基本畫布。 但是,當我運行此代碼時,該事件實際上從未發生。 我在用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");
    }

您發現了一個令人困惑的“畫布”怪癖。 您必須設置背景色才能進行點擊測試。

因此,例如,將您的代碼更改為此,它將觸發事件:

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

但是,您需要考慮的一件事是Canvas是否是使用正確的面板。 它非常原始,通常不使用,除非您需要嚴格定義布局或對性能進行微優化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM