【问题标题】:Weird issues with PointerWheelChangedPointerWheelChanged 的​​奇怪问题
【发布时间】:2021-10-10 19:44:46
【问题描述】:

我有一个带有网格的简单页面。网格包含一个 TextBlock。我在页面中添加了 PointerWheelChanged 的​​处理程序。

由于某种原因,该事件仅在鼠标指针位于 TextBlock 内而不是其他任何地方时调用。

我尝试将事件添加到网格中。

我的代码:

<Page
    x:Class="App.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
    PointerWheelChanged="Page_PointerWheelChanged">

    <Grid>
        <TextBlock HorizontalAlignment="Center" Text="TextBlock" TextWrapping="Wrap" VerticalAlignment="Center" Height="980" Width="1480" x:Name="test" FontSize="72"/>
    </Grid>
</Page>
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;

namespace App {
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void Page_PointerWheelChanged(object sender, PointerRoutedEventArgs e) {
            var delta = e.GetCurrentPoint((UIElement)sender).Properties.MouseWheelDelta;
            test.Text = delta.ToString();
        }
    }
}

我最初是在一个更复杂的应用程序中发现的。这是一个简单的应用程序,我尝试在其中重现此行为。

【问题讨论】:

    标签: c# uwp uwp-xaml


    【解决方案1】:

    当您没有为元素设置背景并为元素设置背景=“透明”时,情况会有所不同。后者使控件具有交互性。您可以尝试为网格设置透明背景,以允许网格响应鼠标事件。

    像这样:

      <Grid Background="Transparent" >
        <TextBlock HorizontalAlignment="Center" Text="TextBlock" TextWrapping="Wrap" VerticalAlignment="Center" Height="200" Width="200" x:Name="test" FontSize="72"/>
    </Grid>
    

    出于测试目的,我稍微改变了 Textblock 的大小。

    【讨论】:

      猜你喜欢
      • 2015-02-27
      • 2018-06-26
      • 2011-08-13
      • 2011-08-17
      • 2011-02-02
      • 2014-04-21
      • 2010-12-30
      • 2014-03-21
      • 2018-07-05
      相关资源
      最近更新 更多