如何在 WinRT 中更新基于窗体的进度条?

How to update a progress bar based on a form in WinRT?

我有一个包含几个 TextBox 元素和一个进度条的表单。我希望在 TextBox 分配了一些值时更新进度条。

我注意到在 WPF 中这可以使用 BackgroundWorker 实现,但在 WinRT 中不存在

XAML:

<StackPanel>
    <ProgressBar x:Name="progressBar1" 
                 Value="{Binding ProgressPercent}"  
                 HorizontalAlignment="Left" 
                 IsIndeterminate="False" 
                 Maximum="100" />

    <TextBlock Text="Name" Grid.Column="0"/>
    <TextBox x:Name="NameTextBox" Text="{Binding Name, Mode=TwoWay}"/>

    <TextBlock Text="Data" Grid.Row="1" Grid.Column="0"/>
    <DatePicker Date="{Binding Data, Mode=TwoWay}" 
                HorizontalAlignment="Stretch" 
                VerticalAlignment="Center" 
                Grid.Row="1" 
                Grid.Column="1" 
                CalendarIdentifier="GregorianCalendar" 
                Margin="0,8" />

    <TextBlock Text="Address" Grid.Row="2" Grid.Column="0"/>
    <TextBox x:Name="AddressTextBox" 
             Text="{Binding Address, Mode=TwoWay}" 
             Grid.Row="2" 
             Grid.Column="1" 
             Margin="0,5" />

    <TextBlock Text="Email" Grid.Row="3" Grid.Column="0"/>
    <TextBox x:Name="EmailTextBox" 
             Text="{Binding Email, Mode=TwoWay}" 
             Grid.Row="3" 
             Grid.Column="1" 
             Margin="0,5" />
</StackPanel>

ViewModel:

#region fields

private string progressPercent {get;set;}

#endregion

#region proprietes
 public int ProgressPercent
    {
        get
        {
            return this.progressPercent;
        }
        set
        {
            this.progressPercent = value;
            this.RaisePropertyChanged(() => this.ProgressPercent);
        }
    }
#endregion

这在 WinRT 中如何实现?不幸的是,大多数示例都是针对 Wpf 的

你错过了 INotifyPropertyChanged。否则,进度条将不会从您的 ViewModel 获取更新的值,也不会显示任何进度。

你可以阅读更多INotifyPropertyChanged here.

每次表单的一个字段更新时,需要将百分比值添加到 ProgressPercent 属性,我认为不需要异步代码,但如果您在这里坚持这样做去做

 private string _name = "";
    public string Name
    {
        get
        {
            return _name;
        }

        set
        {
            if (_name == value)
            {
                return;
            }
            _name = value;
            OnPropertyChanged();
            Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
            {
                ProgressPercent += 10; //handle the fact that this needs to be added once using a bool or something 
            });

        }
    }