Sdílet prostřednictvím


Handling Drag-and-Drop Events for Macintosh

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Drag-and-drop events in Silverlight have some differences depending on the platform. In particular, drag-and-drop input actions are reported differently on a Macintosh computer. Therefore, Silverlight-based applications that run on a Macintosh must use different techniques to process a drag-and-drop action. This topic describes specific techniques and workarounds for handling drag-and-drop events on a Macintosh in a Silverlight-based application.

Safari for Macintosh

To handle drag-and-drop events for applications hosted by Safari browser on the Macintosh, you must forward the relevant HTML DOM events to the specific handler in Silverlight. This is a requirement because the Safari plug-in model does not provide these events to plug-ins, such as Silverlight.

The following is an excerpt of the embed tag you would use in Safari to declare drag-and-drop event handlers for the HTML element that instantiates the Silverlight plug-in:

<embed id="AgControl1"
...
  ondragenter="handleDragEnter(event)"
  ondragover="handleDragOver(event)"
  ondragleave="handleDragLeave(event)"
  ondrop="handleDropEvent(event)" 
.../>

The following is an example of a JavaScript function named handleDragEnter that handles the HTML DOM dragEnter event in Safari. The getElementById call obtains the Silverlight plug-in instance. The event is forwarded to the Silverlight plug-in for processing.

function handleDragEnter(oEvent) {
  // Prevent default operations in DOM
  oEvent.preventDefault();
  agControl = document.getElementById("AgControl1");
  var flag = agControl.dragEnter(oEvent);
    // If handled, then stop propagation of event in DOM
    if (flag) oEvent.stopPropagation();
}

You should write one handler for the following drag-and-drop-related HTML DOM events: dragEnter, dragLeave, dragOver, and drop. Each of the event handlers follow the same basic pattern as the handleDragEnter function. For example, you would change the name accordingly (line #1) and you would forward the event to the appropriate event handler in Silverlight (line #5). If necessary, you can add your own application-specific logic.

Firefox

The Firefox browser for Mac OS X restricts object-level access to the drop event. Therefore, the forwarding technique described for Safari might not work for Firefox.