0

我在为ProgressBar.

我的目标是,我希望ProgressBar.Value每次处理时都进行更新,CopyAsync并且CreateFolderAsync.

我的 xaml 文件中有 4 个组件,每次处理完成时都会更新(CopyAsyncCreateFolderAsync),TextBlock运行良好,每次处理完成时都会更新。问题在于ProgressBar它将在所有过程结束时更新 UI。

我正在使用Dispatcher.RunAsync,并且我将更新过程TextBlock放在ProgressBar那里。

请告知,如何更新ProgressBar下面的代码。


主页.xaml

<TextBlock Text="Files:" FontSize="72" Margin="363,270,834,402"></TextBlock>
<TextBlock Text="Folders:" FontSize="72" Margin="273,411,834,270"></TextBlock>
<TextBlock x:Name="Files" FontSize="72" Margin="582,270,609,402"></TextBlock>
<TextBlock x:Name="Folders" FontSize="72" Margin="582,411,588,270"></TextBlock>
<ProgressBar x:Name="FolderBar" Height="25" Margin="10,532,-10,211"></ProgressBar>
<ProgressBar x:Name="FileBar" Height="25" Margin="10,565,-10,178"></ProgressBar>

MainPage.xaml.cs

private async void CopyFolder(string path)
{
    IStorageFolder destination = ApplicationData.Current.LocalFolder;
    IStorageFolder root = Package.Current.InstalledLocation;

    if (path.Equals(ROOT) && !await FolderExistAsync(ROOT))
        await destination.CreateFolderAsync(ROOT);

    destination = await destination.GetFolderAsync(path);
    root = await root.GetFolderAsync(path);

    IReadOnlyList<IStorageItem> items = await root.GetItemsAsync();

    // For count the total files
    if (path.Equals(ROOT))
        TotalFiles(path);

    foreach (IStorageItem item in items)
    {
        if (item.GetType() == typeof(StorageFile))
        {
            IStorageFile presFile = await StorageFile.GetFileFromApplicationUriAsync(
                new Uri("ms-appx:///" + path.Replace("\\", "/") + "/" + item.Name));

            if (!await FileExistAsync(path, item.Name))
            {
                IStorageFile copyFile = await presFile.CopyAsync(destination);
                countFiles++;

                if (copyFile != null)
                {
                    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
                        () =>
                        {
                            // The update for TextBlock is fine
                            Files.Text = countFiles.ToString();
                            // But for the ProgressBar it will update in the end of process
                            FileBar.Value = countFiles / totalFiles * 100;
                        });
                }

            }
        }
        else
        {
            if (!await FolderExistAsync(path + "\\" + item.Name))
            {
                StorageFolder createFolder = await destination.CreateFolderAsync(item.Name);
                countFolders++;

                if (createFolder != null)
                {
                    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
                        () =>
                        {
                            // The update for TextBlock is fine
                            Folders.Text = countFolders.ToString();
                            // But for the ProgressBar it will update in the end of process
                            FolderBar.Value = countFolders / totalFolders * 100;
                        });
                }

            }

            CopyFolder(path + "\\" + item.Name);
        }
    }

}
4

1 回答 1

1

countFilestotalFiles都是整数,所以当你将一个除以另一个时,它会执行整数除法;因为totalFiles总是大于或等于countFiles,所以结果总是 0,除了最后是 1。

要修复它,您需要double在除法之前强制转换为,以便执行浮点除法:

FileBar.Value = (double)countFiles / totalFiles * 100;
于 2013-08-16T10:24:37.300 回答