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();
}
}
答案 0 :(得分:0)
默认情况下,Silverlight中未实现拖放功能。使用Drag&amp;在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();
}
}