Share via


Android での VisualElement の昇格

この Android プラットフォーム固有の機能は、API 21 以降を対象とするアプリケーション上のビジュアル要素の昇格 (Z オーダー) を制御するために使用されます。 ビジュアル要素の昇格によって描画順序が決まります。Z 値が高いビジュアル要素により、Z 値が小さいビジュアル要素が隠されます。 VisualElement.Elevation 添付プロパティを boolean 値に設定し、XAML で使用されます。

<ContentPage ...
             xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
             Title="Elevation">
    <StackLayout>
        <Grid>
            <Button Text="Button Beneath BoxView" />
            <BoxView Color="Red" Opacity="0.2" HeightRequest="50" />
        </Grid>        
        <Grid Margin="0,20,0,0">
            <Button Text="Button Above BoxView - Click Me" android:VisualElement.Elevation="10"/>
            <BoxView Color="Red" Opacity="0.2" HeightRequest="50" />
        </Grid>
    </StackLayout>
</ContentPage>

または、Fluent API を使用して C# から使用することもできます。

using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
...

public class AndroidElevationPageCS : ContentPage
{
    public AndroidElevationPageCS()
    {
        ...
        var aboveButton = new Button { Text = "Button Above BoxView - Click Me" };
        aboveButton.On<Android>().SetElevation(10);

        Content = new StackLayout
        {
            Children =
            {
                new Grid
                {
                    Children =
                    {
                        new Button { Text = "Button Beneath BoxView" },
                        new BoxView { Color = Color.Red, Opacity = 0.2, HeightRequest = 50 }
                    }
                },
                new Grid
                {
                    Margin = new Thickness(0,20,0,0),
                    Children =
                    {
                        aboveButton,
                        new BoxView { Color = Color.Red, Opacity = 0.2, HeightRequest = 50 }
                    }
                }
            }
        };
    }
}

Button.On<Android> メソッドは、このプラットフォーム固有設定が Android 上でのみ実行されるように指定します。 Xamarin.Forms.PlatformConfiguration.AndroidSpecific 名前空間の VisualElement.SetElevation メソッドは、ビジュアル要素の昇格を null 許容 float に設定するために使用されます。 さらに、VisualElement.GetElevation メソッドを使用して、ビジュアル要素の昇格値を取得できます。

その結果、ビジュアル要素の昇格を制御して、Z 値が高いビジュアル要素が、より小さい Z 値のビジュアル要素を隠すことができます。 したがって、この例では、2 つ目の Button は標高値が高いため、BoxView の上にレンダリングされます。

VisualElement の昇格のスクリーンショット