Caliburn micro中的ProgressChanged不会更新进度栏

问题描述 投票:0回答:1

我试图使进度条在C#WPF中工作,但不起作用。

我的xaml

<ProgressBar
    Height="15"
    Maximum="100"
    Minimum="0"
    Value="{Binding Path=Progress, Mode=OneWay}" />

我的ViewModel

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不会更新任何内容。但是控制台在完成功能后会正确记录该值。我对Caliburn micro真的很陌生,希望我能得到并正确地做。我查看了堆栈溢出,但没有找到帮助的答案。

**编辑:**

我试图在标签中显示属性Progress,并且正在正确更新,但在进度栏值中未正确显示。可能是由于我在DataTemplate中使用进度条的方式引起的。

Detail 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>

谢谢,A。

c# wpf data-binding backgroundworker caliburn.micro
1个回答
0
投票

通过在BusyContent="{Binding}"中添加BusyIndicator解决:

<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>
© www.soinside.com 2019 - 2024. All rights reserved.