As we all know, there is no button class in silverlight1.0. To implement the button effect, mouseenter, mouseleave, mouseleftdown, mouseleftup, three image images are required: img_name, img_click, and img_over to indicate the button status.
Mouse event correspondence:
Mouseenter corresponds to img_over; mouseleave corresponds to img_name; mouseleftdown corresponds to img_click; mouseleftup corresponds to img_over;
The three images need to be placed in a canvas class. The most important thing is to make the three images istesthit = false, while the istesthit = true of the canvas.
#01 ffffff of the background of the most important canvas (the background color must be set; otherwise, the click event cannot be triggered)
Instance code:
XAML code
<Canvas. Left = "0" Canvas. Top = "64"
Width = "39" Height =" 360"
X: Name = "Pre" IsHitTestVisible = "True"
Background = "#01 FFFFFF">
<Image Source = "SilverlightImg/PrevArrow.png"
X: Name = "Img_Pre"
Width = "36" Height =" 358"
Canvas. Top = "5" Canvas. Left = "-5"
IsHitTestVisible = "False"/>
<Image IsHitTestVisible = "False"
Width = "36" Height =" 358"
Source = "SilverlightImg/PrevArrow_Over.png"
Stretch = "Fill" Canvas. Left = "-5" Canvas. Top = "5"
X: Name = "Img_Pre_Over" Visibility = "Collapsed"/>
<Image IsHitTestVisible = "False"
Width = "36" Height =" 358"
Canvas. Top = "5" Source = "silverlightimg/prevarrow_click.png"
Stretch = "fill" X: Name = "img_pre_click"
Canvas. Left = "-8" visibility = "Collapsed"/>
</Canvas>
Bind event:
This. Next. addeventlistener ("mouseenter", handleprenextmouseenter );
This. Next. addeventlistener ("mouseleftbuttondown", handleprenextmousedown );
This. Next. addeventlistener ("mouseleftbuttonup", handleprenextmouseup );
This. Next. addeventlistener ("mouseleave", handleprenextmouseleave );
Event handling code:
Function handlePreNextMouseDown (sender, eventArgs)
{
Var imgName = "Img _" + sender. Name + "_ Click"
Var imgButton = sender. findName (imgName );
ImgButton. visibility = "Visible ";
ImgName = "Img _" + sender. Name + "_ Over ";
ImgButton = sender. findName (imgName );
ImgButton. visibility = "Collapsed ";
}
Function handleprenextmouseup (sender, eventargs)
{
VaR imgname = "IMG _" + sender. Name + "_ click"
VaR imgbutton = sender. findname (imgname );
Imgbutton. Visibility = "Collapsed ";
Imgname = "IMG _" + sender. Name + "_ over ";
Imgbutton = sender. findname (imgname );
Imgbutton. Visibility = "visible ";
}
Function handlePreNextMouseLeave (sender, eventArgs)
{
HandleMouseLeave (sender, eventArgs );
Var imgName = "Img _" + sender. Name + "_ Click ";
Var imgButton = sender. findName (imgName );
If (imgButton! = Null)
{
ImgButton. visibility = "Collapsed ";
}
}
Function handleMouseEnter (sender, eventArgs)
{
Var imgName = "Img _" + sender. Name + "_ Over ";
Var imgButton = sender. findName (imgName );
ImgButton. visibility = "Visible ";
ImgName = "Img _" + sender. Name;
ImgButton = sender. findName (imgName );
ImgButton. visibility = "Collapsed ";
}
Function handleMouseLeave (sender, eventArgs)
{
Var imgName = "Img _" + sender. Name + "_ Over ";
Var imgButton = sender. findName (imgName );
ImgButton. visibility = "Collapsed ";
ImgName = "Img _" + sender. Name;
ImgButton = sender. findName (imgName );
ImgButton. visibility = "Visible ";
}
This is a small experience summarized in Silverlight development. Let's record it here