I recently read "Pro Silverlight 2 in C #2008". I personally feel that this book is well written.
The book is printed in color so that readers can see it immediately.ProgramThe final effect of running. Although it is an advanced tutorial,
However, it seems very easy to understand. In some cases, it may be difficult to study it carefully.
Reading a lot of books will inevitably feel unfamiliar if you do not practice them. In combination with the examples in the book,
At the same time, I also exchanged experiences with my friends who are learning Silverlight. Based on the example of the mouse event in chapter 4,
It is adapted into a wuziqi game.
XAML code:
< Usercontrol X : Class = "Wuzi. Page" Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation" Xmlns : X = "Http://schemas.microsoft.com/winfx/2006/xaml"> < Canvas X : Name = "Parentcanvas" Mouseleftbutt background = "Burlywood"> </ Canvas > </ Usercontrol >
C # code:
First, draw a 15 × 15 chessboard.
Public Page () {initializecomponent (); Int Offset = 0;// 15 lines in vertical and horizontal directions For ( Int I = 0; I <= 15; I ++ ){ // Vertical line Line LinEx = New Line (); LinEx. Stroke = New Solidcolorbrush ( Colors . Black); LinEx. x1 = 10 + offset; LinEx. y1 = 10; LinEx. x2 = 10 + offset; LinEx. y2 = 610; parentcanvas. children. add (LinEx ); // Horizontal line Line Liney = New Line (); Liney. Stroke = New Solidcolorbrush ( Colors . Black); Liney. x1 = 10; Liney. y1 = 10 + offset; Liney. x2 = 610; Liney. y2 = 10 + offset; parentcanvas. children. add (Liney ); // Line spacing Offset + = 40 ;}
}
When you click the chessboard, black and white sub-entries are displayed in order.
// Track whether a pawn is dragged Private bool Isdragging = False ; // When a pawn is clicked, the coordinate value relative to the pawn is recorded. Private Point Mouseoffset; // Judge sunspots Private int Num = 0; Private void Canvas_click ( Object Sender, mousebuttoneventargs e ){ // Create a pawn when the mouse is not dragged. If (! Isdragging ){ Int Y; y = num % 2; ellipse = New Ellipse (); // Determine the color of the pawnpiece, Which is black or even. If (Y = 0) {ellipse. Fill = New Solidcolorbrush ( Colors . Black );} Else {Ellipse. Fill = New Solidcolorbrush ( Colors . White );} // Set the pawn size Ellipse. width = 40; ellipse. Height = 40; // Change the Mouse shape of a pawn (hand-shaped) Ellipse. cursor = cursors. hand; // Locate the coordinates of the pawns Based on the mouse position Point Point = E. getposition ( This ); Ellipse. setvalue ( Canvas . Topproperty, point. Y-ellipse. Height/2); ellipse. setvalue ( Canvas . Leftproperty, point. X-ellipse. width/2 ); // Trace the left mouse button Ellipse. mouseleftbuttondown + = ellipse_mousedown; // Add a pawn Parentcanvas. Children. Add (ellipse); num ++ ;}}
Mouse operation tracking
// Press and hold the left mouse button Private void Ellipse_mousedown ( Object Sender, mousebuttoneventargs e ){ // Drag and drop the pawns Isdragging = True ; Ellipse = (ellipse) sender; // Obtain the coordinate value of the relative chess piece. The coordinate value is (0, 0) in the upper left corner of the chess piece. Mouseoffset = E. getposition (ellipse ); // Track other mouse operations Ellipse. mousemove + = ellipse_mousemove; ellipse. mouseleftbuttonup + = ellipse_mouseup; // Capture the mouse position and bind the pawn with the mouse Ellipse. capturemouse ();} // Move the mouse Private void Ellipse_mousemove ( Object Sender, mouseeventargs e ){ If (Isdragging) {ellipse = (ellipse) sender; // Obtain the coordinates of the canvas. Point Point = E. getposition ( This ); // Move the pawns Ellipse. setvalue ( Canvas . Topproperty, point. Y-mouseoffset. Y); ellipse. setvalue ( Canvas . Leftproperty, point. X-mouseoffset. X );}} // Open the left mouse button Private void Ellipse_mouseup ( Object Sender, mousebuttoneventargs e ){ If (Isdragging) {ellipse = (ellipse) sender; // Cancel the mouse operation trail Ellipse. mousemove-= ellipse_mousemove; ellipse. mouseleftbuttonup-= ellipse_mouseup; ellipse. releasemousecapture (); isdragging = False ;}}
So far, the prototype of a wuziqi game has been completed, and other functions can be improved. For example, you can place the pawns neatly and determine whether to win or lose.
For this example, refer to Chapter 4 ■ dependency properties and routed events from apress "Pro Silverlight 2 in C #2008 ".