我有一个包含少量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>
视图模型:
#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的
答案 0 :(得分:2)
每次更新表单的某个字段时,需要将百分比值添加到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
});
}
}
答案 1 :(得分:1)
您错过了INotifyPropertyChanged
。如果没有这个,进度条不会从ViewModel获取更新的值,也不会显示任何进度。
您可以阅读更多INotifyPropertyChanged
here.