模拟视觉拖/放(此代码有什么问题?)
我有一个 WPF 4/VB.net 2010 项目,我正在尝试进行可视化“拖放”(让一个对象在 MouseUp 上跟随鼠标并在 MouseDown 上“粘住”。)我有以下代码:
Private Sub Tile1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Tile1.MouseDown
Tile1.CaptureMouse()
IsDragging = True
End Sub
Private Sub Tile1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles Tile1.MouseMove
If IsDragging = True Then
Dim canvPosToWindow As Point = canv.TransformToAncestor(Me).Transform(New Point(0, 0))
Dim r As Rectangle = TryCast(sender, Rectangle)
Dim upperlimit As Double = canvPosToWindow.Y + (r.Height / 2)
Dim lowerlimit As Double = canvPosToWindow.Y + canv.ActualHeight - (r.Height / 2)
Dim leftlimit As Double = canvPosToWindow.X + (r.Width / 2)
Dim rightlimit As Double = canvPosToWindow.X + canv.ActualWidth - (r.Width / 2)
Dim absmouseXpos As Double = e.GetPosition(Me).X
Dim absmouseYpos As Double = e.GetPosition(Me).Y
If (absmouseXpos > leftlimit AndAlso absmouseXpos < rightlimit) AndAlso (absmouseYpos > upperlimit AndAlso absmouseYpos < lowerlimit) Then
r.SetValue(Canvas.LeftProperty, e.GetPosition(canv).X - (r.Width / 2))
r.SetValue(Canvas.TopProperty, e.GetPosition(canv).Y - (r.Height / 2))
End If
End If
End Sub
Private Sub Tile1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Tile1.MouseUp
Tile1.ReleaseMouseCapture()
IsDragging = False
End Sub
我收到此错误:
NullReferenceException,未将对象引用设置为对象的实例。
在以下每一行中:
Dim upperlimit As Double = canvPosToWindow.Y + (r.Height / 2)
Dim lowerlimit As Double = canvPosToWindow.Y + canv.ActualHeight - (r.Height / 2)
Dim leftlimit As Double = canvPosToWindow.X + (r.Width / 2)
Dim rightlimit As Double = canvPosToWindow.X + canv.ActualWidth - (r.Width / 2)
我做错了什么?
I have a WPF 4/VB.net 2010 project, and I'm trying to do a visual "drag and drop" (having an object follow the mouse on MouseUp and "stick" on MouseDown.) I have the following code behind:
Private Sub Tile1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Tile1.MouseDown
Tile1.CaptureMouse()
IsDragging = True
End Sub
Private Sub Tile1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles Tile1.MouseMove
If IsDragging = True Then
Dim canvPosToWindow As Point = canv.TransformToAncestor(Me).Transform(New Point(0, 0))
Dim r As Rectangle = TryCast(sender, Rectangle)
Dim upperlimit As Double = canvPosToWindow.Y + (r.Height / 2)
Dim lowerlimit As Double = canvPosToWindow.Y + canv.ActualHeight - (r.Height / 2)
Dim leftlimit As Double = canvPosToWindow.X + (r.Width / 2)
Dim rightlimit As Double = canvPosToWindow.X + canv.ActualWidth - (r.Width / 2)
Dim absmouseXpos As Double = e.GetPosition(Me).X
Dim absmouseYpos As Double = e.GetPosition(Me).Y
If (absmouseXpos > leftlimit AndAlso absmouseXpos < rightlimit) AndAlso (absmouseYpos > upperlimit AndAlso absmouseYpos < lowerlimit) Then
r.SetValue(Canvas.LeftProperty, e.GetPosition(canv).X - (r.Width / 2))
r.SetValue(Canvas.TopProperty, e.GetPosition(canv).Y - (r.Height / 2))
End If
End If
End Sub
Private Sub Tile1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Tile1.MouseUp
Tile1.ReleaseMouseCapture()
IsDragging = False
End Sub
I am getting this error:
NullReferenceException, Object reference not set to an instance of an object.
On each of the following lines:
Dim upperlimit As Double = canvPosToWindow.Y + (r.Height / 2)
Dim lowerlimit As Double = canvPosToWindow.Y + canv.ActualHeight - (r.Height / 2)
Dim leftlimit As Double = canvPosToWindow.X + (r.Width / 2)
Dim rightlimit As Double = canvPosToWindow.X + canv.ActualWidth - (r.Width / 2)
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑这个语句使 r “空”而不是一个正确的矩形。
发送者可能不是一个矩形,而可能是一个 WPF-Control(System.Windows.Controls.Control 的某个子类)。
您可以通过在该行上放置一个调试点来轻松地使用 Visual Studio 调试器进行检查,并查看该语句是否计算结果。
祝您的拖放实施顺利:)。
I suspect that this statement makes r "null" and not a proper rectangle.
The sender is probably not a rectangle but probably a WPF-Control (some subclass of System.Windows.Controls.Control).
You can easily check with the Visual Studio debugger, by placing a debug-point on this line and see if this statement evaluates.
Good luck with your drag-drop implementation :).