• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

正确实现Silverlight拖拽功能

武飞扬头像
51CTO
帮助0

Silverlight拖拽功能的实现再实际开发编程中是一个非常重要的基础功能。对于一个开发人员来说,如果想要很好的使用Silverlight来实现相关功能需求,就需要牢固掌握这些基础功能的应用。#t#

下面的示例演示如何在基于 Silverlight 的应用程序中拖放对象。出于安全考虑,不能在应用程序之间拖放对象。因此,说成在 Silverlight 插件区域内"滑动"对象更为准确。但是,术语"拖放"更为人知,因此在此处使用。

Silverlight拖拽功能Xaml脚本:

  1. < UserControl x:Class=
    "DragAndDropSimple.Page" 
  2. xmlns="http://schemas.microsoft.
    com/winfx/2006/xaml/presentation"
       
  3. xmlns:x="http://schemas.
    microsoft.com/winfx/2006/xaml"
       
  4. Width="400" Height="300"> 
  5. < Canvas x:Name="rootCanvas" 
  6. Width="640" 
  7. Height="480" 
  8. Background="Gray" 
  9. > 
  10. < !-- You can drag this 
    rectangle around the canvas. --> 
  11. < Rectangle 
  12. MouseLeftButtonDown=
    "Handle_MouseDown" 
  13. MouseMove="Handle_MouseMove" 
  14. MouseLeftButtonUp="Handle_MouseUp" 
  15. Canvas.Left="30" Canvas.
    Top
    ="30" Fill="Red" 
  16. Width="50" Height="50" /> 
  17. < /Canvas> 
  18. < /UserControl> 

后置代码:

  1. // Global variables used to 
    keep track of the   
  2. // mouse position and whether 
    the object is captured  
  3. // by the mouse.  
  4. bool isMouseCaptured;  
  5. double mouseVerticalPosition;  
  6. double mouseHorizontalPosition;  
  7. public void Handle_MouseDown 
    (object sender, MouseEventArgs args)   
  8. {  
  9. Rectangle item = sender as Rectangle;  
  10. mouseVerticalPosition = args.
    GetPosition(null).Y;  
  11. mouseHorizontalPosition = 
    args.GetPosition(null).X;  
  12. isMouseCaptured = true;  
  13. item.CaptureMouse();  
  14. }  
  15. public void Handle_MouseMove
    (object sender, MouseEventArgs args)   
  16. {  
  17. Rectangle item = sender as Rectangle;  
  18. if (isMouseCaptured)   
  19. {  
  20. // Calculate the current 
    position of the object.  
  21. double deltaV = args.GetPosition(null).
    Y - mouseVerticalPosition;  
  22. double deltaH = args.GetPosition(null).
    X - mouseHorizontalPosition;  
  23. double newTop = deltaV   (double)
    item.GetValue(Canvas.TopProperty);  
  24. double newLeft = deltaH   (double)
    item.GetValue(Canvas.LeftProperty);  
  25. // Set new position of object.  
  26. item.SetValue(Canvas.TopProperty, newTop);  
  27. item.SetValue(Canvas.LeftProperty, newLeft);  
  28. // Update position global variables.  
  29. mouseVerticalPosition = args.
    GetPosition(null).Y;  
  30. mouseHorizontalPosition = args.
    GetPosition(null).X;  
  31. }  
  32. }  
  33. public void Handle_MouseUp(object 
    sender, MouseEventArgs args)   
  34. {  
  35. Rectangle item = sender as Rectangle;  
  36. isMouseCaptured = false;  
  37. item.ReleaseMouseCapture();  
  38. mouseVerticalPosition = -1;  
  39. mouseHorizontalPosition = -1;  

Silverlight拖拽功能的实现方法就为大家介绍到这里啦。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /news/detail/tanhbkfaik
系列文章
更多 icon
同类精品
更多 icon
继续加载