共用方式為


Android 上的 ImageButton 置放陰影

此 Android 平臺專用可用來在 上 ImageButton啟用陰影。 XAML 中會藉由將 ImageButton.IsShadowEnabled 可繫結屬性設定為 true來取用,以及一些可控制投下陰影的其他選擇性可繫結屬性:

<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上執行。 命名空間 ImageButton.SetIsShadowEnabled 中的 Xamarin.Forms.PlatformConfiguration.AndroidSpecific 方法可用來控制 是否在 上 ImageButton啟用陰影。 此外,您可以叫用下列方法來控制陰影:

  • SetShadowColor – 設定陰影的色彩。 預設色彩為 Color.Default
  • SetShadowOffset – 設定陰影的位移。 位移會變更陰影的轉換方向,並指定為 Size 值。 結構 Size 值會以裝置無關的單位表示,第一個值是左邊(負值)或右(正值)的距離,而第二個值則為上方(負值)或以下的距離(正值)。 這個屬性的預設值是 (0.0, 0.0),這會導致陰影在 的每一側 ImageButton周圍投射。
  • SetShadowRadius– 設定用來呈現陰影的模糊半徑。 默認半徑值為 10.0。

注意

藉由呼叫 GetIsShadowEnabledGetShadowColorGetShadowOffsetGetShadowRadius 方法,即可查詢陰影的狀態。

結果是可以在 上 ImageButton啟用陰影:

具有陰影的 ImageButton