ProgressBar WPF

3

Hello, I have seen several examples on the internet of progressBar in WPF, but none works.

It appears more does not fill in the values, in fact it fills the values only after running the initial method where it is called.

Source Code:

 public partial class Apresentacao_ProgressBar : Window
{
    private BackgroundWorker backgroundWorker1 = new BackgroundWorker();

    public Apresentacao_ProgressBar()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(Form1_Shown);

        backgroundWorker1.WorkerReportsProgress = true;
        backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
        backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
    }

    void Form1_Shown(object sender, EventArgs e)
    {
        backgroundWorker1.RunWorkerAsync();
    }

    void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i = 0; i <= 100; i++)
        {            
            backgroundWorker1.ReportProgress(i);              
            System.Threading.Thread.Sleep(100);
        }
    }

    void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        PBar.Value = e.ProgressPercentage;
    }
}
    
asked by anonymous 31.08.2017 / 19:57

1 answer

1

1 - BackgroundWorker is currently in disuse.

2 - Use MVVM when working with WPF, it was made to work perfectly with this template.

3 - To run the ProgressBar you need to do the work on another thread ( BackgroundWorker does this but as it says it is deprecated)

follows an example:

private Task _task;

public string Arquivo
{
    get => _arquivo;
    set
    {
        if (value == _arquivo) return;
        _arquivo = value;
        OnPropertyChanged();
    }
}

public double ProgressMax
{
    get => _progressMax;
    set
    {
        if (value.Equals(_progressMax)) return;
        _progressMax = value;
        OnPropertyChanged();
    }
}

public double ProgressValue
{
    get => _progressValue;
    set
    {
        if (value.Equals(_progressValue)) return;
        _progressValue = value;
        OnPropertyChanged();
    }
}

private void Importar(string arquivo = "")
{
    //Minhas verificações e outras coisas que não preciso mostrar

    ProgressValue = 0;

    //Aqui verifico se o arquivo está ok (no meu projeto o arquivo vem de um download, 
    //   então não preciso verificar aqui se ele existe pois o serviço de download já verifica)

    if (string.IsNullOrEmpty(fileName)) return;  //fileName é uma variavel com um arquivo a ser importado
    try
    {
        if ((_task != null) && (_task.IsCompleted == false ||
                                _task.Status == TaskStatus.Running ||
                                _task.Status == TaskStatus.WaitingToRun ||
                                _task.Status == TaskStatus.WaitingForActivation))
            return;  //se já houver um thread para essa variavel _task ele não inicia outro

        _task = Task.Factory.StartNew(TratarArquivo);  //Inicia o Thread
    }
    catch (Exception e)
    {
        ExibirMensagem(Uteis.RetornaMsgErro(e), TipoMensagem.Erro);
    }

}

private void TratarArquivo()
{
    try
    {
        ProgressVisibility = true;

        var i = 0;
        var j = 0;


        var dados = LeArquivo(Arquivo);

        ProgressMax = dados.Count();

        foreach (var linha in dados)
        {
            ProgressValue = j;
            j++;
            //Aqui tem várias coisas que são tratadas, nao importa nesse exemplo
        }
    }
    catch (Exception e)
    {
        ExibirMensagem(Uteis.RetornaMsgErro(e), TipoMensagem.Erro);
    }
    finally
    {
        ProgressVisibility = false;
        ProgressValue = 0;
    }
}

Q: If you do not understand why the properties implement OnPropertyChanged (), you should know more about MVVM and INotifyPropertyChanged

Q.: This data is of class ImportacaoViewModel , I DO NOT have any lines added to the code behind

Q: If you wanted a complete but very simple example of how to do, send me an email celsolivero.no.gmail.com

    
17.11.2017 / 16:37