【问题标题】:Wpf how to automatically closed previous popup when start new popupWpf如何在启动新弹出窗口时自动关闭上一个弹出窗口
【发布时间】:2025-12-03 07:45:01
【问题描述】:

我有一个包含popup的窗口。当执行操作时,这个弹出窗口被调用。现在我想例如当用户连续insert数据并显示弹出窗口时,之前调用的弹出窗口关闭。因为发生冲突。 我的代码是:

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

【问题讨论】:

  • 这是你的答案*.com/questions/8828240/…
  • 为什么不检查你的 Show 方法或 ThreadStartPopup 方法是否已经打开了弹出窗口......?
  • @elgonzo 如何查看?

标签: c# wpf popup


【解决方案1】:
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();
}

【讨论】:

  • 我收到错误:调用线程无法访问此对象,因为不同的线程拥有它。
  • 好的,我已经编辑了我的示例。现在新的Thread 正在激活ManualResetEventSlim 阻止程序。然后调用Dispatcher Operation,完成后,新的Thread 将继续。
  • 再次收到此错误。错误来自此代码:if (_popup != null && _popup.popup.IsOpen)_popup.popup.IsOpen 为空。描述错误:System.Windows.Threading.DispatcherObject.VerifyAccess() 的 System.Windows.Threading.Dispatcher.VerifyAccess() 的 System.Windows.DependencyObject.GetValue(DependencyProperty dp) 的 StackTrace" System.Windows.Controls .Primitives.Popup.get_IsOpen() at x.m0(c 4__this)string
最近更新 更多