Applications that look and behave like the native Windows Phone applications will be more attractive to users than applications that don’t behave like the native applications. So, your applications should try to incorporate as much native behavior as possible. A control behavior that is common in many of the native applications is the TiltEffect behavior. Look at the Phone application, which is used for making telephone calls. When you start the Phone app you see a list of users you have called.
Now pay attention as you select an item in the list. When you select that item, you can see a slight tilt to the item that you select. As you release the item, it returns back to its original position. This slight tilt animation gives you a reassurance that the Phone application has received your input. It is a subtle change, but it will increase your users delight.
This article shows you how to add the TiltEffect behavior to controls in your Silver Light applications. A sample application is created which shows the TiltEffect behavior in action and how to turn the TiltEffect behavior on and off at runtime.
Setup for using the TiltEffect Behavior
The TiltEffect behavior is not included in the controls out of the box. We have to get it from a sample that Microsoft released so that third party developers could add this behavior to their applications. The behavior that we are going to use is in a C# file called TiltEffect.cs in the Control Tilt Effect Sample on MSDN.
First of all, download that sample and extract the files to a folder on your computer. The TiltEffect.cs file is in the ControlTiltEffect subdirectory with all of the other source code for the sample. Note the location of the file so that you can add it to your project.
To add it to your Silver Light project, you need to ….
1. Open your project in Visual Studio and right-mouse click on your project.
2. Select the Add menu option, and then select the Existing Item… menu option.
3. In the Add Existing Item dialog navigate to and select the TiltEffect.cs file that you downloaded earlier.
4. This will make a copy of the file in your project source directory and will add it to your project.
Open the TiltEffect.cs file. Notice that the namespace for this file is ControlTiltEffect. You can either change it to be the same namespace as your project, or leave it as is. For our examples, we will leave it as it is in ControlTiltEffect.
When you want to use this TiltEffect in your application you need to add its namespace to files that use it. To add the namespace to the pages that you want to use the TiltEffect behavior on you controls, add these lines to the top of your XAML file:
This will create a new alias called local for the ControlTiltEffect namespace. This allows the classes in that namespace to be available on this page.
The second line enables the TiltEffect. With these two lines added, all Buttons and List Boxes on this page will now have the TiltEffect behavior. As you will see later, we can turn on and off the TiltEffect for specific controls on the page.
Now that we know how to add the TiltEffect behavior to our project and XAML pages, lets look at an example of how we would use it an application.
This sample app has many controls which will demonstrate how the TiltEffect behavior works. Most of the buttons and list box items on this page will tilt when you press on them. With TiltEffect.IsTiltEnabled="True" that is the default behavior for the page.
Suppressing TiltEffect on single control: SuppressTilt Property
Suppose that we have a certain button on our page that we don’t want to tilt. We can enable and suppress the TiltEffect for each control or list box item separately. In our sample, the second button will not tilt, this is because the Suppressed property has been set to stop it from tilting. Here’s the XAML code for the second button.
Notice that it has the local:TiltEffect.SuppressTilt="True". This suppresses all Tilt effects for this control. The second list box item also has this property set, so it will not tilt either. All of the other controls on this page don’t include this property, so they will tilt.
Stopping TiltEffect for Entire Page: IsTiltEnabled Property
It is also possible to stop the TiltEffect for an entire page. This might be useful for showing a “retro” mode for your application. At the bottom of the page, there is a button labeled “Stop Tilts”. When this button is clicked, all TiltEffects on this page will be stopped. If you press it again, the TiltEffects will be re-enabled.
The button3_Click event handler does this work. The C# code behind is where this takes place. In order to use the TiltEffects in the C# code behind, we add the using ControlTiltEffect; statement.
Then in the button3_Click handler, we get the current IsTiltEnabled property for the entire page. To do this we use the method GetIsTiltEnabled to read the dependency property for the entire page that was given to it by the TiltEffect class. Note the the this object inside the parentheses represents the entire page. This value is saved in the tiltEnabled variable.
Then we set the content for the ToggleTilting button based on the IsTiltEnabled property.
Lastly, we change the IsTiltEnabled property for the entire page, to be the opposite of what it used to be. This time we are modifying the dependency property with the SetIsTiltEnabled method.
At this point, none of the buttons or list box items will tilt. Try it and see. Then press the ToggleTilting button again to let the buttons tilt again.
Complete XAML Source Code
Adding Tilt Effects to your controls add a subtle effect that lets your user know their input was received. It is a little more work to add behaviors like this to your applications, but it will pay off with greater user appeal. You have seen how to add the tilt behavior to you app, how to enable it for entire pages, how to disable it for single control and how to disable it for the entire page. Attention to seemingly small details like this will help your application stand out.
BTW, if you are curious, I suggest that you take a look inside the TiltEffect.cs file. It is an excellent example of how to use storyboards and touch manipulations to build effects from the bare bones up. Taking some time to understand how behaviors like this are built will increase your knowledge and confidence with the Windows Phone development. This is a versatile control that you can adapt in other ways if needed.
To download a complete working Windows Phone project that demonstrates Tilt Effects, click the Download Code button below:
Tomorrow, we are going to look at creating custom ringtones for your users, and how Windows Phone now supports this awesome new feature. See you then!