【发布时间】: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。
【问题讨论】:
-
为什么在可绑定属性中使用可绑定属性?如果您直接使用
<local:LabelStyle PropertyA="{DynamicResource MediumFont}" PropertyB="{DynamicResource MediumFont}"/>,PropertyA 可以工作。 -
我已将某些属性分组到特定的类中,并通过 Bindable 属性使用。
标签: xamarin custom-type dynamicresource