简体   繁体   English

在C#WPF中远程打开后无法关闭窗口

[英]Unable to close window after remotely opening in C# WPF

I'm making a program for fun and im trying to call a window by using NotficationWindow.Show() and inside it I have it to close after a set time using sleep and i keep getting this error: Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed. 我正在制作一个有趣的程序,并试图通过使用NotficationWindow.Show()来调用窗口,并且在其内部,在设置的时间后使用sleep将其关闭,但我一直收到此错误: Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed. , not the best way but it's for private use so if it works them im happy, here's my code: ,不是最好的方法,而是供私人使用,因此,如果它们使我很不高兴,这是我的代码:
MainWindow: 主窗口:

private void ShowNoti_Click(object sender, RoutedEventArgs e)
{
    XuriNotification Noti = new XuriNotification();
    Noti.Show();
}

XuriNotification.xaml.cs: XuriNotification.xaml.cs:

public XuriNotification()
{
    InitializeComponent();
    var desktopWorkingArea = System.Windows.SystemParameters.WorkArea;
    this.Left = desktopWorkingArea.Right - this.Width;
    this.Top = desktopWorkingArea.Bottom - this.Height;

    System.Threading.Thread.Sleep(2000);
    System.Windows.Forms.Application.Exit();
}

It would be better to add a DispatchTimer class to the XuriNotification class, and set its interval. 最好将DispatchTimer类添加到XuriNotification类,并设置其间隔。 Then in it's Tick event, close the notification: 然后在Tick事件中,关闭通知:

System.Windows.Threading.DispatcherTimer dispatcherTimer;

public XuriNotification()
{
    InitializeComponent();
    var desktopWorkingArea = System.Windows.SystemParameters.WorkArea;
    this.Left = desktopWorkingArea.Right - this.Width;
    this.Top = desktopWorkingArea.Bottom - this.Height;

    dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
    dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
    dispatcherTimer.Interval = new TimeSpan(0,0,2);
    dispatcherTimer.Start();

}

private void dispatcherTimer_Tick(object sender, EventArgs e)
{
    System.Windows.Forms.Application.Exit();
}

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

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