Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Event Manipulation in high-level Touch Interface for Windows Phone 7

This tutorial will explain about the events involved in the high-level touch interface in Silverlight for Windows Phone 7.

The Events Manipulation

We all are aware that the screen on the Windows phone is sensitive to touch. Four simultaneous fingers can be identified by the multi-touch screen on a Windows Phone 7 and this feature makes multi-touch so challenging for programmers.

There are three events involved in the high-level touch interface in Silverlight:

  • ManipulationStarted
  • ManipulationDelta
  • ManipulationCompleted

Talking about these events, they don't bother with reporting the activity of individual fingers. In fact, they consolidate the activity of multiple fingers into translation and scaling operations. While Touch.FrameReported delivered touch information for the whole application, theManipulation events are dependent on the individual elements. Hence in SilverlightTapHello1, aManipulationStarted event handler can be set on the TextBlock:

Listing 1: XAML file of SilverlightTapHello1

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBlock Text="Hello, Windows Phone 7!"
             Padding="0 22"
             HorizontalAlignment="Center"
             VerticalAlignment="Center"
             ManipulationStarted="OnTextBlockManipulationStarted" />
</Grid>

Listing 2: MainPage.xaml.cs event handler

public partial class MainPage : PhoneApplicationPage
{
    Random rand = new Random(); 
    public MainPage()
    {
        InitializeComponent();
    }
    void OnTextBlockManipulationStarted(object sender, ManipulationStartedEventArgs args)
    {
        TextBlock txtblk = sender as TextBlock;
        Color clr = Color.FromArgb(255, (byte)rand.Next(256),
                                        (byte)rand.Next(256),
                                        (byte)rand.Next(256));
        txtblk.Foreground = new SolidColorBrush(clr);
        args.Complete();
    }
}

The event handler is able to get the element giving the message from the senderargument which will always be the TextBlock. The TextBlock is also available from theargs.OriginalSource property and the args.ManipulationContainer property.

There is also an easier way to do this where a UIElement class specified the entire Manipulation events. However the Control class supplements those events with protected virtual methods. There is no need to install a handler for theManipulationStarted event on MainPage; in fact one can override the OnManipulationStartedvirtual method.

The approach is actually implemented in the SilverlightTapHello2 project.

Listing 3: XAML file of SilverlightTapHello2

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBlock Name="txtblk"
             Text="Hello, Windows Phone 7!"
             Padding="0 22"
             HorizontalAlignment="Center"
             VerticalAlignment="Center" />
</Grid>

Listing 4: The MainPage class overrides the OnManipulationStarted method

public partial class MainPage : PhoneApplicationPage
{
    Random rand = new Random();
    Brush originalBrush; 
    public MainPage()
    {
        InitializeComponent();
        originalBrush = txtblk.Foreground;
    }

    protected override void OnManipulationStarted(ManipulationStartedEventArgs args)
    {
        if (args.OriginalSource == txtblk)
        {
            txtblk.Foreground = new SolidColorBrush(Color.FromArgb(255, (byte)rand.Next(256),
                                                   (byte)rand.Next(256),(byte)rand.Next(256)));
        }
        else
        {
            txtblk.Foreground = originalBrush;
        }
        args.Complete();
        base.OnManipulationStarted(args);
    }
} 

In the ManipulationStartedEventArgs, there is a property by the name of OriginalSource. This indicates where this event began. In simple terms, we can say that, the topmost element that the user tapped. If this is equivalent to the txtblk object, a random colour is created by the method for the Foreground property else Foreground property is set to the original brush.

Routed Events

The Manipulation events start off at the highest enabled element that is touched by the user but in case that element is not interested in the event, the situation will arise where the event then goes to that element's parent, and so forth up the visual tree ending at the PhoneApplicationFrame element. Input can be grabbed by any element that comes in the way and do something with it.

This is the reason that we can override the OnManipulationStarted method in MainPage and also get manipulation events for the TextBlock. The TextBlock is rarely interested in those events by defaults.

The event argument for the ManipulationStarted event is ManipulationStartedEventArgs that is derived from RoutedEventArgs. It is RoutedEventArgs which specifies the OriginalSource property. This refers to the element on which the event began. However an alternative approach is recommended that combines the two techniques displayed in SilverlightTapHello1 and SilverlightTapHello2.

Listing 5: XAML file of SilverlightTapHello3

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBlock Name="txtblk"
              Text="Hello, Windows Phone 7!"
              Padding="0 22"
              HorizontalAlignment="Center"
              VerticalAlignment="Center"
              ManipulationStarted="OnTextBlockManipulationStarted" 
</Grid>
public partial class MainPage : PhoneApplicationPage
{
    Random rand = new Random();
    Brush originalBrush; 
    public MainPage()
    {
        InitializeComponent();
        originalBrush = txtblk.Foreground;
    } 
    void OnTextBlockManipulationStarted(object sender, ManipulationStartedEventArgs args)
    {
        txtblk.Foreground = new SolidColorBrush(Color.FromArgb(255, (byte)rand.Next(256),
                                               (byte)rand.Next(256),(byte)rand.Next(256)));
        args.Complete();
        args.Handled = true;
    }
    protected override void OnManipulationStarted(ManipulationStartedEventArgs args)
    {
        txtblk.Foreground = originalBrush; 
        args.Complete();
        base.OnManipulationStarted(args);
    }
}

If we see clearly, the logic has been divided into two methods, and makes the entire thing more appealing. The OnTextBlockManipulationStarted method is used to gets events on touching the TextBlock and the OnManipulationStarted event gets all events for MainPage.

Conclusion

The article explained about the concept of touch interface programming as well as event manipulation for windows phone 7.



Software Developer from India. I hold Master in Computer Applications Degree and is well versed with programming languages such as Java, .Net, C and C++ and possess good working knowledge on Mobile Platforms as well.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login