Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.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 隐藏图像_Wpf_Vb.net_Image_Wpf Controls - Fatal编程技术网

Wpf 隐藏图像

Wpf 隐藏图像,wpf,vb.net,image,wpf-controls,Wpf,Vb.net,Image,Wpf Controls,我正在尝试使用以下代码隐藏WPF上当前的所有图像: Dim theImgs() As Controls.Image = {picNextTopic1, picNextTopic2, picNextTopic3, picNextTopic4, picNextTopic5, picNextTopic6, picNextTopic7, picNextTopic8, picNextTopic9, picNextTopic10, picNextTopic11, picNextTopic12, picNex

我正在尝试使用以下代码隐藏WPF上当前的所有图像:

Dim theImgs() As Controls.Image = {picNextTopic1, picNextTopic2, picNextTopic3, picNextTopic4, picNextTopic5, picNextTopic6, picNextTopic7, picNextTopic8, picNextTopic9, picNextTopic10, picNextTopic11, picNextTopic12, picNextTopic13, picNextTopic14, picNextTopic15, picNextTopic16}

Dim intX As Integer = 0

Do Until intX = theImgs.Length
   Try
      theImgs(intX).Visibility = Visibility.Hidden
      intX += 1
   Catch ex As Exception
      MsgBox(ex.Message)
   End Try
Loop
但是,当运行上面的代码时,我会出现以下错误:

调用线程无法访问此对象,因为另一个线程拥有它

如何修复此错误?

更改:

theImgs(intX).Visibility = Visibility.Hidden;
致:

C#

VB

试试这个链接

下面的代码应该可以正常工作:

Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, _
New Action(Function() theImgs(intX).Visibility = Visibility.Hidden))

最有可能的情况是,该图像是在背景线程上创建的,或者您正试图从背景线程对其进行修改。作为一种解决方案,使用
调度程序
而不是后台线程在应用程序的主UI线程上运行代码。@Rachel介意演示一个示例吗?当然,请签出。它用于更新非UI对象,但问题和解决方案仍然相同。@Rachel我是WPF新手,因此该页面上有很多内容,我不确定放在哪里以及所有内容。这些图像已经在WPF表单上了,所以我不知道为什么它说它在不同的线程中…这段代码是从新线程还是BackgroundWorker运行的?如果是这样,您需要使用
调度程序
将更新图像的代码发送到UI线程,因为后台线程无法更新它没有创建的对象的属性。如果没有,则在单独的线程上创建了
theImgs
集合,需要更改代码逻辑以从主UI线程创建它们。似乎无法将其转换为VB.net?我尝试了Application.Current.Dispatcher.Invoke(Function(){theImgs(intX.Visibility=Visibility.Hidden})但这似乎不起作用?我还尝试了Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,New Action(Function(){theImgs(intX.Visibility=Visibility.Hidden}),但这也不起作用。听起来Dispatcher已经对您的第一个异常进行了排序,但肯定还有另一个问题。可以发布xaml吗?我发现错误对象引用未设置为对象的实例。请尝试以下代码:Dispatcher di=Dispatcher.CurrentDispatcher;//在initializeComponent()旁边;将Di变量存储在类级别,现在尝试使用Di.Invoke(DispatcherPriority.Background,uu新操作(Function()theImgs(intX.Visibility=Visibility.Hidden))
Application.Current.Dispatcher.Invoke(
    Function(){ 
        theImgs(intX).Visibility = Visibility.Hidden 
    }
)
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, _
New Action(Function() theImgs(intX).Visibility = Visibility.Hidden))