2012-06-14 26 views
1

XAMLDrop事件在Silverlight沒有觸發帆布/ StackPanel中

<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

拖放默認情況下不會在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(); 
      } 
     } 
+0

嗨Jehof, 感謝您的快速回復。但是令人傷心的是,如果我從堆疊面板本身拖放任何物品,但是如果從桌面上放下任何媒體項目,它就不會觸發evnet。 再次感謝 – Chitta

+0

做工精細在Mozilla 11.0,但不是在IE 8,鉻 – Chitta

+0

我瘋了:( 它在IE 8.0.6001.18702,但不是在IE工作正常8.0.7601.17514。 難道我做錯了什麼?如果不是那麼silverlight是如何跨瀏覽器:( – Chitta