|
In this article we will explore SizeChanged and OrientationChanged evernts. We will discuss about ActualWidth and ActualHeight properties also.
Let's proceed with the example demonstrated in last two articles
Part 1 - Windows Phone 7 Part 2 - Windows Phone 7 Orientation
Add SizeChanged event in SizeChanged="ContentPanel_SizeChanged" in the contentpanel like below in MainPage.xaml <Grid x:Name="ContentPanel" SizeChanged="ContentPanel_SizeChanged" Grid.Row="1" Margin="12,0,12,0"> <TextBlock Name="txtContent" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Grid>
Now Add ContentPanel_SizeChanged event in MainPage.xaml.cs
private void ContentPanel_SizeChanged(object sender, SizeChangedEventArgs e) {
}
Now place below code in ContentPlane_SizeChanged event
private void ContentPanel_SizeChanged(object sender, SizeChangedEventArgs e) { txtContent.Text = String.Format("ContentPanel size: {0}\n" + "TitlePanel size: {1}\n" + "LayoutRoot size: {2}\n" + "MainPage size: {3}\n" + "Frame size: {4}", e.NewSize, new Size(TitlePanel.ActualWidth, TitlePanel.ActualHeight), new Size(LayoutRoot.ActualWidth, LayoutRoot.ActualHeight), new Size(this.ActualWidth, this.ActualHeight), Application.Current.RootVisual.RenderSize); }
The size of ContentPanel can be retrieved using NewSize of the event argument. The TitlePanel, LayoutRoot and MainPage height and width is available in ActualWidth and ActualHeight.The Frame size is available in Application.Current which is static property and returns application obect of current process.
On running the application, below output on Emulator should appear.  On turning the Emulator the text displayed in the Portrait should be turned into landscape. 
First SizeChanged event triggers on page creation when the content grid size changes from 0 to finite size.
MainPage size and the frame size has 32 pixel difference to display system tray.
The 32 pixel space on top can be utilized by making SystemTray.IsVisible to false in MainPage.xaml.
shell :SystemTray.IsVisible="False"
Orientation Event
If any action needs to be performed on PhoneApplicationFrame and PhoneApplicationPage include OrientationChanged events. Let's see how we can do this.
Place a TextBlock in the content panel of MainPage.xaml
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <TextBlock Name="txtContent" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Grid>
Place below code in the MainPage.xaml.cl public MainPage() { InitializeComponent(); txtContent.Text = Orientation.ToString(); } protected override void OnOrientationChanged(OrientationChangedEventArgs args) { txtContent.Text = args.Orientation.ToString(); base.OnOrientationChanged(args); }
Now run the application.
Now turn the emulator, emulator should give below output
On orientation change OnOrientaionChanged event gets new valude in args.
This ends the article of Windows Phone 7 SizeChanged and OrientationChanged event.
|