如何显示进度条,并使其进步?(Xamarin.Android)

问题描述 投票:1回答:4

我调用Web服务,得到4个文件,虽然这些文件加载​​我想展现给用户(圆形或水平也没关系)的进展。我已经按照网上的例子,但没有出现在屏幕上。

MobileSellReference.Service1 service = new MobileSellReference.Service1();
service.Url = settings.Synchronization.Msellurl;
ProgressBar progressBar = FindViewById<ProgressBar>(Resource.Id.progressBar);
progressBar.Max = 100;
progressBar.Progress = 0;
byte[][] basedataResult = service.ToPPC(basedataZipName, objectId);
progressBar.IncrementProgressBy(25);
byte[][] dutybasedataResult = service.ToPPC(dutybasedataZipName, objectId);
progressBar.IncrementProgressBy(25);
byte[][] tranbasedataResult = service.ToPPC(tranbasedataZipName, objectId);
progressBar.IncrementProgressBy(25);
byte[][] vendbasedataResult = service.ToPPC(vendbasedataZipName, objectId);
progressBar.IncrementProgressBy(25);

我发现了很多使用外部进度库的例子,但他们都希望改变Activity的主题。相反,我想建成ProgressBar一些简单的Xamarin.Android。例如,当第一个文件被下载我想圆的1/4将被填充,当2个文件被下载圆的1/2等等填补。类似地,对于水平ProgressBar

xamarin.android progress-bar
4个回答
3
投票

使用AsyncTask

.axml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="0" />
    <ProgressBar
        android:id="@+id/pb"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal" />
</LinearLayout>

MainActivity

public class MainActivity : Activity
{
    ProgressBar pb;
    TextView tv;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        pb = FindViewById<ProgressBar>(Resource.Id.pb);
        tv = FindViewById<TextView>(Resource.Id.tv);
        UpdatePB uptask = new UpdatePB(this,pb,tv);
        uptask.Execute(100);
    }

   public class UpdatePB : AsyncTask<int, int, string>
    {
        Activity mcontext;
        ProgressBar mpb;
        TextView mtv;
        public UpdatePB(Activity context,ProgressBar pb,TextView tv) {
            this.mcontext = context;
            this.mpb = pb;
            this.mtv = tv;
        }
        protected override string RunInBackground(params int[] @params)
        {
            // TODO Auto-generated method stub
            for (int i = 1; i <= 4; i++)
            {
                try
                {
                    Thread.Sleep(3000);
                }
                catch (InterruptedException e)
                {
                    // TODO Auto-generated catch block
                    Android.Util.Log.Error("lv",e.Message);
                }
                mpb.IncrementProgressBy(25);
                PublishProgress(i * 25);

            }
            return "finish";
        }

        protected override void OnProgressUpdate(params int[] values)
        {
            mtv.Text = String.ValueOf(values[0]);
            Android.Util.Log.Error("lv==", values[0] + "");
        }
        protected override void OnPostExecute(string result)
        {
            mcontext.Title = result;
        }


    }
}

1
投票

这可能是一个有用的链接:

https://developer.android.com/reference/android/widget/ProgressBar.html

码:

 <ProgressBar
  android:id="@+id/determinateBar"
  style="@android:style/Widget.ProgressBar.Horizontal"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:progress="25"/>

...然后你只需要改变的进展情况; 25,50,75,100?


0
投票

运行到同样的问题后,我发现了另一个解决方案,得到它的工作。我舍不得定义一个新类(类似的AsyncTask)来解决这个问题,所以看着异步等待和线程。我发现,在Android.Widget.ProgressBar布局文件中定义了以下这样一个.axml后:

<ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:progress="0"
    style="?android:attr/progressBarStyleHorizontal" />

我能得到它的更新,如果我把更新任务在System.Threading.Tasks.Task.Run和传递,做与RunOnUiThread调用,比如更新的操作:

btnDoStuff.Click += (sender, e) =>
{
    Task.Run(() =>
    {
        RunOnUiThread(() => 
        {
            progressBar.Max = 100;
            progressBar.Progress = 0;
            progressBar.Visibility = ViewStates.Visible;
        });

        DoSomeWork1(arguments);
        RunOnUiThread(() => progressBar.Progress += 25);

        DoSomeWork2(arguments);
        RunOnUiThread(() => progressBar.Progress += 25);

        DoSomeWork3(arguments);
        RunOnUiThread(() => progressBar.Progress += 25);

        DoSomeWork4(arguments);
        RunOnUiThread(() => progressBar.Progress += 25);
    });
}

但即使如此,我已经有一些不一致的行为 - 有可能是时间给它的元素,以及...

© www.soinside.com 2019 - 2024. All rights reserved.