gpt4 book ai didi

wpf - WPF DoDragDrop导致控件动画停止

转载 作者:行者123 更新时间:2023-12-04 23:48:32 26 4
gpt4 key购买 nike

这里是场景(简化):我在Window上有一个控件(比如说一个Rectangle)。我迷上了MouseMove事件,以使其启动拖放操作。然后在MouseDown事件中进行动画处理,向右移动50个像素。但是,当我将鼠标向下按住Rectangle时,控件将移动大约一个像素,然后暂停。只有当我移动鼠标时,动画才会继续。有谁知道为什么以及如何解决这个问题?非常感谢!!

这是重现此问题的源代码:

public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}

private void rectangle1_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
DragDrop.DoDragDrop(this, new DataObject(), DragDropEffects.Copy);
}
}

private void rectangle1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
ThicknessAnimation animation = new ThicknessAnimation();
Thickness t = rectangle1.Margin;
t.Left += 50;
animation.To = t;
animation.Duration = new Duration(TimeSpan.FromSeconds(0.25));
rectangle1.BeginAnimation(Rectangle.MarginProperty, animation);
}
}


如果您想要Window1.xaml:

<Window x:Class="DragDropHaltingTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Rectangle Margin="12,12,0,0" Name="rectangle1" Stroke="Black" Fill="Blue" Height="29" HorizontalAlignment="Left" VerticalAlignment="Top" Width="31" MouseMove="rectangle1_MouseMove" MouseLeftButtonDown="rectangle1_MouseLeftButtonDown" />
</Grid>

最佳答案

我偶然发现了同样的问题。我的拖放源沿动画路径移动。如果我将鼠标放在拖动源的路径上并保持按下鼠标左键,则当源触摸鼠标时动画将停止。当我释放鼠标按钮或移动鼠标时,动画将继续。 (有趣的是,如果我打开Windows Task Manager并刷新了其定期处理列表,动画还将继续!)

情况分析

据我了解,WPF动画在CompositionTarget.Rendering事件中进行了更新。在正常情况下,每次屏幕刷新时都会触发,这可能是每秒60次。就我而言,当拖动源在鼠标下方移动时,它会触发MouseMove事件。在该事件处理程序中,我调用DragDrop.DoDragDrop。此方法将阻塞UI线程,直到拖放完成为止。 UI线程输入DragDrop.DoDragDrop后,将不触发CompositionTarget.Rendering。从某种意义上讲,触发事件的线程在执行拖放操作时被阻塞是可以理解的。但!如果您移动鼠标(也许其他输入也可以解决问题),则会触发MouseMove事件。它在仍被拖放操作阻止的同一UI线程中触发。触发并处理MouseMove之后,CompositionTarget.Rendering继续在仍被“阻止”的UI线程中定期触发。

我从下面两个堆栈跟踪的比较中收集了这一信息。我根据对堆栈框架含义的理解不足将其分类。第一个堆栈跟踪来自我的MouseMove事件处理程序,而没有拖放操作处于活动状态。这是“通常的情况”。


C)特定于事件的处理(鼠标事件)
System.Windows.Input.MouseEventArgs.InvokeEventHandler

System.Windows.Input.InputManager.HitTestInvalidatedAsyncCallback
B)消息发送
System.Windows.Threading.ExceptionWrapper.InternalRealCall

MS.Win32.HwndSubclass.SubclassWndProc
MS.Win32.UnsafeNativeMethods.DispatchMessage
A)应用程序主循环
System.Windows.Threading.Dispatcher.PushFrameImpl

System.Threading.ThreadHelper.ThreadStart


第二个堆栈跟踪是在拖放操作期间从我的CompositionTarget.Rendering事件处理程序中获取的。框架组A,B和C与上述相同。


F)特定于事件的处理(渲染事件)
System.Windows.Media.MediaContext.RenderMessageHandlerCore
System.Windows.Media.MediaContext.AnimatedRenderMessageHandler
E)消息分发(与B相同)
System.Windows.Threading.ExceptionWrapper.InternalRealCall

MS.Win32.HwndSubclass.SubclassWndProc
D)拖放(从我的事件处理程序中开始)
MS.Win32.UnsafeNativeMethods.DoDragDrop

System.Windows.DragDrop.DoDragDrop
C)特定于事件的处理(鼠标事件)
B)消息发送
A)应用程序主循环


