Windows Phone development path (13) touch

Source: Internet
Author: User

Silverlight supports two different programming interfaces to support multi-point touch, which can be simply classified as the underlying interface and high-level interface. The underlying interface is based on the static touch. framereported event. The high-level interface consists of three events defined in the uielement class: manipulationstarted, manipulationdeta, and manipulationcompleted. These events are collectively called Manipulation events, it combines the interaction operations of multiple fingers into two factors: Moving and scaling. The following describes how to use the underlying interface and the high-level interface.

Use the underlying touch interface

  To use the underlying touch interface, you must register an event handler for the static touch. framereported event.The following example demonstrates that when you click textblock to test the area, the text is randomly changed to another color. When you click the area outside the textblock test area, the text becomes the original color.
XAML code:

<Grid x:Name="LayoutRoot" Background="Transparent">
<TextBlock Name="txtblk"
Text="Hello,Windows Phone 7!"
Padding="0,34"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>

C # code:

Public partial class mainpage: phoneapplicationpage
{
Random Rand = new random ();
Brush originalbrush;

// Constructor
Public mainpage ()
{
Initializecomponent ();

Originalbrush = txtblk. Foreground; // obtain the image brush of txtblk. The foreground attribute is of the brush type. The default value is solidcolorbrush, and its color value is black.
Touch. framereported + = ontouchframereported; // register the event handler
}

Protected void ontouchframereported (Object sender, touchframeeventargs E)
{
Touchpoint primarytouchpoint = E. getprimarytouchpoint (null); // call the method of the touchframeeventargs object to obtain the relative position between the touch point and the upper left corner of the screen.
If (primarytouchpoint! = NULL & primarytouchpoint. Action = touchaction. Down) // The main touch point whose action is down
{
If (primarytouchpoint. touchdevice. directlyover = txtblk) // the top-level element of the finger touch
{
Txtblk. foreground = new solidcolorbrush (color. fromargb (255, (byte) Rand. next (256), (byte) Rand. next (256), (byte) Rand. next (256); // specify a random color
}
Else
{
Txtblk. Foreground = originalbrush; // set the foreground of txtblk back to the original paint brush.
}
}
}
}

Effect


Click the textblock area. Click a region other than textblock.

Use high-level touch interface

  The high-level touch interface in Silverlight contains three events: manipulationstarted, manipulationdeta, and manipulationcompleted.Touch. framereported transmits touch information for the entire application, and the manipulation event is based on a single element. Therefore, you can register the manipulationstarted event handler to the textblock. The following example uses the high-level touch interface to overwrite the previous example.

XAML code:

<Grid x:Name="LayoutRoot" Background="Transparent">
<TextBlock Name="txtblk"
Text="Hello,Windows Phone 7!"
Padding="0,34"
HorizontalAlignment="Center"
VerticalAlignment="Center"
ManipulationStarted="txtblk_ManipulationStarted"/>
</Grid>

C # code:

Public partial class mainpage: phoneapplicationpage
{
Random Rand = new random ();
Brush originalbrush;

// Constructor
Public mainpage ()
{
Initializecomponent ();

Originalbrush = txtblk. Foreground; // obtain the original paint brush.
}

Private void txtblk_manipulationstarted (Object sender, manipulationstartedeventargs e) // process registered event handlers
{
Txtblk. foreground = new solidcolorbrush (color. fromargb (255, (byte) Rand. next (256), (byte) Rand. next (256), (byte) Rand. next (256); // specify a random color
E. Complete (); // indicates that the system does not need to process the manipulation event involved by the finger.
E. Handled = true; // indicates that the event is now being processed and no further transfer is required to the upper layer of the visualization tree. Knowledge about routing events
}

Protected override void onmanipulationstarted (manipulationstartedeventargs e) // override the base class virtual Method
{
Txtblk. Foreground = originalbrush; // you can specify the original image brush.

E. Complete ();
Base. onmanipulationstarted (E); // The base keyword indicates the method that calls the base class.
}
}
Routing event

The manipulation event is derived from the available top-level elements of the user touch. However, if the top-level element does not care about the event, the event will be forwarded to the parent element of the element, so it will be forwarded until the highest level of phoneapplicationframe element in the visualization tree. Any element along the way can obtain and process the input event, and prevent the event from being passed to the upper layer of the tree.

  This is why the onmanipulationstarted method can be rewritten in mainpage to make textblock obtain the manipulation event.
   

Related Article

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.