我正在尝试使进度条在C# WPF中工作,但它不起作用。
我的 xaml
<ProgressBar
Height="15"
Maximum="100"
Minimum="0"
Value="{Binding Path=Progress, Mode=OneWay}" />
我的视图模型
public ShellViewModel()
{
loadAirportInfosBgWk = new BackgroundWorker();
loadAirportInfosBgWk.DoWork += loadAirportInfosBgWk_DoWork;
loadAirportInfosBgWk.ProgressChanged += loadAirportInfosBgWk_ProgressChanged;
loadAirportInfosBgWk.RunWorkerCompleted += loadAirportInfosBgWk_RunWorkerCompleted;
loadAirportInfosBgWk.WorkerReportsProgress = true;
loadAirportInfosBgWk.RunWorkerAsync();
}
private void loadAirportInfosBgWk_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
IsBusy = false;
}
private void loadAirportInfosBgWk_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
Progress = e.ProgressPercentage;
Console.WriteLine($"{e.ProgressPercentage}%");
}
private void loadAirportInfosBgWk_DoWork(object sender, DoWorkEventArgs e)
{
IsBusy = true;
// Do stuff
loadAirportInfosBgWk.ReportProgress(prog); // prog is int between 0 and 100
}
// _ variables are private variable at the top I omitted them
public bool IsBusy
{
get { return _isBusy; }
set {
_isBusy = value;
NotifyOfPropertyChange(() => IsBusy);
}
}
public int Progress
{
get { return _progress; }
set {
if (_progress != value)
{
_progress = value;
NotifyOfPropertyChange(() => Progress);
}
}
}
奇怪的是,RunWorkerCompleted
正确隐藏了加载组件,但ProgressChanged
不会更新任何内容。但是控制台在完成的功能中正确记录了该值。 我真的是卡利本微的新手,希望我能得到它并正确地做它。我查看了堆栈溢出,但没有找到帮助答案。
**编辑:**
我尝试在标签中显示属性Progress
,它正在正确更新,但不在进度条值中。 可能是由于我在数据模板中使用进度条的方式引起的。
详细信息 xaml :
<xctk:BusyIndicator
Grid.RowSpan="2"
Margin="0,0,-0.2,25.6"
Panel.ZIndex="5"
DisplayAfter="0"
IsBusy="{Binding IsBusy}">
<xctk:BusyIndicator.BusyContentTemplate>
<DataTemplate>
<StackPanel Margin="4">
<TextBlock
HorizontalAlignment="Center"
FontWeight="Bold"
Text="Downloading Email" />
<StackPanel Margin="4">
<TextBlock Text="Chargement des données..." />
<ProgressBar
Height="15"
Maximum="100"
Value="{Binding Path=Progress}" />
</StackPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button
Grid.Column="0"
Margin="0,0,2,0"
HorizontalAlignment="Right"
Content="Pause" />
<Button
Grid.Column="1"
Margin="2,0,0,0"
HorizontalAlignment="Left"
Content="Cancel" />
</Grid>
</StackPanel>
</DataTemplate>
</xctk:BusyIndicator.BusyContentTemplate>
<xctk:BusyIndicator.OverlayStyle>
<Style TargetType="Rectangle">
<Setter Property="Fill" Value="#ffffeeee" />
</Style>
</xctk:BusyIndicator.OverlayStyle>
<xctk:BusyIndicator.ProgressBarStyle>
<Style TargetType="ProgressBar">
<Setter Property="Visibility" Value="Collapsed" />
</Style>
</xctk:BusyIndicator.ProgressBarStyle>
</xctk:BusyIndicator>
谢谢 一个。
通过在BusyIndicator
中添加BusyContent="{Binding}"
来解决:
<xctk:BusyIndicator
Grid.RowSpan="2"
Margin="0,0,-0.2,25.6"
Panel.ZIndex="5"
BusyContent="{Binding}"
DisplayAfter="0"
IsBusy="{Binding IsBusy}">
<!-- Code -->
</xctk:BusyIndicator>