我在全局资源中使用这种颜色(在app.xaml中)
<Color x:Key="MyColor" x:FactoryMethod="FromHex">
<x:Arguments>
<x:String>#ffffff</x:String>
</x:Arguments>
</Color>
我需要不同的颜色用于Android和Windows Phone。我尝试了以下代码:
<Color x:Key="MyColor" x:FactoryMethod="FromHex">
<x:Arguments>
<OnPlatform x:TypeArguments="x:String"
Android="#006ABB"
WindowsPhone="#ffffff" />
<x:String></x:String>
</x:Arguments>
</Color>
但它不起作用。告诉我 - 如何在代码隐藏中添加它。可能吗?
你可以在App.xaml
中做到这一点,不需要去代码。只是你需要一点不同的方法。这是代码
<Application.Resources>
<ResourceDictionary>
<OnPlatform
x:Key="MyColor"
x:TypeArguments="Color"
Android="#006ABB"
iOS="#006A00"
WinPhone="#ffffff"/>
</ResourceDictionary>
</Application.Resources>
之所以这样做OnPlatform
是因为泛型类定义了隐式对话运算符该运算符可以将每个OnPlatform
对象转换为其入站泛型T
类。 像这样的东西
public static implicit operator T(OnPlatform<T> onPlatform)