Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
wpf在过程中强制更新UI窗口_Wpf_Multithreading_User Interface_Refresh - Fatal编程技术网

wpf在过程中强制更新UI窗口

wpf在过程中强制更新UI窗口,wpf,multithreading,user-interface,refresh,Wpf,Multithreading,User Interface,Refresh,如果我使用file.cur或.ani替换鼠标光标,我只需要显示一个自定义控件(指针旋转的时钟),并用它替换鼠标光标 Me.CUrsor=新光标(“.ani文件的绝对路径”) 没有问题:我可以在一个过程中更改光标:但是动画的质量非常差,而且,出于其他原因,我更喜欢使用我的小用户控件。问题是如果我写: Me.gridScreen.Visibility=可见性.Visibility “有些操作需要大约1秒的时间 Me.gridScreen.Visibility=Visibility.Hidden (g

如果我使用file.cur或.ani替换鼠标光标,我只需要显示一个自定义控件(指针旋转的时钟),并用它替换鼠标光标 Me.CUrsor=新光标(“.ani文件的绝对路径”) 没有问题:我可以在一个过程中更改光标:但是动画的质量非常差,而且,出于其他原因,我更喜欢使用我的小用户控件。问题是如果我写:

Me.gridScreen.Visibility=可见性.Visibility

“有些操作需要大约1秒的时间

Me.gridScreen.Visibility=Visibility.Hidden

(gridScreen是包含用户控件的网格)

显然,我什么也看不到,因为UI的更新发生在过程的末尾。我已尝试使用我的.UpdateLayout(),但它不起作用

我曾尝试以多种方式使用dispacker,但没有一种有效:-(

这是我失败的尝试:

(uCurClock是usercontrol,gridScreen位于窗口顶层的网格,背景为trasparent,包含usercontrol)


PIleggi

虽然以下代码可以满足您的要求,但我怀疑它实际上对您没有帮助,因为您提到了动画。您将需要使用多个线程。不过,为了说明原因,这里有一些东西可以回答您提出的问题:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click

    uc1.Visibility = Visibility.Visible
    Cursor = Cursors.Wait

    ' Push visibility changes now.
    ' (Sort of like DoEvents - and a horrible idea for exactly the same
    ' reasons that DoEvents was a total train wreck. Never actually do
    ' this - use a background thread instead.)
    Dim df As New DispatcherFrame(True)
    Dispatcher.BeginInvoke(Sub() df.Continue = False, DispatcherPriority.ContextIdle)
    Dispatcher.PushFrame(df)

    Thread.Sleep(1000)

    ClearValue(CursorProperty)
    uc1.Visibility = Visibility.Hidden

End Sub
假设页面上有一些名为uc1的usercontrol,这将迫使它在缓慢的过程运行时可见

但不会运行任何动画。问题是,如果你在UI线程上执行一些缓慢的操作,UI线程将无法执行任何其他操作-它无法运行动画,也无法响应用户输入。基本上,UI已冻结。此处显示的代码使用户控件可见的唯一原因是它基本上说“立即执行任何未完成的UI线程工作”,其副作用是处理对Visible属性的更改

但是动画也发生在UI线程上

如果您想正确地执行此操作,您需要在后台线程上执行此操作,可以使用BackgroundWorker,也可以编写自己的线程代码。

参考

好的ole
DoEvents
用于WPF

Public Sub DoEvents()
    Dim frame As New DispatcherFrame()
    Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, New DispatcherOperationCallback(AddressOf ExitFrame), frame)
    Dispatcher.PushFrame(frame)
End Sub

Public Function ExitFrame(ByVal f As Object) As Object
    CType(f, DispatcherFrame).Continue = False
    Return Nothing
End Function

格里菲茨:谢谢!我用了.ani光标,但你的回答很有趣!
Public Sub DoEvents()
    Dim frame As New DispatcherFrame()
    Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, New DispatcherOperationCallback(AddressOf ExitFrame), frame)
    Dispatcher.PushFrame(frame)
End Sub

Public Function ExitFrame(ByVal f As Object) As Object
    CType(f, DispatcherFrame).Continue = False
    Return Nothing
End Function