【问题标题】:WPF forcing GUI update using DipatcherWPF 使用 Dispatcher 强制 UI 更新
【发布时间】:2010-09-24 09:11:04
【问题描述】:

我只需要显示一个自定义控件(带有旋转指针的时钟)并用它来替换鼠标光标,问题是如果我写:

Me.gridScreen.Visibility = Visibility.Visible
' some operations that takes about 1 second
Me.gridScreen.Visibility = Visibility.Hidden
(gridScreen is the grid that contains the user-control)

显然我什么也看不到,因为 UI 的更新发生在程序结束时。我已经尝试过 Me.UpdateLayout(),但它不起作用。

我尝试过以多种方式使用拆包器,但都没有用 :-(

这是我失败的尝试:

(uCurClock是用户控件,gridScreen是一个放置在窗口顶层的Grid,背景透明,包含用户控件)

Private Sub showClock()
    Dim thread = New System.Threading.Thread(AddressOf showClockIntermediate)
    thread.Start()
End Sub

Private Sub hideClock()
    Dim thread = New System.Threading.Thread(AddressOf hideClockIntermediate)
    thread.Start()
End Sub

Private Sub showClockIntermediate()
    Me.Dispatcher.BeginInvoke(DispatcherPriority.Normal, _
        New Action(AddressOf showClockFinale))
End Sub

Private Sub hideClockIntermediate()
    Me.Dispatcher.BeginInvoke(DispatcherPriority.Normal, _
        New Action(AddressOf hideClockFinale))
End Sub

Private Sub showClockFinale()
    Dim pt As Point = Mouse.GetPosition(Nothing)
    Me.uCurClock.Margin = New Thickness(pt.X - 9, pt.Y - 9, 0, 0)
    Me.gridScreen.Visibility = Visibility.Visible
    Me.Cursor = Cursors.None
    Me.UpdateLayout()
End Sub

Private Sub hideClockFinale()
    Me.gridScreen.Visibility = Visibility.Hidden
    Me.Cursor = Cursors.Arrow
    Me.UpdateLayout()
End Sub

Private Sub u_MouseMove(ByVal sender As System.Object, _
    ByVal e As MouseEventArgs) Handles gridScreen.MouseMove

    Dim pt As Point = e.GetPosition(Nothing)
    Me.uCurClock.Margin = New Thickness(pt.X - 9, pt.Y - 9, 0, 0)

    e.Handled = True
End Sub

Private Sub u_MouseEnter(ByVal sender As System.Object, _
    ByVal e As MouseEventArgs) Handles gridScreen.MouseEnter

    Me.uCurClock.Visibility = Visibility.Visible

    e.Handled = True
End Sub

Private Sub u_MouseLeave(ByVal sender As System.Object, _
    ByVal e As MouseEventArgs) Handles gridScreen.MouseLeave

    Me.uCurClock.Visibility = Visibility.Hidden

    e.Handled = True
End Sub

皮莱吉

【问题讨论】:

    标签: wpf user-interface dispatcher


    【解决方案1】:

    问题与调度程序上正在执行的消息的组合无关,而在于您正在调度程序上执行长时间运行的工作。您必须确保在后台线程上执行长时间运行/可能阻塞的操作。最简单的方法是使用BackgroundWorker 组件。

    请原谅 C#:

    var backgroundWorker = new BackgroundWorker();
    
    backgroundWorker.DoWork += delegate
    {
        // long running work goes here
    };
    backgroundWorker.RunWorkerCompleted += delegate
    {
        // change cursor back to normal here
    };
    
    // change cursor to busy here
    
    // kick off the background task
    backgroundWorker.RunWorkerAsync();
    

    【讨论】:

    • 谢谢,但我知道没有解决方案。事实上,在许多情况下,我有许多操作的程序,我从 UI 和其他人检索数据,我修改 UI 控件的集合源的数据。这些操作在后台线程中是被禁止的,分离起来太难了。我想我只会改变鼠标光标...
    • 非常感谢!我知道 BackgroundWorker,但我有一个问题,即在我的方法(已经存在)中分离长进程和 UI 上的操作太难了。目前唯一的方法是仅更改鼠标光标: Me.Cursor = New Cursor(".ani 文件的绝对路径")
    猜你喜欢
    • 1970-01-01
    • 2013-04-11
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 2014-11-23
    相关资源
    最近更新 更多