本文介绍了我想从一个Silverlight的UIElement拖动图像并将其放置在桌面的用户或Windows资源管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含图像的Silverlight控件。我希望用户能够拖动出来的图像Silverlight应用程序,并把它的任何地方,他们将能够删除的图像。例如,桌面或PowerPoint幻灯片或Word文档。我已阅读一切迄今说,它不能做,但我发现很难相信。我很新的Silverlight和RIA开发,因此任何帮助将非常感激。



下面是我的winform形式的代码示例,但从未启动拖动

 的String [] = ASTRING {}的ImagePath; 
数据对象的数据=新的DataObject(DataFormats.FileDrop,ASTRING);
data.SetData(DataFormats.StringFormat,的ImagePath);
的DoDragDrop(数据,DragDropEffects.Copy);


解决方案

好麻烦的是,在Silverlight拖动操作没有按'T有浏览器(设计)外简单地访问任何东西。根据用户的设置,你甚至必须得到明确许可的剪贴板操作和沙箱临时文件存储。这的确听起来像一个任务更适合WPF应用程序(也许 Web部署)或其他一些桌面应用技术



然而,正在这里说有一些事情你可以尝试/考虑:




  1. 走出布劳尔的&P / Invoke的Silverlight的/ JavaScript的/ ActiveX技术组合在Internet Explorer

  2. 的Silverlight 5托管(听说P /调用将支持Silverlight的时候5出来)

  3. Silverlight的连接到Web服务在同一台计算机(疯狂上运行,但你没有问不疯狂,你自找的可能的话)



我不是很熟悉Win32 API中拖放所以这将需要大量的研究和实验我之前。可以证实,这是甚至有可能(我已经可以告诉你,这是不实际的)



修改:基于额外信息你提供的关于我怀疑这是可以做到的,你尝试什么的问题。首先,您使用WPF或WinForms的?我以为WPF但你的评论人说的WinForms。我不是很熟悉WPF拖/放操作,不过话说看着吧,我想你的代码是在正确的道路上。我创建了一个WPF应用程序,并启动了的KeyDown 活动期间的拖累。这意味着鼠标按钮不一定按下。如果我启动了的DragDrop ,而按钮的的下降,它的工作。如果我开始当鼠标按键的不是的下来,我只好推下鼠标按钮,拖动操作将开始(这是出乎意料的,因为我认为鼠标必须已经下降)。如果我按下鼠标关闭应用程序之外,则给了WPF应用程序焦点(ALT + Tab键),然后启动的DragDrop 当鼠标按键仍下降,但没'将不起作用。我到了 MouseDevice 参考,并检查了 LeftButton 属性和状态已显示为释放,即使按钮仍然被按住。看来这里的关键是这样的拖/放内部鼠标状态交互。您可能需要找到一种方法来设置鼠标的状态(与UI自动化API可能?)。在这一点上应该是非常明显,这整个事情是一个黑客(尽管它可能是能够得到它以某种方式工作)。


I have a Silverlight control containing an image. I want the user to be able to drag the image out of the Silverlight application and drop it anywhere they would be able to drop an image. For example, to the Desktop or to a PowerPoint slide or Word document. Everything that I have read thus far says it cannot be done but I find that hard to believe. I'm very new to Silverlight and RIA development so any help would be much appreciated.

Below is the code sample in my WinForm Form but the drag never starts.

string[] aString = { imagePath };
DataObject data = new DataObject(DataFormats.FileDrop, aString);
data.SetData(DataFormats.StringFormat, imagePath);
DoDragDrop(data, DragDropEffects.Copy);
解决方案

Well the trouble is that a drag operation in Silverlight doesn't have simple access to anything outside the browser (by design). Depending on the user's settings you even have to get explicit permission for clipboard operations and sandboxed temporary file storage. This really sounds like a task better suited to a WPF application (perhaps with web deployment?) or some other desktop application technology.

However, that being said here are some things you could try/consider:

  1. Silverlight/Javascript/ActiveX combination hosted in Internet Explorer
  2. Silverlight 5 "Out of Brower" & P/Invoke (I heard P/Invoke will be supported when Silverlight 5 comes out)
  3. Silverlight connecting to a web service running on the same computer (crazy, but you didn't ask for "not crazy", you asked for possible)

I am not very familiar with drag and drop in the Win32 API so it would take a lot of research and experimentation before I could confirm that this was even possible (and I can already tell you it isn't practical).

Edit: Based on the extra information you provided about the question I suspect it is possible to do what you are attempting. First, are you using WPF or WinForms? I assume WPF but one of your comments says WinForms. I wasn't very familiar with WPF drag/drop operations, but having looked into it, I think your code is on the right path. I created a WPF application and initiated a drag during a KeyDown event. This meant that the mouse button was not necessarily pressed. If I initiated the DragDrop while the button was down it worked. If I initiated while the mouse button wasn't down, I had to push the mouse button down and the drag operation would start (this was unexpected since I assumed the mouse would have to already be down). If I pressed the mouse down outside the application, then gave the WPF app focus (ALT+Tab), then initiated the DragDrop while the mouse button was still down, it didn't work. I got a reference to the MouseDevice and checked the LeftButton property, and the state was showing as "Released" even though the button was still being held down. It seems the key here is the way drag/drop interacts with internal mouse state. You might have to find a way to set the mouse state (maybe with the UI Automation API?). At this point it should be painfully obvious that this whole thing is a hack (even though it is probably possible to get it to work somehow).

这篇关于我想从一个Silverlight的UIElement拖动图像并将其放置在桌面的用户或Windows资源管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 16:51