Nibblestutotials.net tutorial-button advanced of blend & silverlight1 Series

Source: Internet
Author: User

Source files: Part1 and Part2

Creating multiple buttons: creating the key class

This section describes how to create multiple buttons by copying the same XAML file. We will use a file named key. XAML to create a digital keyboard.

We will start the tutorial with the provided images and animations. If you have any questions about drawing and making animations in blend, please refer to the buttons basics section.

To complete this tutorial, you need to install the September preview version of expression blend 2 and Visual Studio 2005, orcas Beta or later.

  1. Open the provided project file
    Use blend to open the provided project file:
    A. Select a File> open a project...
    B. Browse to your project folder.
    C. Double-click the keyboard. csproj file.
    In this project, you will findProgramPay attention to the following two files:
    Scene. XAML: this file is an empty file. We will use JavaScript to create buttons instead of blend.
    Key. XAML: This is the image required by the keyboard. Note that this XAML file has two timelines: mouseenter and mouseleftbuttondown. They willCode.
  2. Create a new Javascript file (key. JS)
    A. in the project panel file area, right-click the project name and select edit in Visual Studio.
    B. in Visual Studio, right-click the project name and choose add> new project... In the Add new project dialog box, select a JScript file. And name it key. js.
    C. Click Add. The new Javascript file will be opened and in the pendingediting state.
  3. Add Constructor
    A. Add the following lines of code: Keyboard. Key =   Function (Control, target, key, x, y)
    {
    This . Target = Target;
    This . Key = Target. findname ( " Keytext " );
    This . Mouseenter = Target. findname ( " Mouseenter " );
    This . Mousedown = Target. findname ( " Mouseleftbuttondown " );
    }

    These code lines create a buttonobject (this.tar get) that represents a text block (this. Key) of letters or numbers and an animated reference that defines the State of two buttons (this. mouseenter and this. mousedown ).
    Add the following code after the code referenced by this. mousedown:This. Target. cursor= "Hand";

    This line moves the mouse over the button.This. Target ["Canvas. Top"]=Y;
    This. Target ["Canvas. Left"]=X;

    The two rows change the button position based on the parameter passed by the value.This. Key. Text=Key;

    The text of the last line to change the button.
    Finally, we add event listeners for each status. This . Target. addeventlistener ( " Mouseenter " , Silverlight. createdelegate ( This , This . Handlemouseenter ));
    This . Target. addeventlistener ( " Mouseleave " , Silverlight. createdelegate ( This , This . Handlemouseleave ));
    This . Target. addeventlistener ( " Mouseleftbuttondown " , Silverlight. createdelegate ( This , This . Handlemousedown ));
    This . Target. addeventlistener ( " Mouseleftbuttonup " , Silverlight. createdelegate ( This , This . Handlemouseup ));

    Now, each status of the button has a function to be triggered when a corresponding event occurs with the mouse. These functions will be defined in the next step.
    Reference Image

  4. Add event handle
    A. Add the following code: Keyboard. Key. Prototype =
    {
    Handlemouseenter: Function (Sender, eventargs)
    {
    This . Mouseenter. Begin ();
    },
    Handlemouseleave: Function (Sender, eventargs)
    {
    This . Mouseenter. Stop ();
    },
    Handlemousedown: Function (Sender, eventargs)
    {
    This . Mousedown. Begin ();
    },
    Handlemouseup: Function (Sender, eventargs)
    {
    This . Mousedown. Stop ();
    }
    }

    These events only start and stop the timeline we created in blend. Using this model gives developers and designers greater flexibility.
    Image participation

  5. Add reference to key. js in HTML
    Do not forget to publish to default.html in the following line:<Script Type="Text/JavaScript"SRC="Key. js"> </SCRIPT>

    See Image

  6. Now that you have the key class, we will modify the scene. XAML. js file to instantiate these "keys" to create our keypad.

 

