
Getting yourself Oriented with WP7 Orientation technology. This title was picked from Charles Petzold’s book on WP7 and here we will explore the technology behind how orientation works behind the scenes.
The SupportedPageOrientation is actually and enumeration and the available members are Potrait, Landscape and PotraitorLandscape. The enumeration is a property of PhoneApplicationPage Class.
The result seen in the earlier blog on WP7 demonstrates the dynamic layout of Silverlight which is extended by WPF which was designed to adjust to different aspect ratios and screen/window sizes. What is most noticeable is that different elements change their size when the phone is used between landscape or portrait. This is particularly useful when an application is viewed in landscape during which the element sizes are increased to accommodate the screen size to give a uniform look and return back to a smaller size when changed to portrait. This is specifically important when you want to build applications that are orientation independent and one cannot anticipate the position of the keyboard on a phone. This is needless to say when there is a plethora of phone hardware options available.
To give more flexibility, the PhoneApplicationPage Class provides OrientationChanged events. Let’s take a look at a very useful example that leverages this event. From our earlier blog which demonstrated the orientation of an image, let’s add a text box to the main page. To do this, open the solution in Blend and do the following:
1) Drag a Text Box from the available controls
2) From the designer select the ‘PhoneApplicationPage’ and select the events icon located next to the properties icon
3) Enter a meaningful name for the OrientChanged event handler and the C# code window is opened
4) Enter the following code (Note: event handler is created and only code in braces is required):
private void test_orientation(object sender Microsoft.Phone.Controls.OrientationChangedEventArgs e)
{
TextBoxtb = new TextBox();
tb.Text = Orientation.ToString();
MessageBox.Show(tb.Text);
}
Now start entering some text (or by just clicking in the text box) and see the on screen key board popup. This is when you change the orientation by clicking on the rotate option from the emulator and once can see the keyboard adjusting as per the orientation and the Orientation Changed events are fired as seen from the output of the Message Box. These events can be leveraged to adjust your content appropriately. The images are screen shots that depict someassociated event arguments such as PortraitUp and LandscapeLeft
No comments:
Post a Comment