【问题标题】:How to change BackgroundColor using OnPlatform Xamarin如何使用 OnPlatform Xamarin 更改 BackgroundColor
【发布时间】:2018-05-07 18:44:09
【问题描述】:

我正在尝试根据正在运行的设备更改条目的背景。 我尝试根据以下信息进行操作:https://developer.xamarin.com/guides/xamarin-forms/xaml/passing-arguments/

并得到了这个:

        <Entry TextColor="White"
            x:Name="entryTop"
            HeightRequest="40"
            WidthRequest="150"
            RelativeLayout.XConstraint = "{ConstraintExpression Type=RelativeToParent,
                Property=Width,
                Factor=.48,
                Constant=10}"
            RelativeLayout.YConstraint = "{ConstraintExpression Type=RelativeToParent,
                Property=Height,
                Factor=0,
                Constant=5}" >
            <Entry.BackgroundColor>
                <OnPlatform x:TypeArguments="x:String">
                    <On Platform="Android" Value="Transparent" />
                    <On Platform="iOS" Value="white" />
                </OnPlatform>
            </Entry.BackgroundColor>
        </Entry>

当我尝试构建我的项目时,我收到此错误:没有为 BackgroundColor 找到属性、可绑定属性或事件,或者值和属性之间的类型不匹配。

我尝试了颜色工厂方法和几个TypeArguments,但似乎找不到正确的方法。

【问题讨论】:

  • BackgroundColor 不是字符串,它的类型是 Color
  • 我已经尝试过使用颜色类型,但它不起作用。还有 Color.FromHex 和我之前提到的工厂方法,如我发布的链接中所示。

标签: c# xaml xamarin.ios xamarin.forms xamarin.android


【解决方案1】:

我找到了答案:

     <Entry.BackgroundColor>
         <OnPlatform x:TypeArguments="Color"
             Android="#5b5b5b"                 
             iOS="#fff">
         </OnPlatform>
     </Entry.BackgroundColor>

以这种方式使用时,TypeArgument 颜色仅接收十六进制值。它不适用于 RGB 或静态颜色(“白色”、“黑色”等)。我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2017-02-09
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    • 2021-08-10
    相关资源
    最近更新 更多