鼠标点击到达后窗口 WPF

发布于 2024-10-16 23:57:05 字数 3873 浏览 1 评论 0原文

我创建了一种“处理”对话框,它显示某个进程的进度。它包含一个进度条和一个标签。这是代码:

public class ProcessingDialog
    {
        FrontWindow win;
        public ProcessingDialog(int min, int max, string initialMessage,string title, bool isContineous)
        {
            Thread th= new Thread(new ThreadStart(delegate()
            {
                win = new FrontWindow();
                win.Title = title;
                if (isContineous == false)
                {
                    win.ProgressBarProgress.Minimum = min;
                    win.ProgressBarProgress.Maximum = max;
                }
                else
                    win.ProgressBarProgress.IsIndeterminate = true;

                win.LabelStatus.Content = initialMessage;
                win.ShowInTaskbar = false;
                win.Topmost = true;
                win.ShowDialog();
            }
            ));
            th.SetApartmentState(ApartmentState.STA);
            th.Start();
        }


        public void Increment(int margin)
        {
            win.Dispatcher.Invoke(new ThreadStart(delegate { win.ProgressBarProgress.Value+=margin; }), null); ;
        }

        public void Reset()
        {
            win.Dispatcher.Invoke(new ThreadStart(delegate { win.ProgressBarProgress.Value=0; }), null); ;
        }

        public void ChangeMessage(string message)
        {
            win.Dispatcher.Invoke(new ThreadStart(delegate { win.LabelStatus.Content=message; }), null); ;
        }


        public void Close()
        {
            win.Dispatcher.Invoke(new ThreadStart(delegate { win.Close(); }), null); ;
        }
    }

这是 FrontWindow 的标记:

<Window x:Class="WpfApplicationUnleashed.FrontWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="FrontWindow" Height="165" Width="300" WindowStyle="ThreeDBorderWindow" WindowStartupLocation="CenterOwner" ResizeMode="NoResize">
    <DockPanel>
        <ProgressBar x:FieldModifier="public" x:Name="ProgressBarProgress" Height="15" Margin="20" DockPanel.Dock="Top" VerticalAlignment="Top"></ProgressBar>
        <Label x:FieldModifier="public" x:Name="LabelStatus" Margin="10" VerticalAlignment="Top" HorizontalAlignment="Left" DockPanel.Dock="Bottom"></Label>
    </DockPanel>
</Window>

这是我显示对话框的窗口。它只包含一个按钮,其单击事件是这样的:

   private void button1_Click(object sender, RoutedEventArgs e)
        {
            ProcessingDialog pd = new ProcessingDialog(0, 10, "Starting", "Processing", false);
            Thread.Sleep(1000);

            pd.ChangeMessage("Step 1");
            pd.Increment(1);
            Thread.Sleep(1000);

            pd.ChangeMessage("Step 2");
            pd.Increment(1);
            Thread.Sleep(1000);

            pd.ChangeMessage("Step 3");
            pd.Increment(1);
            Thread.Sleep(1000);


            pd.ChangeMessage("Step 4");
            pd.Increment(1);
            Thread.Sleep(1000);


            pd.ChangeMessage("Step 5");
            pd.Increment(1);
            Thread.Sleep(1000);


            pd.ChangeMessage("Step 6");
            pd.Increment(1);
            Thread.Sleep(1000);


            pd.ChangeMessage("Step 7");
            pd.Increment(1);
            Thread.Sleep(1000);


            pd.ChangeMessage("Step 8");
            pd.Increment(1);
            Thread.Sleep(1000);


            pd.ChangeMessage("Step 9");
            pd.Increment(1);
            Thread.Sleep(1000);


            pd.ChangeMessage("Step 10");
            pd.Increment(1);
            Thread.Sleep(1000);

            pd.Close();


        }

问题是,当显示它时,如果我再次单击该按钮,当对话框关闭时,它会再次打开。我的意思是它不会被单击,因为它是模态的,但它只是排队,当对话框关闭时,它会被单击。

我怎样才能防止这种情况发生?

I have created a sort of "processing" dialogbox which displays the progess of a certain process.It contains one progressbar and one Label. Here is the code:

