One of the new features of Silverlight 4 is drag-and-drop support. It is easy to use.
You can manage all drag-and-drop operations with three events.
1: // drag? Put? After I complete my tasks? Parts t
2: media.Drop += doDrop;
3: // drag? Put? Why? Parts t
4: media.DragEnter += doDragEnter;
5: // drag? Put? Jieáshu? Hour + thing? Parts t
6: media.DragLeave += doDragLeave;
7:
In the drop event. You can also drag and drop E. Data to the Silverlight file set and use the following code to get the file set:
1: IDataObject dataObject = e.Data as IDataObject;
2: FileInfo[] files = dataObject.GetData( DataFormats.FileDrop ) as FileInfo[];
3:
After obtaining files, I can use recursion to list all files:
1: foreach ( FileInfo file in files )
2: {}
3:
After obtaining fileinfo, we can drag and drop the extension of the file to perform corresponding operations. The following example shows how to drag and drop a XAML file and convert it into a uielement:
1: if ( file.Extension.Equals( ".xaml" ) )
2: {
3: string contents;
4: using ( Stream stream = file.OpenRead() )
5: {
6: using ( StreamReader reader = new StreamReader( stream ) )
7: {
8: contents = reader.ReadToEnd();
9: }
10: }
11: UIElement uiElement = XamlReader.Load( contents ) as UIElement;
12: this.xamlViewer.Children.Add( uiElement );
13: }
14:
The operation is so simple!