[编辑]
我用完整的代码和解释编辑了我的问题,希望有些东西可以给我更清楚的解释。
我有以下具有后台工作程序的类来跟踪循环的百分比进度并更新ProgressWin的XAML上的标签上的百分比进度。以下代码完美无缺。 (我的问题远远低于代码之后。)
public partial class ProgressWin : Window
{
BackgroundWorker backgroundWorker1 = new BackgroundWorker();
public ProgressWin()
{
InitializeComponent();
InitializeBackgroundWorker();
startAsync();
}
// Set up the BackgroundWorker object by
// attaching event handlers.
private void InitializeBackgroundWorker()
{
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.WorkerSupportsCancellation = true;
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
}
private void startAsync()
{
backgroundWorker1.RunWorkerAsync();
}
public void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 1; i <= 10; i++)
{
System.Threading.Thread.Sleep(500);
worker.ReportProgress(i * 10);
}
}
// This event handler updates the progress.
public void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label1.Content = (e.ProgressPercentage.ToString() + "%");
}
// This event handler deals with the results of the background operation.
public void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
resultLabel.Content = "Done!";
}
}
这是我的问题,现在不是跟踪ProgressWin中的循环,而是需要跟踪我的MainWindow中的循环:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
for (int i = 1; i <= 10; i++)
{
System.Threading.Thread.Sleep(500);
????.ReportProgress(i * 10);
}
}
}
我不知道从哪里开始。我尝试从ProgressWin实例化一个对象并调用DoWork,但我最终冻结了ProgressWin窗口。
答案 0 :(得分:1)
你可以通过
简单地调用另一个类的方法 backgroudnWorker.DoWork += new DoWorkEventHandler(SomeClass.SomeStaticMethod);
或
backgroudnWorker.DoWork += new DoWorkEventHandler(SomeClassInstance.SomeMethod);
要从另一个类MainWindow
调用ProgressScreen
类的方法,您应该在MainWindow
中引用ProgressScreen
类的实例,然后使用该实例,您可以调用任何公共来自MainWindow
类
ProgressScreen
方法
和Oliver Said一样,您需要backgroundworker
的实例从其他方法发送进度更新。
答案 1 :(得分:1)
由于问题被重写,我也重写了我的整个答案。
为了让这个工作正常,您的MainWindow
有一个 ProgressWindow
,并且应该像背景工作者一样使用它:
public partial class MainWindow : Window
{
private ProgressWindow _Progress;
public MainWindow()
{
InitializeComponent();
_Progress = new ProgressWindow();
_Progress.ProgressChanged += OnProgressChanged;
}
private void OnProgressChanged(object sender, ProjectChangedEventArgs e)
{
//ToDo: Update whatever you like in your MainWindow
}
}
要完成此操作,您的ProgressWindow
应该只订阅工作人员事件并重新抛出它:
public partial class ProgressWin : Window
{
// Add this to your class above in your question
public event ProgressChangedEventHandler ProgressChanged;
// Change or merge this with your existing function
private void backgroundWorker1_ProgressChanged(object sender, ProjectChangedEventArgs e)
{
var temp = ProgressChanged;
if(temp !=null)
temp(this, e);
}
}