' MouseEventArgs'不包含' GetPosition'的定义

时间:2017-01-18 09:53:11

标签: c# wpf visual-studio xaml uwp

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

CS1061' MouseEventArgs'不包含' GetPosition'的定义没有扩展方法' GetPosition'接受第一个类型' MouseEventArgs'可以找到(你错过了使用指令或程序集引用吗?)

我发现这个我在堆栈上使用的示例(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&amp; using Windows.Devices.Input

提前感谢一大堆提供任何帮助。

2 个答案:

答案 0 :(得分:1)

为了更好地支持触摸和着墨,对鼠标事件进行了抽象。这叫做指针。所以你有一个PointerMoved事件

xaml中的

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

    </Grid>

并在代码中

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

答案 1 :(得分:0)

  

但仍然不确定如何获取我的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