1

XAML

 <Grid x:Name="LayoutRoot" Background="White">
        <Canvas  x:Name="VideoCanvas" Width="800" Height="600" AllowDrop="True" Background="RosyBrown" HorizontalAlignment="Center">
            <StackPanel Drop="VideoCanvas_Drop" AllowDrop="True" Name="CommandBar" Orientation="Horizontal" Background="Beige" Height="39" Width="783" Margin="5" HorizontalAlignment="Center" Canvas.Left="3">
                <Button Name="PlayMovie" Background="AntiqueWhite" Content="Play" FontWeight="Bold" Click="PlayMovie_Click" Margin="50,5,0,5" Width="100"/>
                <Button Name="PauseMovie" Background="AntiqueWhite" Content="Pause" FontWeight="Bold" Click="PauseMovie_Click" Margin="50,5,0,5" Width="100"/>
                <Button Name="VideoStop" Background="AntiqueWhite" Content="Stop" FontWeight="Bold" Click="VideoStop_Click" Margin="50,5,0,5" Width="100" />
            </StackPanel>
            <MediaElement x:Name="mediaEl" Canvas.Top="150" Width="800" Height="450" AllowDrop="True" AutoPlay="True" />
        </Canvas>
    </Grid>

C#

private void VideoCanvas_Drop(object sender, DragEventArgs e)
        {
            DragEventArgs dr = e as DragEventArgs;

            if (dr.Data == null)
                return;
            IDataObject data = dr.Data;
            FileInfo[] file = data.GetData(DataFormats.FileDrop) as FileInfo[];
            if (file.Count() > 0)
                mediaEl.SetSource(file[0].OpenRead());
            if (mediaEl.CurrentState != MediaElementState.Playing)
            {
                mediaEl.Play();
            }
        }
4

1 回答 1

0

Silverlight 中默认不实现拖放。要在 Silverlight 中使用拖放功能,您必须使用Silverlight Toolkit中的 DragDropTargets (PanelDragDropTarget, ListBoxDragDropTarget,...) 。

XAML

<Grid x:Name="LayoutRoot" Background="White">
        <Canvas  x:Name="VideoCanvas" Width="800" Height="600" AllowDrop="True" Background="RosyBrown" HorizontalAlignment="Center">
            <toolkit:PanelDragDropTarget AllowDrop="True" Drop="VideoCanvas_Drop">
              <StackPanel Name="CommandBar" Orientation="Horizontal" Background="Beige" Height="39" Width="783" Margin="5" HorizontalAlignment="Center" Canvas.Left="3">
                  <Button Name="PlayMovie" Background="AntiqueWhite" Content="Play" FontWeight="Bold" Click="PlayMovie_Click" Margin="50,5,0,5" Width="100"/>
                  <Button Name="PauseMovie" Background="AntiqueWhite" Content="Pause" FontWeight="Bold" Click="PauseMovie_Click" Margin="50,5,0,5" Width="100"/>
                  <Button Name="VideoStop" Background="AntiqueWhite" Content="Stop" FontWeight="Bold" Click="VideoStop_Click" Margin="50,5,0,5" Width="100" />
              </StackPanel>
            </toolkit:PanelDragDropTarget>
            <MediaElement x:Name="mediaEl" Canvas.Top="150" Width="800" Height="450" AllowDrop="True" AutoPlay="True" />
        </Canvas>
    </Grid>

C#

private void VideoCanvas_Drop(object sender, Microsoft.Windows.DragEventArgs e)
        {
            DragEventArgs dr = e as DragEventArgs;

            if (dr.Data == null)
                return;
            IDataObject data = dr.Data;
            FileInfo[] file = data.GetData(DataFormats.FileDrop) as FileInfo[];
            if (file.Count() > 0)
                mediaEl.SetSource(file[0].OpenRead());
            if (mediaEl.CurrentState != MediaElementState.Playing)
            {
                mediaEl.Play();
            }
        }
于 2012-06-14T06:51:12.330 回答