Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ViewModel WPF中将commandparameter传递给Backgroundworker_C#_Wpf_Backgroundworker - Fatal编程技术网

C# 在ViewModel WPF中将commandparameter传递给Backgroundworker

C# 在ViewModel WPF中将commandparameter传递给Backgroundworker,c#,wpf,backgroundworker,C#,Wpf,Backgroundworker,我的WPF应用程序的主窗口上有许多按钮。这些按钮的命令应具有相同的实现/功能,但根据按下的按钮,功能访问的文件/路径会发生变化。如何使用CommandParameter从ViewModel中检测单击了哪个按钮?如何在方法Dowork中使用此参数? 在本例中,Button1的CommandParameter称为“Button1”,Button2的CommandParameter称为“Button2” 她是我的ViewModel中Backgroundworker的代码: public ViewMod

我的WPF应用程序的主窗口上有许多按钮。这些按钮的命令应具有相同的实现/功能,但根据按下的按钮,功能访问的文件/路径会发生变化。如何使用CommandParameter从ViewModel中检测单击了哪个按钮?如何在方法Dowork中使用此参数? 在本例中,Button1的CommandParameter称为“Button1”,Button2的CommandParameter称为“Button2”

她是我的ViewModel中Backgroundworker的代码:

public ViewModel()
    {
        ...

        this.instigateWorkCommand = new DelegateCommand(o => this.worker.RunWorkerAsync(), o => !this.worker.IsBusy);
        this.worker = new BackgroundWorker();
        this.worker.DoWork += this.DoWork;
        this.worker.ProgressChanged += this.ProgressChanged;
        this.worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_Completeted);
        this.worker.WorkerReportsProgress = true;
    }
private void DoWork(object sender, DoWorkEventArgs e)
    {
        // if (parameter.ToString().contains("button1")...
        // if (parameter.ToString().contains("button2")...
    }
她是我的ViewModel中的嫁妆代码:

public ViewModel()
    {
        ...

        this.instigateWorkCommand = new DelegateCommand(o => this.worker.RunWorkerAsync(), o => !this.worker.IsBusy);
        this.worker = new BackgroundWorker();
        this.worker.DoWork += this.DoWork;
        this.worker.ProgressChanged += this.ProgressChanged;
        this.worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_Completeted);
        this.worker.WorkerReportsProgress = true;
    }
private void DoWork(object sender, DoWorkEventArgs e)
    {
        // if (parameter.ToString().contains("button1")...
        // if (parameter.ToString().contains("button2")...
    }
通常使用该方法将参数传递给
DoWork
方法

this.worker.RunWorkerAsync("button1");
dowworkeventargs
中,属性包含传递给方法RunWorkerAsync的值

private void DoWork(object sender, DoWorkEventArgs e)
{
    if (e.Argument == "button1"){

    }
}

我建议,当您
使用任何按钮运行
worker时,将参数作为对象传递给您的方法。

请查看此处