[Android] Set Page background color (#5066) * Fixes #5057 * closes #4916 * Fixes...
authorAndrei Nitescu <nitescua@yahoo.com>
Mon, 18 Feb 2019 11:19:57 +0000 (13:19 +0200)
committerRui Marinho <me@ruimarinho.net>
Mon, 18 Feb 2019 11:19:57 +0000 (11:19 +0000)
* [Android] Fix setting Page background color

* Update Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue5057.xaml.cs

Co-Authored-By: andreinitescu <nitescua@yahoo.com>
* Update Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue5057.xaml.cs

Co-Authored-By: andreinitescu <nitescua@yahoo.com>
* Refactoring and added test info

* Update Xamarin.Forms.Platform.Android/Renderers/PageRenderer.cs

Co-Authored-By: andreinitescu <nitescua@yahoo.com>
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue5057.xaml [new file with mode: 0644]
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue5057.xaml.cs [new file with mode: 0644]
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
Xamarin.Forms.Platform.Android/Renderers/PageRenderer.cs

diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue5057.xaml b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue5057.xaml
new file mode 100644 (file)
index 0000000..c5a1d1c
--- /dev/null
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
+             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
+             x:Class="Xamarin.Forms.Controls.Issues.Issue5057">
+       <StackLayout Padding="10">
+               <Label Text="When both Page.BackgroundImage and Page.BackgroundColor are set, the BackgroundImage takes precedence."/>
+               <Label Text="If Page.BackgroundImage is set, setting BackgroundColor won't be have any visual effect"/>
+               <Label Text="Clearing the BackgroundImage by setting it to null, makes Page background displayed with BackgroundColor"/>
+               <Button Text="Set background color to Red" 
+                Clicked="BtnSetBkgndColorRed_Clicked"
+                BackgroundColor="White"/>
+               <Button Text="Set background image" 
+                Clicked="BtnSetBkgndImg_Clicked" 
+                BackgroundColor="White"/>
+               <Button Text="Set background color to default" 
+                Clicked="BtnSetBkgndColorDefault_Clicked" 
+                BackgroundColor="White"/>
+               <Button Text="Set background image to null" 
+                Clicked="BtnSetBkgndImgNull_Clicked" 
+                BackgroundColor="White"/>
+       </StackLayout>
+</ContentPage>
\ No newline at end of file
diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue5057.xaml.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue5057.xaml.cs
new file mode 100644 (file)
index 0000000..357bf97
--- /dev/null
@@ -0,0 +1,42 @@
+using System;
+using System.Collections.Generic;
+using Xamarin.Forms.CustomAttributes;
+using Xamarin.Forms.Internals;
+using Xamarin.Forms.Xaml;
+
+namespace Xamarin.Forms.Controls.Issues
+{
+#if APP
+       [Preserve(AllMembers = true)]
+       [Issue(IssueTracker.Github, 5057, "Android: ContentPage BackgroundColor ignored in Forms ",
+               PlatformAffected.Android)]
+       [XamlCompilation(XamlCompilationOptions.Compile)]
+       public partial class Issue5057 : ContentPage
+       {
+               public Issue5057()
+               {
+                       InitializeComponent();
+               }
+
+               void BtnSetBkgndColorRed_Clicked(object sender, System.EventArgs e)
+               {
+                       BackgroundColor = Color.Red;
+               }
+
+               void BtnSetBkgndImg_Clicked(object sender, System.EventArgs e)
+               {
+                       BackgroundImage = "test.jpg";
+               }
+
+               void BtnSetBkgndColorDefault_Clicked(object sender, System.EventArgs e)
+               {
+                       BackgroundColor = (Color)BackgroundColorProperty.DefaultValue;
+               }
+
+               void BtnSetBkgndImgNull_Clicked(object sender, System.EventArgs e)
+               {
+                       BackgroundImage = null;
+               }
+       }
+#endif
+}
index 1df9e0d..3ae665d 100644 (file)
       <SubType>Code</SubType>
     </Compile>
     <Compile Include="$(MSBuildThisFileDirectory)Issue4600.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Issue5057.xaml.cs">
+      <DependentUpon>Issue5057.xaml</DependentUpon>
+      <SubType>Code</SubType>
+    </Compile>
     <Compile Include="$(MSBuildThisFileDirectory)LegacyComponents\NonAppCompatSwitch.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)MapsModalCrash.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)ModalActivityIndicatorTest.cs" />
       <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
     </EmbeddedResource>
   </ItemGroup>
+  <ItemGroup>
+    <EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue5057.xaml">
+      <SubType>Designer</SubType>
+      <Generator>MSBuild:Compile</Generator>
+    </EmbeddedResource>
+  </ItemGroup>
 </Project>
\ No newline at end of file
index 72524d0..294a747 100644 (file)
@@ -62,8 +62,7 @@ namespace Xamarin.Forms.Platform.Android
                                Id = Platform.GenerateViewId();
                        }
 
-                       UpdateBackgroundColor(view);
-                       UpdateBackgroundImage(view);
+                       UpdateBackground(false);
 
                        Clickable = true;
                }
@@ -72,9 +71,9 @@ namespace Xamarin.Forms.Platform.Android
                {
                        base.OnElementPropertyChanged(sender, e);
                        if (e.PropertyName == Page.BackgroundImageProperty.PropertyName)
-                               UpdateBackgroundImage(Element);
+                               UpdateBackground(true);
                        else if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName)
-                               UpdateBackgroundColor(Element);
+                               UpdateBackground(false);
                        else if (e.PropertyName == VisualElement.HeightProperty.PropertyName)
                                UpdateHeight();
                }
@@ -105,22 +104,25 @@ namespace Xamarin.Forms.Platform.Android
                        _previousHeight = newHeight;
                }
 
-               void UpdateBackgroundColor(Page view)
+               void UpdateBackground(bool setBkndColorEvenWhenItsDefault)
                {
-                       if (view.Parent is BaseShellItem)
+                       Page page = Element;
+
+                       string bkgndImage = page.BackgroundImage;
+                       if (!string.IsNullOrEmpty(bkgndImage))
+                               this.SetBackground(Context.GetDrawable(bkgndImage));
+                       else
                        {
-                               var background = view.BackgroundColor;
-                               var color = Context.Resources.GetColor(global::Android.Resource.Color.BackgroundLight, Context.Theme);
-                               SetBackgroundColor(background.IsDefault ? color : background.ToAndroid());
+                               Color bkgndColor = page.BackgroundColor;
+                               bool isDefaultBkgndColor = bkgndColor.IsDefault;
+                               if (page.Parent is BaseShellItem && isDefaultBkgndColor)
+                               {
+                                       var color = Context.Resources.GetColor(global::Android.Resource.Color.BackgroundLight, Context.Theme);
+                                       SetBackgroundColor(color);
+                               }
+                               else if (!isDefaultBkgndColor || setBkndColorEvenWhenItsDefault)
+                                       SetBackgroundColor(bkgndColor.ToAndroid());
                        }
-                       else if (view.BackgroundColor != Color.Default)
-                               SetBackgroundColor(view.BackgroundColor.ToAndroid());
-               }
-
-               void UpdateBackgroundImage(Page view)
-               {
-                       if (!string.IsNullOrEmpty(view.BackgroundImage))
-                               this.SetBackground(Context.GetDrawable(view.BackgroundImage));
                }
        }
 }
\ No newline at end of file