2013-08-16 5 views
0

ProgressBar에 대한 애니메이션을 수행 할 때 문제가 발생했습니다.WinRT - 업데이트 진행률 막대

내 목적은, 는 내가 ProgressBar.Value 업데이 트가 CopyAsyncCreateFolderAsync을 처리 할 때마다합니다.

내가의 TextBlock 잘 실행되고있는 프로세스를 수행 할 때마다 (CopyAsyncCreateFolderAsync)를 업데이트 될 것입니다 내 XAML 파일의 4 개 구성 요소가 항상 프로세스가 완료 될 때마다 업데이트 할 수 있습니다. 문제는 ProgressBar에 있으며 모든 프로세스가 끝날 때 UI가 업데이트됩니다.

나는 Dispatcher.RunAsync을 사용 중이며 TextBlockProgressBar에 대한 업데이트 프로세스가 있습니다.

아래 코드는 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); 
     } 
    } 

} 
+0

코드를 통해 속성을 직접 설정하여 구성 요소를 업데이트하면 안됩니다. mvvm-pattern (http://en.wikipedia.org/wiki/Model_View_ViewModel) 또는 mvvm-light (http://mvvmlight.codeplex.com/) –

답변

1

countFilestotalFiles이 모두 정수 그래서 당신이 다른 하나를 분할 할 때, 정수 나누기를 수행합니다. totalFilescountFiles 항상보다 크거나 같은 때문에, 결과는 그것을 해결하려면 1.

을의 말을 제외하고 항상 0입니다, 당신은 당신이 분할 전에 부동 소수점을 수행하기 위해, double로 캐스팅 할 필요가 division :

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

아, 내 실수를 고맙게 생각합니다. 당신은 대답을 : D – Crazenezz