C# WinRT-更新进度条
我在为C# WinRT-更新进度条,c#,asynchronous,windows-8,progress-bar,C#,Asynchronous,Windows 8,Progress Bar,我在为ProgressBar制作动画时遇到问题 我的目标是,我希望ProgressBar.Value在每次处理copyanc和CreateFolderAsync时更新 我的xaml文件中有4个组件,它们将在每次进程完成时更新(CopySync和CreateFolderAsync),TextBlock运行正常,它总是在每次进程完成时更新。问题出在ProgressBar中,它将在所有过程结束时更新UI 我使用的是Dispatcher.RunAsync,我在其中加入了TextBlock和Progres
ProgressBar
制作动画时遇到问题
我的目标是,我希望ProgressBar.Value
在每次处理copyanc
和CreateFolderAsync
时更新
我的xaml文件中有4个组件,它们将在每次进程完成时更新(CopySync
和CreateFolderAsync
),TextBlock
运行正常,它总是在每次进程完成时更新。问题出在ProgressBar
中,它将在所有过程结束时更新UI
我使用的是Dispatcher.RunAsync
,我在其中加入了TextBlock
和ProgressBar
的更新过程
请告知如何更新以下代码的ProgressBar
。
MainPage.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);
}
}
}
private async void CopyFolder(字符串路径)
{
IStorageFolder destination=ApplicationData.Current.LocalFolder;
IStorageFolder root=Package.Current.InstalledLocation;
if(path.Equals(ROOT)&&!wait FolderExistAsync(ROOT))
等待目的地。CreateFolderAsync(根);
destination=wait destination.GetFolderAsync(路径);
root=等待root.GetFolderAsync(路径);
IReadOnlyList items=await root.GetItemsAsync();
//计算总文件数
if(路径等于(根))
总文件(路径);
foreach(项目中的IStorageItem项目)
{
if(item.GetType()==typeof(StorageFile))
{
IStorageFile presFile=Wait StorageFile.GetFileFromApplicationUriAsync(
新Uri(“ms appx://“+path.Replace(“\\”,“/”+“/”+item.Name));
如果(!await FileExistAsync(路径、项目名称))
{
IStorageFile copyFile=wait presFile.CopyAsync(目标);
countFiles++;
if(copyFile!=null)
{
等待Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
() =>
{
//TextBlock的更新很好
Files.Text=countFiles.ToString();
//但对于ProgressBar,它将在进程结束时更新
FileBar.Value=countFiles/totalFiles*100;
});
}
}
}
其他的
{
如果(!await FolderExistAsync(路径+“\\”+项.名称))
{
StorageFolder createFolder=Wait destination.CreateFolderAsync(item.Name);
countFolders++;
如果(createFolder!=null)
{
等待Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
() =>
{
//TextBlock的更新很好
Folders.Text=countFolders.ToString();
//但对于ProgressBar,它将在进程结束时更新
FolderBar.Value=countFolders/totalFolders*100;
});
}
}
CopyFolder(路径+“\\”+项名称);
}
}
}
countFiles
和totalFiles
都是整数,因此当您将其中一个除以另一个时,它将执行整数除法;由于totalFiles
始终大于或等于countFiles
,因此结果始终为0,除非在结尾处为1
要修复它,您需要在除法之前强制转换为double
,以便执行浮点除法:
FileBar.Value = (double)countFiles / totalFiles * 100;
您不应该通过直接通过代码设置组件的属性来更新组件。我强烈建议您看看mvvm模式()或mvvm灯()