2017-01-18 100 views
0

我正在製作一個有趣的程序,並試圖通過使用NotficationWindow.Show()調用一個窗口,並在它內部使用睡眠設置時間後關閉它,並且不斷收到此消息錯誤:Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed.,不是最好的方式,但它是供私人使用,所以如果它的工作原理他們我很高興,這是我的代碼:
主窗口: 在C#中遠程打開後無法關閉窗口WPF

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

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

回答

1

將DispatchTimer類添加到XuriNotification類並設置其時間間隔會更好。然後在它的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(); 
} 
+0

我使用的代碼和我沒有錯誤,但窗口永遠不會關閉 – ZerterCodes

+0

沒關係,我固定它 – ZerterCodes