简体   繁体   中英

ProgressBar with BackgroundWorker MVVM

I just recently started getting acquainted with progress bar. I have an MVVM project How can I use ICommand in DoWork ? I have many functions for exporting, adding, downloading, etc (considering that all calculations will be inside these functions).

Can I use them like this in DoWork ?

private readonly BackgroundWorker worker;
private int progress;
public int CurrentProgress
{
    get { return this.progress; }
    set
    {
        if (this.progress != value)
        {
            this.progress = value;
            OnPropertyChanged("CurrentProgress");
        }
    }
}

private void ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    CurrentProgress = e.ProgressPercentage;
}

private void DoWork(object sender, DoWorkEventArgs e)
{
   ICommand ExportXml;
   ICommand DownloadFile;
   ICommand AddFile;
}

public ICommand ExportXml
{
    get
    {
        return new Command(() =>
        {
            SaveFileDialog saveXml = new SaveFileDialog()
            {
                Filter = ".xml file|*.xml",
                DefaultExt = ".xml"
            };
            if (saveXml.ShowDialog() == true)
            {
               ExportXml(saveXml.Filename);
            }
        });
    }
}

You can export anonymous metod to named method and then can use this method in code.

public ICommand ExportXmlCommand => new Command(ExportXml);

private void ExportXml()
{
    SaveFileDialog saveXml = new SaveFileDialog()
    {
        Filter = ".xml file|*.xml",
        DefaultExt = ".xml"
    };
    if (saveXml.ShowDialog() == true)
    {
        ExportXml(saveXml.Filename);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM