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

[Bug] DynamicResource not working for custom type property in Xamarin #14314

Open
MuneeshKumarG opened this issue Jun 1, 2021 · 3 comments
Open
Labels
a/Xaml </> s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/unverified New report that has yet to be verified t/bug 🐛
Projects

Comments

@MuneeshKumarG
Copy link

We have tried to bind dynamic resource for my custom class properties. But DynamicResource working fine for object type property, but not working for double type property. See my code below.

You can confirm the dynamic resource not set for PropertyA, by setting break point for PropertyA property change method.

For PropertyB property change method, break point hit with dynamic resource value.

 <ContentPage.Resources>
        <ResourceDictionary>
            <x:Double x:Key="MediumFont">25</x:Double>
        </ResourceDictionary>
    </ContentPage.Resources>

    <StackLayout>
                
        <local:TestClass >
            <local:TestClass.HintStyle>
                <local:LabelStyle PropertyA="{DynamicResource MediumFont}" PropertyB="{DynamicResource MediumFont}"/>
            </local:TestClass.HintStyle>
        </local:TestClass>
        
    
    </StackLayout>
 public class TestClass :View
    {

        public LabelStyle HintStyle
        {
            get => (LabelStyle)GetValue(HintStyleProperty);
            set => SetValue(HintStyleProperty, value);
        }

        public static readonly BindableProperty HintStyleProperty =
            BindableProperty.Create(nameof(HintStyle), typeof(LabelStyle), typeof(TestClass),
            new LabelStyle(), BindingMode.Default);
    }

    public class LabelStyle :View
    {
        public double PropertyA
        {
            get => (double)GetValue(PropertyAProperty);
            set => SetValue(PropertyAProperty, value);
        }

        public static readonly BindableProperty PropertyAProperty =
           BindableProperty.Create(nameof(PropertyA), typeof(double), typeof(LabelStyle), 
               0d, BindingMode.Default, null, OnPropertyAChanged);
        private static void OnPropertyAChanged(BindableObject bindable, object oldValue, object newValue)
        {
        }

        public object PropertyB
        {
            get => GetValue(PropertyBProperty);
            set => SetValue(PropertyBProperty, value);
        }

        private static readonly BindableProperty PropertyBProperty =
            BindableProperty.Create(nameof(PropertyB), typeof(object), typeof(LabelStyle),
            null, BindingMode.Default, null, OnPropertyBChanged);

        private static void OnPropertyBChanged(BindableObject bindable, object oldValue, object newValue)
        {
        }
    }

Please help me. Why the dynamic resource not working for PropertyA.

@MuneeshKumarG MuneeshKumarG added s/unverified New report that has yet to be verified t/bug 🐛 labels Jun 1, 2021
@jsuarezruiz jsuarezruiz added this to New in Triage via automation Jun 1, 2021
@Yuvasrisivakumar
Copy link

Any update on this?

@jsuarezruiz
Copy link
Contributor

LabelStyle is a View, but has no Parent set, the Parent doesn’t set his children, context is not propagated. Without it, Dynamic Resources can’t work.

@jsuarezruiz jsuarezruiz moved this from New to Needs Info in Triage Jun 18, 2021
@jsuarezruiz jsuarezruiz added the s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. label Jun 18, 2021
@Yuvasrisivakumar
Copy link

Hi,

Thanks for your update, and we have tested our sample with your given solution and it works at our end, but we can't set the parent to the LabelStyle class when we inherit the BindableObject in the LabelStyle class. Can you please share any other solution to this problem?

public class LabelStyle : BindableObject
{
public double PropertyA
{
get => (double)GetValue(PropertyAProperty);
set => SetValue(PropertyAProperty, value);
}

    public static readonly BindableProperty PropertyAProperty =
       BindableProperty.Create(nameof(PropertyA), typeof(double), typeof(LabelStyle),
           0d, BindingMode.Default, null, OnPropertyAChanged);
    private static void OnPropertyAChanged(BindableObject bindable, object oldValue, object newValue)
    {
    }
    public object PropertyB
    {
        get => GetValue(PropertyBProperty);
        set => SetValue(PropertyBProperty, value);
    }

    private static readonly BindableProperty PropertyBProperty =
        BindableProperty.Create(nameof(PropertyB), typeof(object), typeof(LabelStyle),
        null, BindingMode.Default, null, OnPropertyBChanged);

    private static void OnPropertyBChanged(BindableObject bindable, object oldValue, object newValue)
    {
    }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a/Xaml </> s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/unverified New report that has yet to be verified t/bug 🐛
Projects
Triage
  
Needs Info
Development

No branches or pull requests

3 participants