此特定于 Android 平台的功能用于在 ImageButton 上启用投影。 通过将 ImageButton.IsShadowEnabled 可绑定属性设置为 true,它可以与多个控制投影的其他可选可绑定属性一起用于 XAML:
<ContentPage ...
             xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core">
    <StackLayout Margin="20">
       <ImageButton ...
                    Source="XamarinLogo.png"
                    BackgroundColor="GhostWhite"
                    android:ImageButton.IsShadowEnabled="true"
                    android:ImageButton.ShadowColor="Gray"
                    android:ImageButton.ShadowRadius="12">
            <android:ImageButton.ShadowOffset>
                <Size>
                    <x:Arguments>
                        <x:Double>10</x:Double>
                        <x:Double>10</x:Double>
                    </x:Arguments>
                </Size>
            </android:ImageButton.ShadowOffset>
        </ImageButton>
        ...
    </StackLayout>
</ContentPage>
或者,可以使用 Fluent API 从 C# 使用它:
using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
...
var imageButton = new Xamarin.Forms.ImageButton { Source = "XamarinLogo.png", BackgroundColor = Color.GhostWhite, ... };
imageButton.On<Android>()
           .SetIsShadowEnabled(true)
           .SetShadowColor(Color.Gray)
           .SetShadowOffset(new Size(10, 10))
           .SetShadowRadius(12);
重要
投影作为 ImageButton 背景的一部分绘制,并且仅在设置了 BackgroundColor 属性时才绘制背景。 因此,如果未设置 ImageButton.BackgroundColor 属性,则不会绘制投影。
ImageButton.On<Android> 方法指定这一平台特定功能仅可在 Android 上运行。 Xamarin.Forms.PlatformConfiguration.AndroidSpecific 命名空间中的 ImageButton.SetIsShadowEnabled 方法用于控制是否在 ImageButton 上启用投影。 此外,可以调用以下方法来控制投影:
- SetShadowColor– 设置投影的颜色。 默认颜色为- Color.Default。
- SetShadowOffset– 设置投影的偏移。 偏移会更改阴影的投射方向,并指定为值- Size。 结构值- Size使用的单位与设备无关,第一个值是到左边(负值)或右边(正值)的距离,第二个值是到顶部(负值)或底部(正值)的距离。 此属性的默认值为 (0.0, 0.0),这会在- ImageButton的每一侧投射阴影。
- SetShadowRadius– 设置用于呈现投影的模糊半径。 默认半径值为 10.0。
注意
可以通过调用 GetIsShadowEnabled、GetShadowColor、GetShadowOffset、GetShadowRadius 方法来查询投影的状态。
结果是可以在 ImageButton 上启用投影:
