Xamarin形式的Animation和GridLength

时间:2018-09-12 11:22:58

标签: animation xamarin.forms gridlength

我有Model课

public class Model : INotifyPropertyChanged
...
private GridLength detailsPanelHeight { get; set; }
public GridLength DetailsPanelHeight
{
    get { return detailsPanelHeight; }
    set
    {
        if (!GridLength.Equals(detailsPanelHeight, value))
        {
            detailsPanelHeight = value;
            OnPropertyChanged("DetailsPanelHeight");
        }
    }
}
...

XAML代码的一部分:

<RowDefinition Height="{Binding DetailsPanelHeight}" />

执行动画的代码(平滑更改行高):

var animate = new Animation(d => currentItem.DetailsPanelHeight = d, 0, 100);
animate.Commit(this, "ExpandAnimation", 50, 1000, Easing.SpringOut);

代码以折叠行: var animate = new Animation(d => currentItem.DetailsPanelHeight = d, 100, 0); animate.Commit(this, "CollapseAnimation", 50, 1000, Easing.SpringOut);

它第一次工作,但是第二次出现错误:“值小于0或不是数字\ n参数名称:值”。我看到d的值小于零。

我该怎么做才能解决此问题?

1 个答案:

答案 0 :(得分:1)

我使用了对我来说非常有效的类似方法。我希望它也适合您。

此动画在 delete 动作调用后调用命令时会折叠视图单元格。这是代码:

点击事件处理程序:

private async void RemoveButtonTapped(object sender, EventArgs e)
{
    Parallel.Invoke(() =>
        {
             if (RemoveCommand?.CanExecute(RemoveCommandParameter) ?? false)
                 RemoveCommand.Execute(RemoveCommandParameter);
        },
        AnimatedDestruction);
}

动画方法

private async void AnimatedDestruction()
{
    uint transitionTime = 300;
    decimal delayFactor = 1.2m;

    // Note: stackPanel is the viewCell's top-level container

    await Task.WhenAll(
        stackPanel.FadeTo(0, Convert.ToUInt32(transitionTime * delayFactor), Easing.CubicInOut),
        View.InterpolateValue(stackPanel.Height, 0, Transition, transitionTime, Easing.CubicInOut)
        );
}

过渡回调函数

private void Transition(double value)
{
    const double minHeightValue = 0.001;

    value = value <= minHeightValue ? minHeightValue : value;

    Height = value;
    ForceUpdateSize();
}   

InterpolateValue作为扩展方法(非常可重用)

public static Task<bool> InterpolateValue(this View view, double initialValue, double endValue, Action<double> transformIteration, uint length, Easing easing)
{
    Task<bool> ret = new Task<bool>(() => false);

    if (!view.AnimationIsRunning(nameof(InterpolateValue)))
    {
        try
        {
            easing = easing ?? Easing.Linear;
            var taskCompletionSource = new TaskCompletionSource<bool>();

            view.Animate(nameof(InterpolateValue), ((_double) => initialValue - (initialValue * _double)), transformIteration, 16, length, easing, (v, c) => taskCompletionSource.SetResult(c));
            ret = taskCompletionSource.Task;
        }
        catch
        {
            // supress animation overlapping errors 
        }
    }

    return ret;
}

我希望它对您有用。