【问题标题】:How to make ProgressBar progress in alertDialog?(Xamarin.Android)如何在 alertDialog 中使 ProgressBar 进度?(Xamarin.Android)
【发布时间】:2018-04-02 12:13:43
【问题描述】:

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

我希望进度条在我指定的地方增加。但是当我运行应用程序时,我只有无限循环的进度条。我想逐渐增加。并在达到100后消失。我在 Xamarin.Android 中找到的信息太少了,但对我没有任何帮助。

【问题讨论】:

    标签: xamarin.android progress-bar


    【解决方案1】:

    在你找到viewbyreference的那一行之后:

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

    【讨论】:

    • 进步不是一点点一点一点地增加的
    • 可能任务执行得非常快,以至于您无法看到逐步更新的进度。尝试添加延迟,检查我编辑的答案。
    • 我已经在每个IncrementProgressBy 调用之后或之前放置了Thread.Sleep(2000),但它仍然会立即增加进度。
    • 能否请您发布您的 xaml 和 *.cs 文件。
    【解决方案2】:

    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>
    

    【讨论】:

      猜你喜欢
      • 2018-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多