我在UI线程中有一个带有DispatcherTimer的WPF应用程序。
private int TimeLimit = 500;
private DispatcherTimer _timer;
public MainWindow()
{
_timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(TimeLimit) };
_timer.Tick += TimerTick;
_timer.Start();
InitializeComponent();
}
private void TimerTick(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("hh:mm:ss.fff") + " Timer");
}
当我悬停一个按钮,实现Drag逻辑时,我的DispatcherTimer正在按住。
XAML:
<Button Width="200" Height="50" Content="Test" PreviewMouseMove="_MouseMove"/>
代码:
private Point startPoint = new Point();
private void _MouseMove(object sender, MouseEventArgs e)
{
System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("hh:mm:ss.fff") + " MousePosition");
// Get the current mouse position
Point mousePos = e.GetPosition(null);
Vector diff = startPoint - mousePos;
if (e.LeftButton == MouseButtonState.Pressed &&
Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)
{
// Get the dragged Item
Button item = sender as Button;
if (item != null)
{
// Get the data
String contact = (String)item.Content;
// Initialize the drag & drop operation
DataObject dragData = new DataObject("String", contact);
DragDrop.DoDragDrop(item, dragData, DragDropEffects.Move);
}
}
}
我必须使用DispatcherTimer。我如何结交朋友DoDragDrop和DispatcherTimer?