2012-12-24 40 views
0

我在這裏要求一些關於C#和特別是WPF的幫助。 :DC# - WPF - 從另一個類更新進度條

我有一個WPF項目,我需要更新另一個類的進度條的值。進度條在'NdWindow'類中,我需要從'Automator'類中更新它的值。

我已經嘗試了一些東西,但沒有爲我工作。

在 'NdWindow' 類:

public partial class NdWindow : Window 
{ 
    public NdWindow() 
    { 
     InitializeComponent(); 
    } 

    public NdWindow(int progress) 
    { 
     InitializeComponent(); 
     setprogress(progress); 
    } 

    public void setprogress(int progress) 
    { 
     this.progressBar.Value = progress; 
     MessageBox.Show(Convert.ToString(progress)); 
    } 

而在 'Automator的' 類:

public static void post() 
{ 
    NdWindow test = new NdWindow(); 
    test.setprogress(10); 
} 

如果我運行該程序,消息框彈出窗口,並顯示我的價值我已經setprogress內發送()。 我也嘗試在構造函數中發送值,但它沒有幫助。

請幫助我,如果你可以。 :D

謝謝!

PS:「發佈」功能是通過點擊按鈕執行的。我沒有在這裏寫這個代碼。我希望這對你來說不是問題。 :-)

+0

檢查該解決方案http://stackoverflow.com/questions/5789926/update-a-progressbar-from-another-thread?rq = 1 – Nogard

回答

3

在你的post方法中,你創建新的NdWindow,女巫不是你想要改變進度條值的窗口。

您應該以某種方式獲得NdWindow,Automator類。

public class Automator 
{ 
    private NdWindow ndWindow; 
    public Automator(NdWindow ndwindow) 
    { 
     this.ndWindow = ndwindow; 
    } 

    public void Post() 
    { 
     ndWindow.setprogress(10); 
    } 
} 

public partial class NdWindow : Window 
{ 
    private Automator automator; 
    public NdWindow() 
    { 
     InitializeComponent(); 
     this.automator = new Automator(this); 
    } 

    private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     Automator.Post(); 
    } 
} 

或者你可以發送NdWindow您post方法

public class Automator 
{ 
    public static void Post(NdWindow ndWindow) 
    { 
     ndWindow.setprogress(10); 
    } 
} 

public partial class NdWindow : Window 
{ 
    private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     Automator.Post(this); 
    } 
} 
+0

非常感謝!您的解決方案完美運作:-D – RazvanR104