Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Polygon Shape doesn't work with DynamicResource color binding #11691

Closed
bdgza opened this issue Aug 7, 2020 · 11 comments · Fixed by #12761
Closed

Polygon Shape doesn't work with DynamicResource color binding #11691

bdgza opened this issue Aug 7, 2020 · 11 comments · Fixed by #12761
Assignees
Labels
4.8.0 regression on 4.8.0 a/shapes blocker Issue blocks next stable release. Prioritize fixing and reviewing this issue. e/3 🕒 3 t/bug 🐛

Comments

@bdgza
Copy link

bdgza commented Aug 7, 2020

Description

Updating to XF 4.8 from 4.7 the DynamicResource fill color binding on the Polygon shape is no longer recognized. It still works with a fixed color.

Steps to Reproduce

  1. Bind a DynamicResource color to the fill color on a Polygon shape in XAML.

Expected Behavior

The DynamicResource color binding is used to fill the Polygon.

Actual Behavior

The Polygon is not drawn at all.

Basic Information

  • Version with issue: Xamarin Forms 4.8.0.1269
  • Last known good version: 4.7.0.1260
  • IDE: Visual Studio for Mac 8.7
  • Platform Target Frameworks:
    • iOS: 13.6 (13.20.2.2)
    • Android: 10 (10.3.1.4)
  • Affected Devices: iOS Simulator, Samsung SM-T395

Screenshots

4.7:
Simulator Screen Shot - iPhone 11 - 2020-08-07 at 14 48 00

4.8:
Simulator Screen Shot - iPhone 11 - 2020-08-07 at 14 48 50

Reproduction Link

PolygonDynamicColor.zip

@PureWeen
Copy link
Contributor

PureWeen commented Aug 7, 2020

@StephaneDelcroix or @jsuarezruiz ?

@samhouts samhouts added 4.8.0 regression on 4.8.0 a/shapes i/regression s/unverified New report that has yet to be verified t/bug 🐛 labels Aug 7, 2020
@jsuarezruiz
Copy link
Contributor

The type supported by the Shapes Fill and Stroke properties in Xamarin.Forms 4.8 is a Brush (SolidColorBrush, LinearGradientBrush, or RadialGradientBrush) instead of a Color.
The first case works because there is a TypeConverter, but in the second, using a Color type, it is not working. Changing the Color resource by a SolidColorBrush fixes the problem.

@jsuarezruiz jsuarezruiz added the s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. label Aug 10, 2020
@StephaneDelcroix
Copy link
Member

@jsuarezruiz so you'd like TypeConverters to be applied with DynamicResource, or any kind of Binding too ?

@bdgza
Copy link
Author

bdgza commented Aug 10, 2020

I define a SolidColorBrush entry in my App.xaml and reference this Brush in my Polygon Fill instead of the Color. But then setting the Color for my SolidColorBrush has the same problem as setting the Color directly.

I can only use a fixed color or StaticResource Color, as soon as I change the Brush Color to a DynamicResource Color it doesn't render the Polygon.

@samhouts samhouts removed the s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. label Aug 12, 2020
@jsuarezruiz
Copy link
Contributor

jsuarezruiz commented Aug 17, 2020

Could you take a look to this modified sample:
PolygonDynamicColor.zip
?

Captura de pantalla 2020-08-17 a las 14 28 48

@bdgza
Copy link
Author

bdgza commented Aug 17, 2020

I took a look, but what if I wanted to do it like how it is in this modified modified sample:
PolygonDynamicColor.zip

@jsuarezruiz jsuarezruiz removed the s/unverified New report that has yet to be verified label Aug 21, 2020
@jsuarezruiz
Copy link
Contributor

@bdgza Thanks for the new modified sample. Now I see the issue!. Will answer here when I have the fix.

@rmarinho rmarinho added the blocker Issue blocks next stable release. Prioritize fixing and reviewing this issue. label Nov 6, 2020
rmarinho pushed a commit that referenced this issue Nov 11, 2020
#11911 fixes #11691

* Propagate BC to Brush childrens

* Added Brush unit test to check if propagate the bc correctly

* Added another repro sample

* Fix the issue

* Added brush parent unit test
@BurkusCat
Copy link
Contributor

I think this needs reopened @rmarinho. I've posted a comment on #12836 describing more but if the color dynamically changes the polygon shape does not change color. It is behaving like a StaticResource instead of a DynamicResource.

@jsuarezruiz
Copy link
Contributor

@BurkusCat Thanks for the feedback, I will review the issue changing the DynamicResource.

@AdrianoRochedo
Copy link

Xamarin.Forms 5.0.0.1931

  • Works on Android
  • Does not work on UWP
  • Not tested on IOS

@Juansero29
Copy link

I'm having issues with the brush not changing using <PackageReference Include="Xamarin.Forms" Version="5.0.0.2012" /> when using a static brush that defines its Color property via a DynamicResource expression. Here's what my code looks like.

In DarkTheme.xaml

<?xml version="1.0" encoding="UTF-8" ?>
<ResourceDictionary
    x:Class="EnigmatiKreations.Framework.Controls.DarkTheme"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:d="http://xamarin.com/schemas/2014/forms/design"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Color x:Key="PrimaryColor">Black</Color>
    <Color x:Key="AccentColor">WhiteSmoke</Color>
    <Color x:Key="TertiaryColor">#484848</Color>
    <Color x:Key="CardEvenColor">#212121</Color>
</ResourceDictionary>

In LightTheme.xaml

