【问题标题】:How to show ProgressBar and make it progress?(Xamarin.Android)如何显示 ProgressBar 并使其取得进展?(Xamarin.Android)
【发布时间】:2018-01-09 08:28:01
【问题描述】:

我调用了一个获取 4 个文件的 Web 服务,当这些文件正在加载时,我想向用户显示进度(圆形或水平的都没有关系)。我已经按照互联网上的示例进行操作,但屏幕上没有显示任何内容。

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 的主题。相反,我想要在Xamarin.Android 中内置一些简单的ProgressBar。例如,当下载第一个文件时,我希望填充圆圈的 1/4,当下载 2 个文件时,要填充圆圈的 1/2 等等。同样对于水平的ProgressBar

【问题讨论】:

标签: xamarin.android progress-bar


【解决方案1】:

使用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;
        }
       

    }
}

【讨论】:

  • 不知道可以把你给我的ProgressBar放到AlertDialog里吗?
  • 我看到了你的另一个问题,我正在尝试。你解决了这个问题吗?还是您尝试过我的解决方案?
  • 您的解决方案正在运行,但如果我在 AlertDialog 中执行它会很棒,因为这样我会阻止用户在 ProgressBar 进行时单击其他按钮。
  • 好的,如果此解决方案适合您,请给我投票或接受,谢谢。
  • 我知道这是一个旧的,但它不适合我。我正在加载数据,然后以编程方式构建我的主要活动。没有显示进度条。知道为什么吗?
【解决方案2】:

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

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?

【讨论】:

    【解决方案3】:

    遇到同样的问题后,我找到了另一个解决方案。我不愿意定义一个新类(如 AsyncTask)来解决这个问题,所以研究了异步等待和线程。我发现在.axml 布局文件中定义了Android.Widget.ProgressBar 之后,如下所示:

    <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);
        });
    }
    

    但即便如此,我也有一些不一致的行为 - 也可能存在时间因素......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-17
      • 1970-01-01
      • 2018-06-18
      • 2014-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多