我认为我的代码是不言自明的:
<Label Style="{DynamicResource labelStyle}"
HorizontalTextAlignment="End" Text="{Binding message}">
<OnPlatform x:TypeArguments="Color">
<OnPlatform.iOS>
{DynamicResource rightBubbleFontColor}
</OnPlatform.iOS>
<OnPlatform.Android>
{DynamicResource rightBubbleFontColor}
</OnPlatform.Android>
<OnPlatform.Android>
{StaticResource rightBubbleFontColor}
</OnPlatform.Android>
</OnPlatform>
</Label>
我正在尝试将颜色动态绑定到标签。根据当前平台的不同,它必须是另一种资源(DynamicResource或StaticResource)。
尝试构建解决方案时出现此异常:
System.ArgumentException: An item with the same key has already been added.
我现在有以下代码:
<Label Style="{DynamicResource labelStyle}"
HorizontalTextAlignment="End" Text="{Binding message}">
<Label.TextColor>
<OnPlatform
x:Key="RightBubbleFontColor"
x:TypeArguments="Color"
iOS="{DynamicResource rightBubbleFontColor}"
Android="{DynamicResource rightBubbleFontColor}"
WinPhone="{StaticResource rightBubbleFontColor}">
</OnPlatform>
</Label.TextColor>
</Label>
而且我收到以下错误消息: Object reference not set to an instance of an object.
当我将绑定替换为一种颜色时,它将起作用。
工作示例:
<Label Style="{DynamicResource labelStyle}"
HorizontalTextAlignment="End" Text="{Binding message}">
<Label.TextColor>
<OnPlatform
x:Key="RightBubbleFontColor"
x:TypeArguments="Color"
iOS="Red"
Android="Green"
WinPhone="Blue">
</OnPlatform>
</Label.TextColor>
</Label>
因此,我想绑定这种方式肯定是一个问题。
提出了一个新问题以更好地描述问题:https : //stackoverflow.com/questions/39852888/xamarin-forms-use-dynamicresource-or-staticresource-depending-on-os
您有两个<OnPlatform.Android>
要素。我以为最后一个会是<OnPlatform.WinPhone>
。
已编辑
现在,您已经解决了该问题,也许可以尝试一下使其真正起作用?
<OnPlatform
x:Key="BubbleTextColor"
x:TypedArguments="Color"
iOS="{DynamicResource rightBubbleFontColor}"
Android="{DynamicResource rightBubbleFontColor}"
WinPhone="{StaticResource rightBubbleFontColor}" />
然后在必要时将其绑定。
不知道它的工作方式是否会与您现有的有所不同,但是我认为值得一试。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句