如何显示ProgressBar并使其进展?(Xamarin.Android)

时间:2018-01-09 08:28:01

标签: xamarin.android progress-bar

我调用一个获取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的主题。相反,我想在ProgressBar内置一些简单的Xamarin.Android。例如,当下载第一个文件时,我想要填充圆的1/4,当下载2个文件时要填充的圆的1/2等等。类似地,对于水平ProgressBar

3 个答案:

答案 0 :(得分:3)

使用<?php namespace App\Http\Requests\Ad; use App\Models\Ads\Ad; use App\Traits\Validation; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Support\Facades\Auth; class AdRequest extends FormRequest { use Validation; public function authorize() { // #1 try to get user with Auth (gave null) dd(Auth::guard('api')->user()); // #2 try to get use with auth() helper (gave null) dd(auth()->guard('api')->user()) // #3 try to get just check auth()->user (gave null) dd(auth()->user()) or dd(Auth::user()) // #4 try to get user with request (gave null) $user = $this->user('api'); dd($user);

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

答案 1 :(得分: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?

答案 2 :(得分: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);
    });
}

但是即使那样,我的行为也有些不一致-可能还有一些时间上的限制...