[Android] Set Page background color (#5066) * Fixes #5057 * closes #4916 * Fixes #5073

* [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>
This commit is contained in:
Andrei Nitescu 2019-02-18 13:19:57 +02:00 коммит произвёл Rui Marinho
Родитель c3a5a6b850
Коммит 2277ce8e72
4 изменённых файлов: 94 добавлений и 18 удалений

Просмотреть файл

@ -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>

Просмотреть файл

@ -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
}

Просмотреть файл

@ -428,6 +428,10 @@
<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" />
@ -1091,4 +1095,10 @@
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue5057.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</EmbeddedResource>
</ItemGroup>
</Project>

Просмотреть файл

@ -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)
{
var background = view.BackgroundColor;
var color = Context.Resources.GetColor(global::Android.Resource.Color.BackgroundLight, Context.Theme);
SetBackgroundColor(background.IsDefault ? color : background.ToAndroid());
}
else if (view.BackgroundColor != Color.Default)
SetBackgroundColor(view.BackgroundColor.ToAndroid());
}
Page page = Element;
void UpdateBackgroundImage(Page view)
string bkgndImage = page.BackgroundImage;
if (!string.IsNullOrEmpty(bkgndImage))
this.SetBackground(Context.GetDrawable(bkgndImage));
else
{
if (!string.IsNullOrEmpty(view.BackgroundImage))
this.SetBackground(Context.GetDrawable(view.BackgroundImage));
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());
}
}
}
}