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

CS1061'MouseEventArgs'不包含'GetPosition'的定义,并且找不到扩展方法'GetPosition'接受类型为'MouseEventArgs'的第一个参数(您是否缺少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.Inputusing Windows.Devices.Input

预先感谢您提供的任何帮助。

最佳答案

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


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

<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

10-07 15:56