【问题标题】:DynamicResource not working for custom type property in XamarinDynamicResource 不适用于 Xamarin 中的自定义类型属性
【发布时间】:2021-08-18 19:54:24
【问题描述】:

我们已尝试为我的自定义类属性绑定动态资源。但 DynamicResource 对于对象类型属性工作正常,但不适用于双类型属性。请参阅下面的代码。

您可以通过设置PropertyA属性更改方法的断点来确认PropertyA没有设置动态资源。

对于PropertyB属性更改方法,断点命中动态资源值。

 <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)
        {
        }
    }

请帮助我。为什么动态资源不适用于 PropertyA。

【问题讨论】:

  • 为什么在可绑定属性中使用可绑定属性?如果您直接使用&lt;local:LabelStyle PropertyA="{DynamicResource MediumFont}" PropertyB="{DynamicResource MediumFont}"/&gt;,PropertyA 可以工作。
  • 我已将某些属性分组到特定的类中,并通过 Bindable 属性使用。

标签: xamarin custom-type dynamicresource


【解决方案1】:

解决方案 1:

使用StaticResource 代替DynamicResource。动态资源存储 Key,在本例中为“MediumFont”,它是一个字符串,因此不会绑定到 double 类型的属性。

  <local:TestClass>
            <local:TestClass.HintStyle>
                <local:LabelStyle PropertyA="{StaticResource MediumFont}" PropertyB="{StaticResource MediumFont}"/>
            </local:TestClass.HintStyle>
        </local:TestClass>

解决方案 2:

当使用 DynamicResource 时,我们必须使用 type object 作为可绑定的属性类型。然后将PropertyAProperty 设置为私有。

  public object PropertyA
    {
        get => GetValue(PropertyAProperty);
        set => SetValue(PropertyAProperty, value);
    }

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多