如题所述
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ææ½åè½çå®ç°æ¹æ³å°±ä¸ºå¤§å®¶ä»ç»å°è¿éå¦ã
温馨提示:答案为网友推荐,仅供参考