WPF - 防止用户操作在应用程序繁忙时排队 [英] WPF - prevent user actions queuing-up while application is busy

查看:19
本文介绍了WPF - 防止用户操作在应用程序繁忙时排队的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当应用程序繁忙(意味着 UI 线程被阻塞做某事)时,您建议采用什么方法来忽略用户请求(如鼠标点击).对此的具体示例可能是网格控件的排序.如果我们说排序很慢,那么在操作运行时,我想忽略额外的用户点击,这些点击堆积并多次执行操作.

What approach would you suggest for ignoring user requests (like mouse clicks) while application is busy (meaning that UI thread is blocked doing something). Concrete example for this might be sorting of the grid control. If we say that sorting is slow, then while the operation is running I would like to ignore additional nurveous user clicks which pile up and execute the operation many times.

除了求助于禁用控件或使长时间运行的操作异步,然后手动为用户请求实现生产者-消费者模式(基本上在操作时忽略任何请求)之外,在 WPF 中是否有一种奇特"的方式/技巧?正在运行)?

Is there a "fancy" way / trick of doing this in WPF, besides resorting to disabling controls, or making the long running operation async, and then implementing producer-consumer pattern manually for user request (basically ignoring any requests while operation is running)?

推荐答案

一种方法是挂钩 Dispatcher OperationStarted 和 OperationCompleted 事件.这些事件的处理程序将设置一个绑定到窗口的 IsHitTestVisible 属性的布尔属性.这样,当 Dispatcher 正在处理时,它将阻止用户与 UI 交互.

One way to do this is to hook the Dispatcher OperationStarted and OperationCompleted events. The handlers to these events will set a boolean property that is bound to the IsHitTestVisible property of your window. This way when the Dispatcher is processing it will prevent the user interacting with the UI.

一个例子如下:

视图模型:

    private bool _appIdle = true;

    private void Hooks_OperationStarted(object sender, DispatcherHookEventArgs e)
    {
        ApplicationIdle = false;
    }

    private void Hooks_OperationCompleted(object sender, DispatcherHookEventArgs e)
    {
        ApplicationIdle = true;
    }

    public bool ApplicationIdle
    {
        get { return _appIdle; }
        set
        {
            _appIdle = value;
            RaisePropertyChanged("ApplicationIdle");
        }
    }

    public MainWindowViewModel()
    {
        Application.Current.Dispatcher.Hooks.OperationStarted += Hooks_OperationStarted;
        Application.Current.Dispatcher.Hooks.OperationCompleted += Hooks_OperationCompleted;
    }

主窗口 xaml:

IsHitTestVisible="{Binding ApplicationIdle}"

这篇关于WPF - 防止用户操作在应用程序繁忙时排队的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