因此,WPF在消息分派(B)中正在运行消息分派(E)。这说明了在UI线程上调用DragDrop.DoDragDrop并阻塞该线程的可能性,而我们仍然能够在同一线程上运行事件处理程序。我无法想象为什么内部消息分发从拖放操作开始执行常规CompositionTarget.Rendering事件(会更新动画)开始就没有连续运行。

可能的解决方法

触发额外的MouseMove事件

一种解决方法是,如VlaR建议的那样,在执行DoDragDrop时触发鼠标移动事件。他的链接似乎假设我们正在使用Forms。对于WPF,细节有所不同。在solution on the MSDN forums之后(由原始张贴者?),此代码可以解决问题。我修改了源代码,以便它可以同时在32位和64位上工作,并且还避免了计时器在触发之前发生GC的罕见机会。

[DllImport("user32.dll")]
private static extern void PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

private static Timer g_mouseMoveTimer;

public static DragDropEffects MyDoDragDrop(DependencyObject source, object data, DragDropEffects effects, Window callerWindow)
{
var callerHandle = new WindowInteropHelper(callerWindow).Handle;
var position = Mouse.GetPosition(callerWindow);
var WM_MOUSEMOVE = 0x0200u;
var MK_LBUTTON = new IntPtr(0x0001);
var lParam = (IntPtr)position.X + ((int)position.Y << (IntPtr.Size * 8 / 2));
if (g_mouseMoveTimer == null) {
g_mouseMoveTimer = new Timer { Interval = 1, AutoReset = false };
g_mouseMoveTimer.Elapsed += (sender, args) => PostMessage(callerHandle, WM_MOUSEMOVE, MK_LBUTTON, lParam);
}
g_mouseMoveTimer.Start();
return DragDrop.DoDragDrop(source, data, effects);
}


使用此方法代替 DragDrop.DoDragDrop将使动画继续进行而不停止。但是,似乎没有使 DragDrop尽快了解正在进行的拖动。因此,您有冒险一次输入多个拖动操作的风险。调用代码应防止此类攻击:

private bool _dragging;

source.MouseMove += (sender, args) => {
if (!_dragging && args.LeftButton == MouseButtonState.Pressed) {
_dragging = true;
MyDoDragDrop(source, data, effects, window);
_dragging = false;
}
}


还有另一个小故障。出于某种原因,在开头描述的情况下,只要按下鼠标左键,鼠标没有移动且拖放源在上方移动,鼠标光标就会在正常箭头光标和拖动光标之间闪烁。光标。

仅在鼠标真正移动时才允许拖动

我尝试过的另一种解决方案是在开始所述的情况下禁止从开始拖放。为此,我连接了一些Window事件以更新状态变量,所有代码都可以压缩到此初始化例程中。

/// <summary>
/// Returns a function that tells if drag'n'drop is allowed to start.
/// </summary>
public static Func<bool> PrepareForDragDrop(Window window)
{
var state = DragFilter.MustClick;
var clickPos = new Point();
window.MouseLeftButtonDown += (sender, args) => {
if (state != DragFilter.MustClick) return;
clickPos = Mouse.GetPosition(window);
state = DragFilter.MustMove;
};
window.MouseLeftButtonUp += (sender, args) => state = DragFilter.MustClick;
window.PreviewMouseMove += (sender, args) => {
if (state == DragFilter.MustMove && Mouse.GetPosition(window) != clickPos)
state = DragFilter.Ok;
};
window.MouseMove += (sender, args) => {
if (state == DragFilter.Ok)
state = DragFilter.MustClick;
};
return () => state == DragFilter.Ok;
}


调用代码将如下所示:

public MyWindow() {
var dragAllowed = PrepareForDragDrop(this);
source.MouseMove += (sender, args) => {
if (dragAllowed()) DragDrop.DoDragDrop(source, data, effects);
};
}


这可以解决容易开始重现的情况,在这种情况下开始拖动是因为在按下鼠标左键时动画将拖动源移动到了固定的鼠标光标上。但是,这不能避免根本问题。如果单击拖动源并移动得很少,以至仅触发了一个鼠标事件,动画将停止。幸运的是,这在实际实践中不太可能发生。此解决方案的优点是不直接使用Win32 API,也不必涉及后台线程。

从后台线程调用 DoDragDrop

不起作用必须从UI线程调用 DragDrop.DoDragDrop并将其阻止。调用使用分派器调用 DoDragDrop的后台线程无济于事。

关于wpf - WPF DoDragDrop导致控件动画停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2130128/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com