新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
如何正確實現(xiàn)Silverlight拖拽功能
Silverlight拖拽功能的實現(xiàn)再實際開發(fā)編程中是一個非常重要的基礎(chǔ)功能。對于一個開發(fā)人員來說,如果想要很好的使用Silverlight來實現(xiàn)相關(guān)功能需求,就需要牢固掌握這些基礎(chǔ)功能的應(yīng)用。#t#

網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、重慶小程序開發(fā)公司、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了舟曲免費建站歡迎大家使用!
下面的示例演示如何在基于 Silverlight 的應(yīng)用程序中拖放對象。出于安全考慮,不能在應(yīng)用程序之間拖放對象。因此,說成在 Silverlight 插件區(qū)域內(nèi)"滑動"對象更為準(zhǔn)確。但是,術(shù)語"拖放"更為人知,因此在此處使用。
Silverlight拖拽功能Xaml腳本:
- < UserControl x:Class=
"DragAndDropSimple.Page" - xmlns="http://schemas.microsoft.
com/winfx/2006/xaml/presentation" - xmlns:x="http://schemas.
microsoft.com/winfx/2006/xaml" - Width="400" Height="300">
- < Canvas x:Name="rootCanvas"
- Width="640"
- Height="480"
- Background="Gray"
- >
- < !-- You can drag this
rectangle around the canvas. --> - < Rectangle
- MouseLeftButtonDown=
"Handle_MouseDown" - MouseMove="Handle_MouseMove"
- MouseLeftButtonUp="Handle_MouseUp"
- Canvas.Left="30" Canvas.
Top="30" Fill="Red" - Width="50" Height="50" />
- < /Canvas>
- < /UserControl>
后置代碼:
- // Global variables used to
keep track of the- // mouse position and whether
the object is captured- // by the mouse.
- bool isMouseCaptured;
- double mouseVerticalPosition;
- double mouseHorizontalPosition;
- public void Handle_MouseDown
(object sender, MouseEventArgs args)- {
- Rectangle item = sender as Rectangle;
- mouseVerticalPosition = args.
GetPosition(null).Y;- mouseHorizontalPosition =
args.GetPosition(null).X;- isMouseCaptured = true;
- item.CaptureMouse();
- }
- public void Handle_MouseMove
(object sender, MouseEventArgs args)- {
- Rectangle item = sender as Rectangle;
- if (isMouseCaptured)
- {
- // Calculate the current
position of the object.- double deltaV = args.GetPosition(null).
Y - mouseVerticalPosition;- double deltaH = args.GetPosition(null).
X - mouseHorizontalPosition;- double newTop = deltaV + (double)
item.GetValue(Canvas.TopProperty);- double newLeft = deltaH + (double)
item.GetValue(Canvas.LeftProperty);- // Set new position of object.
- item.SetValue(Canvas.TopProperty, newTop);
- item.SetValue(Canvas.LeftProperty, newLeft);
- // Update position global variables.
- mouseVerticalPosition = args.
GetPosition(null).Y;- mouseHorizontalPosition = args.
GetPosition(null).X;- }
- }
- public void Handle_MouseUp(object
sender, MouseEventArgs args)- {
- Rectangle item = sender as Rectangle;
- isMouseCaptured = false;
- item.ReleaseMouseCapture();
- mouseVerticalPosition = -1;
- mouseHorizontalPosition = -1;
- }
Silverlight拖拽功能的實現(xiàn)方法就為大家介紹到這里啦。
網(wǎng)站欄目:如何正確實現(xiàn)Silverlight拖拽功能
文章源于:http://www.dlmjj.cn/article/coedgpd.html


咨詢
建站咨詢
