Xamarin ProgressBar绑定进度

问题描述 投票:0回答:2

ProgressBar的Progress属性由名为UploadProgressCallback的方法更新,该方法由UploadProgressChanged事件调用。一切正常,但我注意到ProgressBar直接从0变为1,似乎没有平稳更新,因此我决定分析操作输出,并意识到将发送字节除以文件大小后的结果被截断为0,因此得出了一些结果值应该为0.234或0.643,进入Progress属性为“ 0”,这会导致小节一直停留在初始点,当上传完成时,除法显然会返回1,小节将被填充。我尝试了很多事情,但输出始终为0。有人能解决这个问题吗?

xaml上的ProgressBar视图

<ProgressBar IsVisible="{Binding UploadProgressVisibility}" IsEnabled="{Binding UploadProgressVisibility}" FlowDirection="LeftToRight" ProgressColor="Orange" Progress="{Binding UploadProgress}" Grid.Column="0" Grid.Row="1" HeightRequest="20" WidthRequest="80" Margin="0" />

UploadProgressCallback

webclient.UploadProgressChanged += new UploadProgressChangedEventHandler((s,e) => UploadProgressCallback(s,e,idrefecence));

private void UploadProgressCallback(object sender, UploadProgressChangedEventArgs e, int idreference)
{
    double temp = (double) e.BytesSent / filesize;
    Device.BeginInvokeOnMainThread(() => 
    {
        valuesLock.EnterWriteLock();
        try
        {
           MyCollection[idreference].UploadProgress = temp;
           // this value is being bound to Progress property of ProgressBar 
        }
        finally { valuesLock.ExitWriteLock(); }
    });
}

也尝试过:

double temp = Convert.ToDouble((double)e.BytesSent / filesize, CultureInfo.InvariantCulture);

使用此方法,我可以获得正确的结果,但小数点分隔符是','而不是'。'

c# xamarin double progress-bar
2个回答
0
投票

一切正常,但我注意到ProgressBar直接从0变为1,似乎没有平稳的更新

如果您希望Progressbar顺利更新,建议您使用JimBobBennett.AnimatedProgress来实现动画。

首先,通过nuget软件包安装JimBobBennett.AnimatedProgress,然后在xaml中引用此dll。

然后将此附加属性用于ProgressBar。

   <ProgressBar
            jbb:AttachedProperties.AnimatedProgress="{Binding Source={x:Reference entry1}, Path=Text}"
            jbb:AttachedProperties.AnimatedProgressAnimationTime="1000"
            jbb:AttachedProperties.AnimatedProgressEasing="BounceOut"
            ProgressColor="Red" />

        <Entry x:Name="entry1" />

截图:

enter image description here

请看附件属性:

  public static class AttachedProperties
{
    public static BindableProperty AnimatedProgressProperty =
       BindableProperty.CreateAttached("AnimatedProgress",
       typeof(double),
       typeof(ProgressBar),
       0.0d,
       BindingMode.OneWay,
       propertyChanged: (b, o, n) => ProgressBarProgressChanged((ProgressBar)b, (double)n));

    public static BindableProperty AnimatedProgressAnimationTimeProperty =
       BindableProperty.CreateAttached("AnimatedProgressAnimationTime",
       typeof(int),
       typeof(ProgressBar),
       800,
       BindingMode.OneWay);

    public static BindableProperty AnimatedProgressEasingProperty =
       BindableProperty.CreateAttached("AnimatedProgressEasing",
       typeof(string),
       typeof(ProgressBar),
       default(string),
       BindingMode.OneWay);

    public static double GetAnimatedProgress(BindableObject target) => (double)target.GetValue(AnimatedProgressProperty);
    public static void SetAnimatedProgress(BindableObject target, double value) => target.SetValue(AnimatedProgressProperty, value);

    public static int GetAnimatedProgressAnimationTime(BindableObject target) => (int)target.GetValue(AnimatedProgressAnimationTimeProperty);
    public static void SetAnimatedProgressAnimationTime(BindableObject target, int value) => target.SetValue(AnimatedProgressAnimationTimeProperty, value);

    public static string GetAnimatedProgressEasing(BindableObject target) => (string)target.GetValue(AnimatedProgressEasingProperty);
    public static void SetAnimatedProgressEasing(BindableObject target, string value) => target.SetValue(AnimatedProgressEasingProperty, value);

    private static void ProgressBarProgressChanged(ProgressBar progressBar, double progress)
    {
        ViewExtensions.CancelAnimations(progressBar);

        var animationTime = GetAnimatedProgressAnimationTime(progressBar);
        var easingName = GetAnimatedProgressEasing(progressBar);

        progressBar.ProgressTo(progress, Convert.ToUInt32(Math.Max(0, animationTime)), GetEasing(easingName));
    }

    private static Easing GetEasing(string easingName)
    {
        if (easingName.ToUpper() == nameof(Easing.BounceIn).ToUpper())
            return Easing.BounceIn;
        if (easingName.ToUpper() == nameof(Easing.BounceOut).ToUpper())
            return Easing.BounceOut;
        if (easingName.ToUpper() == nameof(Easing.CubicIn).ToUpper())
            return Easing.CubicIn;
        if (easingName.ToUpper() == nameof(Easing.CubicOut).ToUpper())
            return Easing.CubicOut;
        if (easingName.ToUpper() == nameof(Easing.CubicInOut).ToUpper())
            return Easing.CubicInOut;
        if (easingName.ToUpper() == nameof(Easing.Linear).ToUpper())
            return Easing.Linear;
        if (easingName.ToUpper() == nameof(Easing.SinIn).ToUpper())
            return Easing.SinIn;
        if (easingName.ToUpper() == nameof(Easing.SinOut).ToUpper())
            return Easing.SinOut;
        if (easingName.ToUpper() == nameof(Easing.SinInOut).ToUpper())
            return Easing.SinInOut;
        if (easingName.ToUpper() == nameof(Easing.SpringIn).ToUpper())
            return Easing.SpringIn;
        if (easingName.ToUpper() == nameof(Easing.SpringOut).ToUpper())
            return Easing.SpringOut;

        return Easing.SinIn;
    }
}

0
投票

int除以​​int将返回int。您需要先将其中一个值转换为double,然后除法将返回double。不需要Convert

double temp = ((double)e.BytesSent) / filesize;
© www.soinside.com 2019 - 2024. All rights reserved.