2010-09-24 72 views
0

我只需要显示自定义控制(带有旋转指针的时钟)GUI更新并以此取代鼠标光标,问题是,如果我写:WPF强制使用Dipatcher

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(),但它不起作用。

我已经tryed使用在许多方式dispacker但没有,工程:-(

这就是我丢失的尝试:

(uCurClock是用户控件,gridScreen放置在顶层网格在窗口中,用trasparent背景中,包含该用户控件)

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 

PIleggi

回答

1

的问题不是用MES的组合物做圣人在调度员上执行,这就是你在调度员上执行长时间运行的工作。您必须确保长时间运行/可能的阻塞操作在后台线程上执行。最简单的方法是使用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(); 
+0

谢谢你,但我知道有没有解决方案。事实上,在很多情况下,我有程序与梅尼操作,我从UI中检索数据,其他人在修改UI控件的集合源数据。这些操作在后台线程中被禁止,并且很难做到分离。我想我只会改变鼠标光标... – lamarmora 2010-09-24 10:04:34

+0

非常感谢!我知道BackgroundWorker,但我有这样的问题:在我的方法中(已经存在)在进程和UI之间的操作之间进行分离太困难了。目前唯一的方法是只更改鼠标光标:Me.Cursor =新光标(“.ani文件的绝对路径”) – lamarmora 2010-09-24 15:40:43