C# 显示特定时段的WPF弹出窗口
我很难让弹出窗口在特定时间内保持打开状态。我有一个派生的弹出类,并把它放在打开的事件中C# 显示特定时段的WPF弹出窗口,c#,wpf,popup,C#,Wpf,Popup,我很难让弹出窗口在特定时间内保持打开状态。我有一个派生的弹出类,并把它放在打开的事件中 private void Popup_Opened(object sender, EventArgs e) { DispatcherTimer time = new DispatcherTimer(); time.Interval = TimeSpan.FromSeconds(5); time.Start(); time.Tick
private void Popup_Opened(object sender, EventArgs e)
{
DispatcherTimer time = new DispatcherTimer();
time.Interval = TimeSpan.FromSeconds(5);
time.Start();
time.Tick += delegate
{
this.IsOpen = false;
};
}
它工作得很好,但每个会话只运行一次。此后,每当调用popup实例时,它都会显示从闪烁到几秒钟之间的任何内容。有人知道为什么吗
我还尝试在触发弹出窗口的事件中使用几乎相同的代码,而不是在弹出窗口本身的打开事件中,即
myPopup.IsOpen = true;
DispatcherTimer time = new DispatcherTimer();
time.Interval = TimeSpan.FromSeconds(5);
time.Start();
time.Tick += delegate
{
myPopup.IsOpen = false;
};
相同的结果。您需要停止计时器,否则它将继续滴答作响并再次尝试关闭弹出窗口。请参阅以下代码
public class Popupex : Popup
{
public Popupex()
{
this.Opened += Popupex_Opened;
}
void Popupex_Opened(object sender, EventArgs e)
{
DispatcherTimer time = new DispatcherTimer();
time.Interval = TimeSpan.FromSeconds(10);
time.Start();
time.Tick += delegate
{
this.IsOpen = false;
time.Stop();
};
}
}
试试这个链接。已经是这样了。我已经看过了。这是一个完全不同的问题,即如何延迟打开弹出窗口。我想把它打开x秒。请参考下面我的答案。谢谢。我的大脑有问题。