如何在alertDialog中使ProgressBar进度?(Xamarin.Android)

时间:2018-04-02 12:13:43

标签: xamarin.android progress-bar

我在alertDialog.SetPositiveButton

中有以下代码
alertConfirmTransfer.SetPositiveButton("ДА", delegate
{
    ProgressBar progressBar = FindViewById<ProgressBar>(Resource.Id.progressBar);
    alertConfirmTransfer.Cancel();
    MobileSellReference.Service1 service = new 
    MobileSellReference.Service1();
    progressBar.IncrementProgressBy(10);
    service.Url = settings.Synchronization.Msellurl;
    progressBar.IncrementProgressBy(10);
    byte[][] resultFromService = service.ToPPC(basedataZipName, objectId);
    progressBar.IncrementProgressBy(10);
    byte[] basedataZipFile = resultFromService[0];
    byte[] dutybasedataZipFile = resultFromService[3];
    byte[] tranbasedataZipFile = resultFromService[2];
    byte[] vendbasedataZipFile = resultFromService[1];
    progressBar.IncrementProgressBy(10);

    string basedataZipFullPath = GlobalVariables.fromserverFolderPath + "/" + basedataZipName;
    string dutybasedataZipFullPath = GlobalVariables.fromserverFolderPath + "/" + dutybasedataZipName;
    string tranbasedataZipFullPath = GlobalVariables.fromserverFolderPath + "/" + tranbasedataZipName;
    string vendbasedataZipFullPath = GlobalVariables.fromserverFolderPath + "/" + vendbasedataZipName;

    Helper.DeleteAllFromFolders(GlobalVariables.fromserverFolderPath, GlobalVariables.vendingFolderPath, GlobalVariables.tranFolderPath, GlobalVariables.debtFolderPath);
    progressBar.IncrementProgressBy(10);
    Helper.EmptyMobileSellDB();
    progressBar.IncrementProgressBy(10);
    System.IO.File.WriteAllBytes(basedataZipFullPath, basedataZipFile);
    progressBar.IncrementProgressBy(10);
    System.IO.File.WriteAllBytes(dutybasedataZipFullPath, dutybasedataZipFile);
    progressBar.IncrementProgressBy(10);
    System.IO.File.WriteAllBytes(tranbasedataZipFullPath, tranbasedataZipFile);
    progressBar.IncrementProgressBy(10);
    System.IO.File.WriteAllBytes(vendbasedataZipFullPath, vendbasedataZipFile);
    progressBar.IncrementProgressBy(10);
}

我希望进度条在我指定的位置增加。但是当我运行应用程序时,我只有infinly循环进度条。我想逐渐增加。达到100后消失。我发现在Xamarin.Android中如何做到这一点的信息太少,而且没有一个帮助过我。

2 个答案:

答案 0 :(得分:0)

在您找到视图的行之后:

ProgressBar progressBar = FindViewById<ProgressBar>(Resource.Id.progressBar);

将最大进度设置为100,然后将当前进度设置为0.

progressBar.Max = 100;
progressBar.Progress = 0;

然后递增进度应该有效。

progressBar.IncrementProgressBy(10);

//尝试在设置进度之前/之后添加延迟。 //您可以将以下代码放在方法中,并在更新进度条后调用该方法。

              try
                {
                    Thread.Sleep(2000);
                }
                catch (Exception exception)
                {
                    Android.Util.Log.Error("Error",exception.Message);
                }

答案 1 :(得分:0)

cs文件

MobileSellReference.Service1 service = new MobileSellReference.Service1();
service.Url = settings.Synchronization.Msellurl;
progressBar.IncrementProgressBy(10);
Thread.Sleep(2000);

byte[][] resultFromService = service.ToPPC(basedataZipName, objectId);
progressBar.IncrementProgressBy(50);
Thread.Sleep(2000);

byte[] basedataZipFile = resultFromService[0];
byte[] dutybasedataZipFile = resultFromService[3];
byte[] tranbasedataZipFile = resultFromService[2];
byte[] vendbasedataZipFile = resultFromService[1];

string basedataZipFullPath = GlobalVariables.fromserverFolderPath + "/" + basedataZipName;
string dutybasedataZipFullPath = GlobalVariables.fromserverFolderPath + "/" + dutybasedataZipName;
string tranbasedataZipFullPath = GlobalVariables.fromserverFolderPath + "/" + tranbasedataZipName;
string vendbasedataZipFullPath = GlobalVariables.fromserverFolderPath + "/" + vendbasedataZipName;

Helper.DeleteAllFromFolders(GlobalVariables.fromserverFolderPath,         GlobalVariables.vendingFolderPath, GlobalVariables.tranFolderPath,     GlobalVariables.debtFolderPath);
progressBar.IncrementProgressBy(5);
Thread.Sleep(2000);
Helper.EmptyMobileSellDB();
progressBar.IncrementProgressBy(5);
Thread.Sleep(2000);
System.IO.File.WriteAllBytes(basedataZipFullPath, basedataZipFile);
progressBar.IncrementProgressBy(10);
Thread.Sleep(2000);
System.IO.File.WriteAllBytes(dutybasedataZipFullPath, dutybasedataZipFile);
progressBar.IncrementProgressBy(10);
Thread.Sleep(2000);
System.IO.File.WriteAllBytes(tranbasedataZipFullPath, tranbasedataZipFile);
progressBar.IncrementProgressBy(10);
Thread.Sleep(2000);
System.IO.File.WriteAllBytes(vendbasedataZipFullPath, vendbasedataZipFile);

上面的代码位于alert.SetPositiveButton("Yes" ....)方法

axml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<LinearLayout
    android:orientation="vertical"
    android:gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/linearLayout1">
    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar" />
    <Button
        android:text="Взема данни"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnGetData" />
    <Button
        android:text="Предава данни"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnTransferData" />
    <Button
        android:text="Пълна синхронизация"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnFullSynchronization" />
</LinearLayout>