Creating multiple buttons Part2: The main scene

  1. Open the provided project file
    Open the project files used in this section in blend.
    A. Select a File> open a project...
    B. Browse and find the folder where the project is stored.
    C. Double-click keyboard. csproj to open it.
  2. Handleload Method
    Open this project in Visual Studio
    A. in the project panel file area, right-click the project name and select edit in Visual Studio.
    B. Open the scene. XAML. js file in Visual Studio.
    C. Add the following code to the handleload method: This . Keys =   New Array ();
    This . Keys [ 0 ] =   New Array ( " 7 " , " 8 " , " 9 " );
    This . Keys [ 1 ] =   New Array ( " 4 " , " 5 " , " 6 " );
    This . Keys [ 2 ] =   New Array ( " 1 " , " 2 " , " 3 " );
    This . Keys [ 3 ] =   New Array ( " 0 " , " . " , " > " );
    This . Downloadkeyxaml ();

    The Code defines an array used to build a numeric keyboard. The last line of code begins to download the key. XAML file.

  3. Downloadkeyxaml () method
    To download an external XAML file, you need to use a downloader object: Downloadkeyxaml: Function ()
    {
    VaR Downloader =   This . Control. Createobject ( " Downloader " );
    Downloader. addeventlistener ( " Completed " , Silverlight. createdelegate ( This , This . Keydownloadfinished ));
    Downloader. Open ( " Get " , " Key. XAML " );
    Downloader. Send ();
    },

    In this method:VaRDownloader= This. Control. Createobject ("Downloader");

    The first line creates a download object.Downloader. addeventlistener ("Completed", Silverlight. createdelegate (This,This. Keydownloadfinished ));

    The second row creates an event to be triggered after the download is complete.Downloader. Open ("Get","Key. XAML");

    The third row defines the XAML file to be downloaded.Downloader. Send ();

    Finally, start the download process.
    Reference

  4. Keydownloadfinished () method
    When the download is complete, keydownloadfinished () is called and the final object of the method is created. Keydownloadfinished: Function (Sender, eventargs)
    {
    VaR Xamlitem = Sender. getresponsetext ( "" );
    For ( VaR I = 0 ; I <   This . Keys. length; I ++ )
    {
    For ( VaR J = 0 ; J <   This . Keys [I]. length; j ++ )
    {
    VaR Key =   This . Control. content. createfromxaml (xamlitem, True );
    This . Rootcanvas. Children. Add (key );
    New Keyboard. Key ( This . Control, key, This . Keys [I] [J], J * 70 + 30 , I * 70 + 30 );
    }
    }
    }

    VaRXamlitem=Sender. getresponsetext ("");

    The first line gets the download result, a XAML file represented by a string.
    When the XAML string is ready, the Code starts two for () loops to create rows and columns composed of keys. Here we will see three lines of code.VaRKey= This. Control. content. createfromxaml (xamlitem,True);

    In the first line of the loop, we have the downloaded XAML string to create an object. Note that this method requires two parameters. The first one is a XAML string. The second one isNamwon.
    When nametyperis is true, the instance of each XAML object will have its own scope, which means that the name of the object and timeline can be repeated without conflict.This. Rootcanvas. Children. Add (key );

    The second row adds the newly created object to the element tree inside the main canvas. The element without this step is always invisible.NewKeyboard. Key (This. Control, key,This. Keys [I] [J], J*70+30, I*70+30);

    The last line of code inside the loop creates an instance of the key class we created earlier. Note that five parameters are passed to the key constructor:
    This. Control: Silverlight Object Reference
    Key: The new object instance we just created using the createfromxaml () method
    This. Keys [I] [J]: number of keys created in step 2.
    J * 70 + 30: X position
    I * 70 + 30: Y position
    Refer to the detailed description of the first key class in the tutorial.

next step...
these are the basic theories required for creating most Silverlight programs. Imagine that you can use some tips to create other interactive objects such as drop-down lists, check boxes, and slide.
you can even create your own control library to reuse them in your project.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.