簡體   English   中英

“MouseEventArgs”不包含“GetPosition”的定義

[英]'MouseEventArgs' does not contain a definition for 'GetPosition'

我確定這是一個非常簡單的方法,但我正在開發一個 C# UWP 應用程序,並試圖制作一個可拖動的用戶控件,但在我的鼠標事件中,我收到以下錯誤:

CS1061“MouseEventArgs”不包含“GetPosition”的定義,並且找不到接受“MouseEventArgs”類型的第一個參數的擴展方法“GetPosition”(您是否缺少 using 指令或程序集引用?)

我發現這個示例我在堆棧上使用( 拖動 WPF 用戶控件)並且之前使用過鼠標按下事件在 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

提前感謝一堆提供的任何幫助。

為了更好地支持觸摸和墨跡書寫,對鼠標事件進行了抽象。 這稱為指針。 所以你有一個 PointerMoved 事件

在 xaml 中:

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

    </Grid>

並在代碼中

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

但我仍然不確定如何獲取我的 var 點並將其轉換為我的 MainPage 上用戶控件的新位置

正如@Dave Smits 所說,您需要一個Pointer事件句柄。 有關句柄指針輸入的更多詳細信息,請參閱此文檔 我認為你困惑的是這段代碼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

暫無
暫無

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

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