我在为ProgressBar
.
我的目标是,我希望ProgressBar.Value
每次处理时都进行更新,CopyAsync
并且CreateFolderAsync
.
我的 xaml 文件中有 4 个组件,每次处理完成时都会更新(CopyAsync
和CreateFolderAsync
),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);
}
}
}