2014-01-14 27 views
1

當隱藏窗口時,如何保持活動狀態,我正在嘗試此代碼。如何讓隱藏的窗口在wpf中保持活動狀態?

Win1:包含當前時間和日期。 ON按鈕單擊win2窗口打開並win1隱藏。

Win2:在win2窗口中,我使用屬性傳遞了win1文本框的值。

但WIN2的時候是不會得到更新..這意味着WIN1是不是活了

有什麼建議?

{ 
    public win1() 
    { 
     InitializeComponent(); 

     DispatcherTimer timer = new DispatcherTimer(new TimeSpan(0,0,1), DispatcherPriority.Normal, delegate 
     { 
      this.textb.Text = DateTime.Now.ToString("HH:mm:ss tt"); 
     }, this.Dispatcher); 
    } 

    private void Grid_Loaded(object sender, RoutedEventArgs e) 
    { 

    } 

    private void but_send_Click(object sender, RoutedEventArgs e) 
    { 
     win2 w2 = new win2(); 
     w2.passval = textb.Text; 
     this.Hide(); 
     w2.ShowDialog(); 
    } 
} 

,這是我的第二個窗口win2.xaml.cs

{ 
    private string nm; 

    public string passval 
    { 
     get { return nm; } 
     set { nm = value;} 
    }  

    public win2() 
    { 
     InitializeComponent(); 

    } 

    private void Grid_Loaded(object sender, RoutedEventArgs e) 
    { 
     lbl1.Content = "Time " + nm; 
    }  
} 

回答

0

的問題是,你正在更新Win1時的TextBox。下面的行副本位於textb.Textw2.passval字符串:

w2.passval = textb.Text; 

但就是這樣:進一步更新textb.Textw2.passval性能沒有影響。

此外,保留在Win1中的引用並直接從您的代理更新w2.passval,也不會更新lbl1的內容,因爲您只在加載網格時更新其值。因此你需要:

  1. 更新w2.passval從委託在Win1。例如:

    public win1()   
    { 
        InitializeComponent(); 
    
        DispatcherTimer timer = new DispatcherTimer(new TimeSpan(0,0,1), DispatcherPriority.Normal, delegate 
        { 
         this.textb.Text = DateTime.Now.ToString("HH:mm:ss tt"); 
         if (_w2 != null) 
          _w2.passval = textb.Text; 
        }, this.Dispatcher); 
    } 
    
    private win2 _w2 = null; // Reference to your Win2 
    
    private void but_send_Click(object sender, RoutedEventArgs e) 
    { 
        _w2 = new win2(); 
        _w2.passval = textb.Text; 
        this.Hide(); 
        w2.ShowDialog(); 
    } 
    
  2. 更新LabelWin2。例如:

    private string nm; 
    
    public string passval 
    { 
        get { return nm; } 
        set { 
         nm = value; 
         // This should rather be done somewhere else, not in the setter 
         lbl1.Content = "Time " + nm; 
        } 
    } 
    
+0

謝謝你這麼多你的時間Jnovo ......我知道了,現在它的工作:)撲通起來 – Antisan

相關問題