异步方法C#中的UWP进度条

时间:2016-06-16 18:09:19

标签: c# uwp uwp-xaml

我正在开发处理图像的应用程序。图像处理需要一段时间(大约10秒钟)。我想添加一个进度条来完成,除非图像处理完成。我在xaml中有这个进度条

<ProgressBar Width="200"                               
Foreground="#FF8B64C3"
Value="20" 
Maximum="100" 
BorderBrush="#FF411F72" 
BorderThickness="1"/>

这是单击按钮进行图像处理时调用的事件

private void ProcessImageButton_Click(object sender, RoutedEventArgs e)
{          
    applyFilters(image1Pixels, image1Width, image1Height);          
}

我想在单击此ProcessImageButton按钮时启动进度条。这是applyFilters方法。

 private async void applyFilters(byte[] pixels, uint width, uint height)
 {
        ProcessImage processImage = new ProcessImage(pixels, width, height);

        byte[] effect = processImage.applyEffect(width, height);

        WriteableBitmap result_image = new WriteableBitmap((int)width, (int)height);
        using (Stream stream = result_image .PixelBuffer.AsStream())
        {
            await stream.WriteAsync(effect, 0, effect.Length);
            MainImage.Source = result_image ;
        }
 }

我希望在将result_image存储到MainImage.Source之前完成进度条。

2 个答案:

答案 0 :(得分:2)

您可以使用不确定的进度条

<ProgressBar x:Name="ImageProgressBar" Visibility="Collapsed" IsIndeterminate="true"/>

并且您需要在加载图像之前和之后更改可见性

        ImageProgressBar.Visibility = Visibility.Visible;

        ImageProgressBar.Visibility = Visibility.Collapsed;

答案 1 :(得分:1)

您可以使用<ProgressRing x:Name="progress" Height="50" Width="50" IsActive="False" />

并在操作开始时设置progress.IsActive = true;

progress.IsActive = false;

在操作结束时