public class ProcessingDialog
    {
        FrontWindow win;
        public ProcessingDialog(int min, int max, string initialMessage,string title, bool isContineous)
        {
            Thread th= new Thread(new ThreadStart(delegate()
            {
                win = new FrontWindow();
                win.Title = title;
                if (isContineous == false)
                {
                    win.ProgressBarProgress.Minimum = min;
                    win.ProgressBarProgress.Maximum = max;
                }
                else
                    win.ProgressBarProgress.IsIndeterminate = true;

                win.LabelStatus.Content = initialMessage;
                win.ShowInTaskbar = false;
                win.Topmost = true;
                win.ShowDialog();
            }
            ));
            th.SetApartmentState(ApartmentState.STA);
            th.Start();
        }


        public void Increment(int margin)
        {
            win.Dispatcher.Invoke(new ThreadStart(delegate { win.ProgressBarProgress.Value+=margin; }), null); ;
        }

        public void Reset()
        {
            win.Dispatcher.Invoke(new ThreadStart(delegate { win.ProgressBarProgress.Value=0; }), null); ;
        }

        public void ChangeMessage(string message)
        {
            win.Dispatcher.Invoke(new ThreadStart(delegate { win.LabelStatus.Content=message; }), null); ;
        }


        public void Close()
        {
            win.Dispatcher.Invoke(new ThreadStart(delegate { win.Close(); }), null); ;
        }
    }

This is the markup for FrontWindow:

<Window x:Class="WpfApplicationUnleashed.FrontWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="FrontWindow" Height="165" Width="300" WindowStyle="ThreeDBorderWindow" WindowStartupLocation="CenterOwner" ResizeMode="NoResize">
    <DockPanel>
        <ProgressBar x:FieldModifier="public" x:Name="ProgressBarProgress" Height="15" Margin="20" DockPanel.Dock="Top" VerticalAlignment="Top"></ProgressBar>
        <Label x:FieldModifier="public" x:Name="LabelStatus" Margin="10" VerticalAlignment="Top" HorizontalAlignment="Left" DockPanel.Dock="Bottom"></Label>
    </DockPanel>
</Window>

And here is the Window from where I show the dialogbox.It just cantains one buttons whose click event is this:

   private void button1_Click(object sender, RoutedEventArgs e)
        {
            ProcessingDialog pd = new ProcessingDialog(0, 10, "Starting", "Processing", false);
            Thread.Sleep(1000);

            pd.ChangeMessage("Step 1");
            pd.Increment(1);
            Thread.Sleep(1000);

            pd.ChangeMessage("Step 2");
            pd.Increment(1);
            Thread.Sleep(1000);

            pd.ChangeMessage("Step 3");
            pd.Increment(1);
            Thread.Sleep(1000);


            pd.ChangeMessage("Step 4");
            pd.Increment(1);
            Thread.Sleep(1000);


            pd.ChangeMessage("Step 5");
            pd.Increment(1);
            Thread.Sleep(1000);


            pd.ChangeMessage("Step 6");
            pd.Increment(1);
            Thread.Sleep(1000);


            pd.ChangeMessage("Step 7");
            pd.Increment(1);
            Thread.Sleep(1000);


            pd.ChangeMessage("Step 8");
            pd.Increment(1);
            Thread.Sleep(1000);


            pd.ChangeMessage("Step 9");
            pd.Increment(1);
            Thread.Sleep(1000);


            pd.ChangeMessage("Step 10");
            pd.Increment(1);
            Thread.Sleep(1000);

            pd.Close();


        }

The problem is that when it's being shown, if i click the button again, when the dialogbox is closed, it opens again. I mean it does not get clicked because it's Modal, but it just queues up and when the dialogbox is closed, it gets clicked.

How can I prevent this from happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长途伴 2024-10-23 23:57:05

也许将命令绑定到按钮并写入CanExecute 方法,如果命令已经在执行中,则阻止执行该命令。

Maybe bind a command to the button and write a CanExecute method which prevents the command from being executed if it is already in progress.

心奴独伤 2024-10-23 23:57:05

如果在前窗口打开时不打算使用后窗口,我会在加载的前窗口上设置一个事件以禁用后窗口,并在关闭时启用后窗口。

If the back window is not meant to be used while the front window is open, I would set an event on the frontwindow's loaded to disable the back window, and on close, enable the back window.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文