c# - WinRT - Update Progress Bar -
i got problem when doing animation progressbar.
my objective is, i want progressbar.value update every time process copyasync , createfolderasync.
i have 4 components in xaml file update every time process done (copyasync , createfolderasync), textblock running fine, update every time process done. problem in progressbar update ui in end of process.
i'm using dispatcher.runasync, , update process textblock , progressbar put in there.
please advise, how update progressbar code below.
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(); // count 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, () => { // update textblock fine files.text = countfiles.tostring(); // progressbar update in 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, () => { // update textblock fine folders.text = countfolders.tostring(); // progressbar update in end of process folderbar.value = countfolders / totalfolders * 100; }); } } copyfolder(path + "\\" + item.name); } } }
countfiles , totalfiles both integers, when divide 1 other, performs integer division; since totalfiles greater or equal countfiles, result 0, except @ end it's 1.
to fix it, need cast double before divide, in order perform floating point division:
filebar.value = (double)countfiles / totalfiles * 100;
Comments
Post a Comment