WinRT-更新进度条

本文关键字:更新 WinRT- | 更新日期: 2023-09-27 18:28:04

我在为ProgressBar制作动画时遇到了一个问题。

我的目标是,每次处理CopyAsyncCreateFolderAsync时,我都希望ProgressBar.Value更新

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

我使用的是Dispatcher.RunAsync,以及TextBlockProgressBar的更新过程。

请告知如何更新以下代码的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>

主页.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);
        }
    }
}

WinRT-更新进度条

countFilestotalFiles都是整数,所以当你把一个除以另一个时,它会执行整数除法;由于totalFiles总是大于或等于countFiles,所以结果总是0,除非在末尾为1。

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

FileBar.Value = (double)countFiles / totalFiles * 100;