【问题标题】:'MouseEventArgs' does not contain a definition for 'GetPosition'“MouseEventArgs”不包含“GetPosition”的定义
【发布时间】:2017-06-02 15:20:24
【问题描述】:

我确定这是一个超级简单的,但我正在开发一个 C# UWP 应用程序并试图制作一个可拖动的用户控件,但在我的鼠标事件中我收到以下错误:

CS1061“MouseEventArgs”不包含“GetPosition”的定义,并且找不到接受“MouseEventArgs”类型的第一个参数的扩展方法“GetPosition”(您是否缺少 using 指令或程序集引用?)

我发现我在堆栈上使用的这个示例 (Dragging a WPF user control) 并且之前使用鼠标按下事件在 winforms 的列表框之间拖动项目,但没有这个问题。

这是我的代码:

<UserControl
    x:Class="HopHaus.TimerControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:HopHaus"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400" Width="120" Height="60">

    <Grid Margin="0,0,0,167" Width="120">
        <Rectangle Fill="#FF404040" HorizontalAlignment="Left" Height="60" Margin="1,0,-1,-133" Stroke="Black" VerticalAlignment="Top" Width="120"/>
        <TextBox x:Name="timersetBox" Margin="-1,-2,1,-59" TextWrapping="Wrap" Text="00" VerticalAlignment="Top" Height="60" BorderBrush="Transparent" FontFamily="Fonts/DIGITAL.TTF#Digital" Foreground="#FF72FB00" FontSize="50" Background="Transparent" TextAlignment="Right" AcceptsReturn="True" SelectionHighlightColor="#000078D7" Width="120"/>
        <Border x:Name="dragBrdr" BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="59" Margin="2,0,0,-59" VerticalAlignment="Top" Width="118"/>
    </Grid>
</UserControl>

还有:

 public sealed partial class TimerControl : UserControl
    {
        Point currentPoint;
        Point anchorPoint;
        bool isInDrag;
        public TimerControl()
        {
            this.InitializeComponent();
        }

        private TranslateTransform transform = new TranslateTransform();
        private void root_MouseMove(object sender, MouseEventArgs e)
        {
            if (isInDrag)
            {
                var element = sender as FrameworkElement;
                currentPoint = e.GetPosition(null);

                transform.X += currentPoint.X - anchorPoint.X;
                transform.Y += (currentPoint.Y - anchorPoint.Y);
                this.RenderTransform = transform;
                anchorPoint = currentPoint;
            }
        }
    }

我同时使用System.Windows.Input & using Windows.Devices.Input

非常感谢您提供的任何帮助。

【问题讨论】:

    标签: c# wpf visual-studio xaml uwp


    【解决方案1】:

    为了更好地支持触摸和墨迹书写,对鼠标事件进行了抽象。这称为指针。所以在你身上你有一个 PointerMoved 事件

    在xml中:

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
              PointerMoved="Grid_PointerMoved">
    
        </Grid>
    

    在代码中

        private void Grid_PointerMoved(object sender, PointerRoutedEventArgs e)
        {
            var point = e.GetCurrentPoint(null);
        }
    

    【讨论】:

    • 好的,非常感谢,过去几个小时我一直在阅读,但我仍然不确定如何将我的 var point 转换为我在 MainPage 上的用户控件的新位置。
    【解决方案2】:

    但我仍然不确定如何获取我的 var 点并将其转换为我的 MainPage 上用户控件的新位置

    正如@Dave Smits 所说,您需要一个Pointer 事件句柄。更多关于句柄指针输入的细节请参考this document。我认为您感到困惑的是,此代码 var point = e.GetCurrentPoint(null); 返回 PointerPoint 对象而不是 Point 结构您需要进行转换。在这种情况下,您可以通过PointerPoint.Position 属性获取Point 结构。更新代码如下:

    <UserControl
        ...
        mc:Ignorable="d"
        d:DesignHeight="300"
        d:DesignWidth="400"  
       Width="120" Height="60"  PointerMoved="Grid_PointerMoved" CanDrag="True">
        <Grid Margin="0,0,0,167" Width="120" >
            <Rectangle Fill="#FF404040" HorizontalAlignment="Left" Height="60" Margin="1,0,-1,-133" Stroke="Black" VerticalAlignment="Top" Width="120"/>
            <TextBox x:Name="timersetBox" Margin="-1,-2,1,-59" TextWrapping="Wrap" Text="00" VerticalAlignment="Top" Height="60" BorderBrush="Transparent" FontFamily="Fonts/DIGITAL.TTF#Digital" Foreground="#FF72FB00" FontSize="50" Background="Transparent" TextAlignment="Right" AcceptsReturn="True" SelectionHighlightColor="#000078D7" Width="120"/>
            <Border x:Name="dragBrdr" BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="59" Margin="2,0,0,-59" VerticalAlignment="Top" Width="118"/>
        </Grid>
    </UserControl>
    

    后面的代码

     Point currentPoint;
     Point anchorPoint;
     bool isInDrag;
     private TranslateTransform transform = new TranslateTransform();
     private void Grid_PointerMoved(object sender, PointerRoutedEventArgs e)
     {
         anchorPoint = new Point(300, 200);
         isInDrag = true;
         if (isInDrag)
         {
             var element = sender as FrameworkElement;
             PointerPoint currentPointpointer = e.GetCurrentPoint(null);
             currentPoint = currentPointpointer.Position;
             transform.X += currentPoint.X - anchorPoint.X;
             transform.Y += (currentPoint.Y - anchorPoint.Y);
             this.RenderTransform = transform;
             anchorPoint = currentPoint;
         }
     }
    

    此外,对于从一个点到另一个点的转换,我建议您使用PointAnimation

    【讨论】:

    • 感谢 Sunteen 我现在明白了,我可以看到正在传递的值,感谢它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多