如何正确实现Silverlight拖拽功能
1个回答
2015-02-03 · 知道合伙人影视综艺行家
关注
展开全部
Silverlight拖拽功能的实现再实际开发编程中是一个非常重要的基础功能。对于一个开发人员来说,如果想要很好的使用Silverlight来实现相关功能需求,就需要牢固掌握这些基础功能的应用。
Silverlight跨平台实现技巧总结
Silverlight开发环境相关前提要素总结
Silverlight加载界面实现方法解析
Silverlight右键应用技巧分享
SilverLight拖动具体实现方式介绍
下面的示例演示如何在基于 Silverlight 的应用程序中拖放对象。出于安全考虑,不能在应用程序之间拖放对象。因此,说成在 Silverlight 插件区域内"滑动"对象更为准确。但是,术语"拖放"更为人知,因此在此处使用。
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拖拽功能的实现方法就为大家介绍到这里啦。
Silverlight跨平台实现技巧总结
Silverlight开发环境相关前提要素总结
Silverlight加载界面实现方法解析
Silverlight右键应用技巧分享
SilverLight拖动具体实现方式介绍
下面的示例演示如何在基于 Silverlight 的应用程序中拖放对象。出于安全考虑,不能在应用程序之间拖放对象。因此,说成在 Silverlight 插件区域内"滑动"对象更为准确。但是,术语"拖放"更为人知,因此在此处使用。
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拖拽功能的实现方法就为大家介绍到这里啦。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询