UWP - ProgressBar 可见值仅在 Sub 结束时更改

UWP - ProgressBar visible value change only at the end of the Sub

我写了这个示例来说明我的问题。 我有一个进度条(在 async Sub 中,但问题也在 Sync 中) 当我增加该值时,我在视频上看不到任何内容。

只有在 Sub 的末尾我才能看到现在位于值顶部的进度条。 为什么?

Private Sub Btn_ProgressBar_Click(sender As Object, e As RoutedEventArgs) Handles Btn_ProgressBar.Click

    Me.BarraProgresso.Minimum = 0
    Me.BarraProgresso.Maximum = 100
    Me.BarraProgresso.Value = 0

    For i As Integer = 1 To 1000
        If i / 100 = CInt(i / 100) Then
            Me.BarraProgresso.Value = CInt(i / 100)
        End If

        For KK As Integer = 0 To 9000000
            Dim _MyApple As String = "Red Apple"
        Next
    Next

End Sub

您的按钮单击处理程序在 UI 线程上同步调用,您正在忙着等待。在您的处理程序 returns 之前,不会呈现任何内容。这个效果更好:

private async void BtnProgressBarClick(object sender, RoutedEventArgs e)
{
    this.BarraProgresso.Minimum = 0;
    this.BarraProgresso.Maximum = 1000;
    this.BarraProgresso.Value = 0;

    for (int i = 0; i < 1000; i++)
    {
        this.BarraProgresso.Value = i;
        await Task.Delay(10); // Gives rendering a chance to run
    }
}

这是 C# 而不是 VB,但我想翻译是可以管理的。 :-)