首页 > 解决方案 > 如何在 Xamarin Forms 中打开滑动时更改滑动视图内容的不透明度?

问题描述

我想在滑动过程中更改不透明度。有没有我可以用来做这件事的事件,如果有的话,有没有人有任何例子说明他们是如何做到这一点的。我想要的是当滑动区域完全显示时,滑动背景的不透明度从 100% 变为 0%。

标签: xamarinxamarin.forms

解决方案


欢迎来到 SO!

我不确定这是否是您想要的,您可以尝试一下SwipeEndedSwipeChanging检查一下。

Xaml 代码(基于官方示例)如下:

...
<SwipeView SwipeEnded="SwipeView_SwipeEnded" SwipeChanging="SwipeView_SwipeChanging">
        <SwipeView.LeftItems>
            <SwipeItems SwipeBehaviorOnInvoked="Close"
                        Mode="Reveal">
                <SwipeItem Text="Favorite"
                            IconImageSource="favorite.png"
                            BackgroundColor="LightGreen"
                            Command="{Binding Source={x:Reference collectionView}, Path=BindingContext.FavoriteCommand}"
                            CommandParameter="{Binding}" />
                <SwipeItem Text="Delete"
                            IconImageSource="delete.png"
                            BackgroundColor="LightPink"
                            Command="{Binding Source={x:Reference collectionView}, Path=BindingContext.DeleteCommand}"
                            CommandParameter="{Binding}" />
            </SwipeItems>
        </SwipeView.LeftItems>
 ...

ContentPage 将OpacitySwipeChanging调用时更改。并且在调用时SwipeEnded设置Opacity1

private void SwipeView_SwipeEnded(object sender, SwipeEndedEventArgs e)
{
    Console.WriteLine("SwipeView_SwipeEnded");
    var swipView = sender as SwipeView;
    
    swipView.Opacity = 1;

}

private void SwipeView_SwipeChanging(object sender, SwipeChangingEventArgs e)
{
    var swipView = sender as SwipeView;
    
    swipView.Opacity = e.Offset/ swipView.Width;
}

效果:

在此处输入图像描述


推荐阅读