Xamarin Forms 如何实现淡出效果隐藏?

4

在我的当前应用程序中,我有一堆按钮,可以隐藏或显示它们对应的stackLayout。 首先,我尝试使用IsVisible属性,但这会导致闪烁, 现在我正在使用LayoutTo(),但仍然会闪烁?

我的代码如下:

async void btnStrike_Clicked(object sender, EventArgs args)
        {
            var layout = this.FindByName<StackLayout>("stkStrikeInfo");
            var rect = new Rectangle(layout.X, layout.Y, layout.Width, layout.Height - layout.Height);
            await layout.LayoutTo(rect, 2500, Easing.Linear);
        }

我想要动态改变高度!

编辑:

我找到了以下代码,它将Stacklayout从页面中移除。但问题是视图没有更新?

1个回答

14

我认为如果您使用默认动画将要隐藏的布局的高度减少到零,那么您会有更好的运气。

void btnStrike_Clicked(object sender, EventArgs args)
{
    // get reference to the layout to animate
    var layout = this.FindByName<StackLayout>("stkStrikeInfo");

    // setup information for animation
    Action<double> callback = input => { layout.HeightRequest = input; }; // update the height of the layout with this callback
    double startingHeight = layout.Height; // the layout's height when we begin animation
    double endingHeight = 0; // final desired height of the layout
    uint rate = 16; // pace at which aniation proceeds
    uint length = 1000; // one second animation
    Easing easing = Easing.CubicOut; // There are a couple easing types, just tried this one for effect

    // now start animation with all the setup information
    layout.Animate("invis", callback, startingHeight, endingHeight, rate, length, easing);
}
如果布局已经被隐藏,而你想要显示它,你需要替换:

如果布局已经被隐藏,而你想要显示它,你需要替换

double startingHeight = layout.Height;
double endingHeight = 0;

需要翻译的内容:

with

double startingHeight = 0;
double endingHeight = 55;

55只是一个随意的高度,如果你想让它回到之前的高度,你需要在隐藏之前将先前的高度保存到一个变量中,并使用该保存的高度代替55。


你好,回调函数出现错误,提示“无法将 lambda 表达式分配给隐式类型的变量”。 - Abrar Ali
尝试使用错误的Animate重载,尝试在编辑中使用显式变量类型再次尝试。 - Will Decker
我该如何做相反的操作?是交换开始和结束吗? - Abrar Ali
让我们在聊天室里继续讨论 - Abrar Ali
1
对于实现此解决方案的其他人的有用提示:确保父视图的“IsClippedToBounds”设置为True,否则您将看到一些奇怪的行为,其中内容似乎延伸到父视图的新高度以下。 - PhillFox
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接