- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的应用程序有一个简单的功能,它连接到 Excel 并在它们之间进行拖放操作。具体来说,我只是从我的应用程序中获取一些文本值,将它们拖到 Excel 中,然后放下。
这在 90% 的时间都有效,但奇怪的是,在某些时候,我的应用程序只是卡住。我附加调试器并暂停执行,它卡在 DragDrop.DoDragDrop
- 此函数永远不会返回,我的应用程序将永远挂起。
有没有办法确保 DoDragDrop
可以返回?或者某种超时?这种情况只有在我将数据放入 Excel 时才会发生,因此据我所知,数据放入已经完成,该函数应该会在我的应用程序中返回。
这是我使用的代码:
DragDrop.DoDragDrop(sender as DependencyObject, draggable.GetDragDropString(), DragDropEffects.Copy);
GetDragDropString()
只是一个函数,它返回要在 Excel 中放置的数据字符串。sender
只是我正在拖动的 UI 组件。像网格、编辑框、文本框等。可以是其中任何一种。
感谢您的帮助!
编辑:由于在某些情况下 DragDrop.DoDragDrop
返回存在问题,也许有人可以帮助编写适当的超时?我已经尝试启动一个新的 Thread
并让它超时,这在简单的情况下以及线程内的工作不需要 UI 资源时有效。但是,当我在超时的新线程中调用 DoDragDrop
时,它会抛出异常,指出该线程无法访问该对象,因为另一个线程拥有它。所以我需要在同一个线程中调用这个函数。所以本质上,当此函数无法在特定时间内返回时,我需要在 UI 线程上超时。
最佳答案
我认为以下内容应该可以完成这项工作,但我会在进行过程中对其进行分解
public class DragDropTimer
{
private delegate void DoDragDropDelegate();
private System.Timers.Timer dragTimer;
private readonly int interval = 3000;
public DragDropTimer()
{
dragTimer = new System.Timers.Timer();
dragTimer.Interval = interval;
dragTimer.Elapsed += new ElapsedEventHandler(DragDropTimerElapsed);
dragTimer.Enabled = false;
dragTimer.AutoReset = false;
}
void DragDropTimerElapsed(object sender, ElapsedEventArgs e)
{
Initiate();
}
public void Initiate()
{
// Stops UI from freezing, call action async.
DoDragDropDelegate doDragDrop = new DoDragDropDelegate(DragDropAction);
// No async callback or object required
doDragDrop.BeginInvoke(null, null);
}
private void DragDropAction()
{
dragTimer.Enabled = false;
// Do your work here. or do work before and disable your timer upto you.
}
}
所以我们有一个基本类DragDropTimer
。我们在构造函数上设置了我们想要的间隔,如果您愿意,您可能想要更改它,我们在计时器结束时调用 DragDropTimerElapsed
。
Initiate
是开始拖动所需的函数,它创建了一个简单的委托(delegate),我们要求它执行 DragAction
步骤,这是您执行所有操作的地方工作并且定时器被禁用。只有当拖放成功时,您才可以选择禁用计时器。如果计时器超时,我们会再次调用 Initiate
重新开始。
关于c# - 在 Excel 中执行拖放操作时 DragDrop.DoDragDrop 不返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35157904/
在Wpf中拖动期间,如何更改鼠标光标(或使用装饰器)以指示droptarget将不接受拖动的项目? 我曾尝试在e.Effects = DragDropEffects.None事件期间设置DragEnt
我真的很难在 Silverlight 4 中获得一个简单的拖放示例。 这是我所拥有的: XAML 代码隐藏 public partial c
我找不到将文件放入富文本框后会发生的事件。 *当您将文件放入 Rtb 时,如果您下载了从 richtextbox 派生的用户控件,则图标的图像将复制到控件上,此控件有一些方法可以操作 RTF,其中包括
System.InvalidOperationException: DragDrop registration did not succeed. ---> System.Threading.Threa
我们在 Visual Studio 解决方案中有一些安装项目(其他项目类型 -> 安装和部署 -> 安装项目)。该项目有另一个库类型项目,其安装类名为 InstallationCore,如项目输出。在
在 WPF 中,我想使用右键单击来启动拖放。类似于 Windows 资源管理器,右键单击,稍微移动,你会得到装饰器来指示操作中的拖放。不要移动,松开按钮时会出现上下文菜单。 我已经启动了这一切,但是当
情况: 我有一个使用 angular-dragdrop 的应用程序在三个单独的列表之间移动项目。 拖放工作正常。但我还需要对同一列表中的项目重新排序。 我在文档中看到也可以在 onDrop 上检索索引
我正在尝试编写一些代码以允许我的应用程序的用户拖放 DataGridView 中的行以重新排序。问题是,被拖动的行在被拖放时消失了——所以拖放的效果只是删除该行。这是我的代码: private R
我已经使用以下方法在列表框中成功实现了拖放: Bea Stollnitz - 如何在数据绑定(bind)的 ItemsControl 之间拖放项目: http://www.zagstudio.com/
我有一个带有用于拖放文件的按钮的 C# 应用程序。我可以从我的桌面上获取 6 个文件并将其放到按钮上并让它处理这 6 个文件。 但是,当我从 DragDrop 事件启动线程并将文件路径传递到从 Dra
该方法连续执行两次,没有明显的理由这样做。它发生在 VS2010 Express (4.0) 和 VS2008 (3.5) 中。 public GUI() { InitializeCompon
我正在尝试以 wpf 形式实现拖放功能。目的是通过拖拽实现用户控件的复制(类似于windows 7文件夹复制使用ctrl + Mouseleftbutton拖放)。当 ctrl 键没有按下时,drop
我在 Canvas 上有一个拖放操作,当一个对象被拖入和拖出它时,它应该做一些事情。我的问题是 DragEnter/DragLeave 事件在鼠标将对象移动到它上面时不断触发,而不仅仅是在进入/退出时
我使用这个组件来处理拖放文件 http://melander.dk/delphi/dragdrop unit DragThread; interface uses Classes,DragDrop
我正在寻找一种方法来确定在发生 DragDrop 事件后拖动了 toolStrip 中的哪个项目,我想要做的就是为工具条中的每个项目创建一个具有不同案例的切换案例,但我似乎无法找到一种比较它们的方法。
我有以下代码,我试图在其中实现拖放功能: public Location_Alert() { InitializeComponent(); #region Fo
从过去几个月开始,我就开始研究它。我在处理拖放事件时遇到问题。 我有两个并排放置的网格,我正在执行从左侧网格(网格 A)到右侧网格(网格 B)的拖放操作。我在 Grid B 上同时使用了 Before
我有一个固定位置的 CssLayout 和一个 DDVerticalLayout 子组件。我根据用户交互动态填充此垂直布局以创建 ListView 。当列表变得比显示区域大时,我遇到了问题。它可以正确
我使用 DragDrop.DoDragDrop 将文件从我的 C# WPF 应用程序复制到 Windows 资源管理器。但是,文件的实际复制导致我的 WPF 应用程序卡住,直到复制完成。此复制操作可能
我正在尝试 TDropFileTarget Melander 的 DragDrop 的组成部分套房。目标是在拖放文件后执行某些任务。另外,如果在处理过程中出现问题,我希望收到异常。 似乎 OnDrop
我是一名优秀的程序员,十分优秀!