<?xml version="1.0" encoding="UTF-8" ?>
<ResourceDictionary
    x:Class="EnigmatiKreations.Framework.Controls.LightTheme"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:d="http://xamarin.com/schemas/2014/forms/design"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Color x:Key="PrimaryColor">WhiteSmoke</Color>
    <Color x:Key="AccentColor">Black</Color>
    <Color x:Key="TertiaryColor">DarkGray</Color>
    <Color x:Key="CardEvenColor">GhostWhite</Color>
</ResourceDictionary>

In Themes.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary
    x:Class="EnigmatiKreations.Framework.Controls.Themes"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:d="http://xamarin.com/schemas/2014/forms/design"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <!--  https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/theming/theming  -->
    <SolidColorBrush x:Key="PrimaryBrush" Color="{DynamicResource PrimaryColor}" />
    <SolidColorBrush x:Key="AccentBrush" Color="{DynamicResource AccentColor}" />
    <SolidColorBrush x:Key="TertiaryBrush" Color="{DynamicResource TertiaryColor}" />
    <SolidColorBrush x:Key="QuaternaryBrush" Color="{DynamicResource CardEvenColor}" />

    [.... more styles here using the brushes and colors ...]
    
</ResourceDictionary>

In App.xaml

<?xml version="1.0" encoding="utf-8" ?>
<Application
    x:Class="ControlsGallery.App"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:Controls="clr-namespace:EnigmatiKreations.Framework.Controls;assembly=EnigmatiKreations.Framework.Controls">
    <Application.Resources>

        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>

                <!--  Imports the app themes (color palettes, and default styles for native controls)  -->
                <!--  Every other template and control depends on this import  -->
                <Controls:Themes />

                <!--  Imports data templates and templates to be reused across the app  -->
                <!--  They themselves make use of themes  -->
                <Controls:Templates />

                <!--  Imports XAML styles and templates for custom controls, templated controls and inherited controls from the app  -->
                <!--  They themselves make use of templates and themes  -->
                <Controls:GenericControls />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

        <!--  Default is set apart to identify it easily when changing it dynamically with switch  -->
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <Controls:DarkTheme />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

In MainPage.xaml

                    <!--  (changing the Fill expression to Fill="{StaticResource AccentBrush}" to shows the same behavior -->
                    <Path
                        Aspect="UniformToFill"
                        Data="M19,13H13V19H11V13H5V11H11V5H13V11H19V13Z"
                        Fill="{DynamicResource AccentBrush}"         
                        HeightRequest="64"
                        HorizontalOptions="Center"
                        VerticalOptions="Center"
                        WidthRequest="64" />

In MainPage.xaml.cs, the code that dynamically changes the app's theme for Xamarin.Forms

        public MainPage()
        {
            InitializeComponent();
            BindingContext = this;
        }

        private void Switch_Toggled(object sender, ToggledEventArgs e)
        {
            var themeMergedDictionary = Application.Current.Resources.MergedDictionaries.Where(d => d.MergedDictionaries.Count == 1).FirstOrDefault();
            if (themeMergedDictionary != null) themeMergedDictionary.MergedDictionaries.Clear();
            if (e.Value)
            {
                Application.Current.UserAppTheme = OSAppTheme.Light;
                themeMergedDictionary.MergedDictionaries.Add(new LightTheme());
            }
            else
            {
                Application.Current.UserAppTheme = OSAppTheme.Dark;
                themeMergedDictionary.MergedDictionaries.Add(new DarkTheme());
            }
        }

It only works if I declare the brushes directly with static color values in LightTheme.xaml and DarkTheme.xaml like this:

Working LightTheme.xaml

<?xml version="1.0" encoding="UTF-8" ?>
<ResourceDictionary
    x:Class="EnigmatiKreations.Framework.Controls.LightTheme"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:d="http://xamarin.com/schemas/2014/forms/design"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Color x:Key="PrimaryColor">WhiteSmoke</Color>
    <Color x:Key="AccentColor">Black</Color>
    <Color x:Key="TertiaryColor">DarkGray</Color>
    <Color x:Key="CardEvenColor">GhostWhite</Color>

    <SolidColorBrush x:Key="PrimaryBrush" Color="WhiteSmoke" />
    <SolidColorBrush x:Key="AccentBrush" Color="Black" />
    <SolidColorBrush x:Key="TertiaryBrush" Color="DarkGray" />
    <SolidColorBrush x:Key="QuaternaryBrush" Color="GhostWhite" />
</ResourceDictionary>

Working DarkTheme.xaml

<?xml version="1.0" encoding="UTF-8" ?>
<ResourceDictionary
    x:Class="EnigmatiKreations.Framework.Controls.DarkTheme"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:d="http://xamarin.com/schemas/2014/forms/design"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Color x:Key="PrimaryColor">Black</Color>
    <Color x:Key="AccentColor">WhiteSmoke</Color>
    <Color x:Key="TertiaryColor">#484848</Color>
    <Color x:Key="CardEvenColor">#212121</Color>


    <SolidColorBrush x:Key="PrimaryBrush" Color="Black" />
    <SolidColorBrush x:Key="AccentBrush" Color="WhiteSmoke" />
    <SolidColorBrush x:Key="TertiaryBrush" Color="#484848" />
    <SolidColorBrush x:Key="QuaternaryBrush" Color="#212121" />
</ResourceDictionary>

Is this a normal behaviour? If so, why? Can someone please explain?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
4.8.0 regression on 4.8.0 a/shapes blocker Issue blocks next stable release. Prioritize fixing and reviewing this issue. e/3 🕒 3 t/bug 🐛
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants