2017-05-28 95 views
0

我有一個窗口,其中包含popup。當執行操作時,這個彈出窗口被稱爲。現在我想例如當用戶連續數據和顯示彈出窗口insert,關閉,因爲碰撞發生。 我的代碼是:Wpf如何自動關閉上一個彈出窗口,當啓動新的彈出窗口

public partial class AvinPopup : Window 
{ 
    static AvinPopup _popup; 
    static int timePopup = 0; 
    static string textPopUp = ""; 

    private AvinPopup() 
    { 
     InitializeComponent(); 
    } 
    private static void StartCloseTimer() 
    { 
     DispatcherTimer timer = new DispatcherTimer(); 
     timer.Interval = TimeSpan.FromSeconds((double)timePopup); 
     timer.Tick += TimerTick; 
     timer.Start(); 
    } 

    private static void TimerTick(object sender, EventArgs e) 
    { 
     DispatcherTimer timer = (DispatcherTimer)sender; 
     timer.Stop(); 
     timer.Tick -= TimerTick; 
     _popup.Close(); 
     _popup.popup.IsOpen = false; 

    } 
    public static void Show(string _textPopup, int _timePopup = 3) 
    { 
     timePopup = _timePopup; 
     textPopUp = _textPopup; 

     Thread newWindowThread = new Thread(ThreadStartPopup); 
     newWindowThread.SetApartmentState(ApartmentState.STA); 
     newWindowThread.IsBackground = true; 
     newWindowThread.Start(); 
    } 

    private static void ThreadStartPopup() 
    { 

     _popup = new AvinPopup(); 
     _popup.popup.VerticalOffset = System.Windows.SystemParameters.PrimaryScreenHeight - 200; 
     _popup.popup.HorizontalOffset = 100; /*System.Windows.SystemParameters.PrimaryScreenWidth +100;*/ 
     _popup.txtPopup.Text = textPopUp; 
     _popup.Show(); 
     StartCloseTimer(); 
     System.Windows.Threading.Dispatcher.Run(); 
    } 
+0

這裏是你的答案https://stackoverflow.com/questions/8828240/listen-to-dependencyproperty-changed-event-and-get-the-old-value – Karolis

+0

爲什麼不檢查你的Show方法或ThreadStartPopup方法是否彈出窗口已經打開或沒有打開? – elgonzo

+0

@elgonzo如何檢查它? – Mohadeseh

回答

0
private static readonly ManualResetEventSlim _Blocker = new ManualResetEventSlim(false); 

private static void ThreadStartPopup() 
{ 
    _Blocker.Reset(); 
    System.Windows.Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => 
    { 
     if (_popup != null && _popup.IsOpen) 
      _popup.IsOpen = false; 

     _Blocker.Set(); 
    })); 

    _Blocker.Wait(); 
    _popup = new AvinPopup(); 
    _popup.popup.VerticalOffset = System.Windows.SystemParameters.PrimaryScreenHeight - 200; 
    _popup.popup.HorizontalOffset = 100; /*System.Windows.SystemParameters.PrimaryScreenWidth +100;*/ 
    _popup.txtPopup.Text = textPopUp; 
    _popup.Show(); 
    StartCloseTimer(); 
    System.Windows.Threading.Dispatcher.Run(); 
} 
+0

我得到錯誤:調用線程不能訪問這個對象,因爲不同的線程擁有它。 – Mohadeseh

+0

好的,我編輯了我的例子。現在新的'Thread'正在激活'ManualResetEventSlim' Blocker。然後調用一個'Dispatcher Operation'並在完成之後,新的'Thread'繼續。 –

+0

再次得到這個錯誤。錯誤來自以下代碼:'if(_popup!= null && _popup.popup.IsOpen)'和'_popup.popup.IsOpen'爲null。說明錯誤:** StackTrace \t「System.Windows.Threading.Dispatcher.VerifyAccess()System.Windows.Threading.DispatcherObject.VerifyAccess()at System.Windows.DependencyObject.GetValue(DependencyProperty dp)at System.Windows.Controls .Primitives.Popup.get_IsOpen()在<> x。<> m0(<> c <> 4__this)string ** – Mohadeseh