[UWP] Adjust bounds for ContentPage when by itself (#61)
authorPaul DiPietro <paul.dipietro@me.com>
Tue, 12 Apr 2016 18:15:21 +0000 (11:15 -0700)
committerJason Smith <jason.smith@xamarin.com>
Tue, 12 Apr 2016 18:15:21 +0000 (11:15 -0700)
In a scenario where there is a ContentPage by itself, things like labels
ran beneath the StatusBar in landscape mode. This is presumably because
the page was assuming there to be a TitleBar and calculating the bounds
based on that fact; by checking for its visibility and adjusting the
bounds as necessary it allows for correct alignment.

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla40185.cs [new file with mode: 0644]
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
Xamarin.Forms.Platform.WinRT/Platform.cs

diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla40185.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla40185.cs
new file mode 100644 (file)
index 0000000..d6f17d9
--- /dev/null
@@ -0,0 +1,47 @@
+using System;
+
+using Xamarin.Forms.CustomAttributes;
+
+#if UITEST
+using Xamarin.UITest;
+using NUnit.Framework;
+#endif
+
+namespace Xamarin.Forms.Controls.Issues
+{
+       [Preserve(AllMembers = true)]
+       [Issue(IssueTracker.Bugzilla, 40185, "[UWP] ContentPage does not have proper right bounds in landscape", PlatformAffected.WinRT)]
+       public class Bugzilla40185 : TestContentPage
+       {
+               protected override void Init()
+               {
+                       Content = new StackLayout
+                       {
+                               VerticalOptions = LayoutOptions.Center,
+                               Children =
+                               {
+                                       new Button
+                                       {
+                                               Text = "Switch Main Page",
+                                               Command = new Command(SwitchMainPage)
+                                       }
+                               }
+                       };
+               }
+
+               void SwitchMainPage()
+               {
+                       Application.Current.MainPage = new ContentPage
+                       {
+                               BackgroundColor = Color.White,
+                               Content = new Label
+                               {
+                                       Text = "This text should be in bounds in landscape mode.",
+                                       HorizontalTextAlignment = TextAlignment.End,
+                                       VerticalTextAlignment = TextAlignment.Center,
+                                       TextColor = Color.Black
+                               }
+                       };
+               }
+       }
+}
index c887494..ca43a27 100644 (file)
@@ -97,6 +97,7 @@
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla39702.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla40173.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla39821.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Bugzilla40185.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)CarouselAsync.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla34561.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla34727.cs" />
index dbef5f7..012dd2c 100644 (file)
@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Threading.Tasks;
 
+using Windows.ApplicationModel.Core;
 using Windows.UI;
 using Windows.UI.Popups;
 using Windows.UI.Xaml;
@@ -421,9 +422,19 @@ namespace Xamarin.Forms.Platform.WinRT
                                StatusBar statusBar = StatusBar.GetForCurrentView();
 
                                bool landscape = Device.Info.CurrentOrientation.IsLandscape();
+                               bool titleBar = CoreApplication.GetCurrentView().TitleBar.IsVisible;
                                double offset = landscape ? statusBar.OccludedRect.Width : statusBar.OccludedRect.Height;
 
                                _bounds = new Rectangle(0, 0, _page.ActualWidth - (landscape ? offset : 0), _page.ActualHeight - (landscape ? 0 : offset));
+
+                               // Even if the MainPage is a ContentPage not inside of a NavigationPage, the calculated bounds
+                               // assume the TitleBar is there even if it isn't visible. When UpdatePageSizes is called,
+                               // _container.ActualWidth is correct because it's aware that the TitleBar isn't there, but the
+                               // bounds aren't, and things can subsequently run under the StatusBar.
+                               if (!titleBar)
+                               {
+                                       _bounds.Width -= (_bounds.Width - _container.ActualWidth);
+                               }
                        }
 #endif
                }