简体   繁体   English

如何在WPF中使隐藏的窗口在背景中存活?

[英]How to keep a hidden window alive in background in wpf?

How can I keep a window alive when it is hidden, I'm trying this code out .. 当窗口隐藏时如何保持活动状态,我正在尝试此代码..

Win1 : Contains Current time and date. Win1:包含当前时间和日期。 ON Button Click the win2 window opens and win1 Hides. ON按钮单击win2窗口将打开,并且win1隐藏。

Win2 : In win2 window i have passed the value of the win1 textbox using property. Win2:在win2窗口中,我已使用属性传递了win1文本框的值。

But in win2 the time is not getting updated .. this means the win1 is not alive anymore 但是在win2中,时间没有更新..这意味着win1不再存在

Any suggestions? 有什么建议么?

{
    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();
    }
}

and this is my second window win2.xaml.cs 这是我的第二个窗口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;
    }    
}

The problem is that you are updating Win1's TextBox . 问题是您正在更新Win1的TextBox The following line copies the string located in textb.Text into w2.passval : 下一行将位于textb.Text的字符串textb.Textw2.passval

w2.passval = textb.Text;

But that's it: further updates to textb.Text has no effect on the w2.passval property. 就是这样:进一步更新textb.Textw2.passval属性没有影响。

Furthermore, keeping a reference to w2 in Win1 and directly updating w2.passval from your delegate, would also not update the contents of lbl1 since your are only updating its value when the Grid is loaded. 此外,保持一个参考w2Win1 ,直接更新w2.passval从您的委托,也将不会因为被加载网格时你只更新其值更新LBL1的内容。 Therefore you'll need to: 因此,您需要:

  1. Update w2.passval from the delegate in Win1 . 更新w2.passval从委托Win1 For instance: 例如:

     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. Update the Label in Win2 . Win2更新Label For instance: 例如:

     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; } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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