为ListBoxItems应用Foreground仅适用于列表框中的Task.Delay [英] Applying Foreground for ListBoxItems only works with Task.Delay in a listbox
问题描述
我正在尝试有选择地将绿色前景应用于列表框中的项目。我使用以下代码来做到这一点:
I am trying to selectively apply a green foreground to items in a listbox. I use the following code to do just that:
Public Async Sub MarkFromCollection()
if lbxLocMins.Items.Count> 0然后'检查列表框是否有项目
Dim tmpList As New List(Of Integer)'创建临时列表以保存 数据以检查
tmpList = Await(DataService.CollGetIdMinforL5IDAsync(thisLocality.LocL5ID))'从与MS SQL Server通信的WCF服务获取数据
如果tmpList.Count> 0然后'检查列表是否包含项目
Await Task.Delay(50)'避免应用程序崩溃??????? !!!! !!!
Dim lbi作为ListBoxItem'创建lbi
For Each b As brkMineral在lbxLocMins.Items'iterate
if tmpList.Contains(b.IdMin)然后'检查集合中的列表是否匹配 列表框中的项目为
lbi =新的ListBoxItem'实施lbi
; lbi = CType(lbxLocMins.ContainerFromItem(b),ListBoxItem)'设置lbi
if Not lbi Is Nothing Then'检查是否lbi什么都没有?
lbi.Foreground = greenBrush'if if:set foreground to green
  ; 结束如果
结束如果是
Next
结束如果
结束如果
End Sub
Public Async Sub MarkFromCollection()
If lbxLocMins.Items.Count > 0 Then 'Check if the Listbox has items
Dim tmpList As New List(Of Integer) 'Create temp list to hold data to check against
tmpList = Await (DataService.CollGetIdMinforL5IDAsync(thisLocality.LocL5ID)) 'Get Data from a WCF-Service that talks to a MS SQL Server
If tmpList.Count > 0 Then 'Check if the list contains items
Await Task.Delay(50) 'avoid App crash???????!!!!!!!
Dim lbi As ListBoxItem 'Create lbi
For Each b As brkMineral In lbxLocMins.Items 'iterate
If tmpList.Contains(b.IdMin) Then 'Check if there is a match between the list from the collection and the items in the listbox
lbi = New ListBoxItem 'instatiate the lbi
lbi = CType(lbxLocMins.ContainerFromItem(b), ListBoxItem) 'set the lbi
If Not lbi Is Nothing Then 'check if lbi is nothing
lbi.Foreground = greenBrush 'if not: set foreground to green
End If
End If
Next
End If
End If
End Sub
只有包含Task.Delay(50)时代码才能正常工作。如果我对这一行发表评论,该应用程序会在5次中崩溃4次。
The code works as expected only if I have the Task.Delay(50) included. If I comment this line out the app crashes 4 times out of 5.
有人可以帮我理解发生了什么吗?
Can anybody help me understand what is going on?
推荐答案
你得到了什么异常,我猜你得到的是一个没有运行的ui-thread错误?
What exception do you get, I'm guessing you're getting a not-running-on-ui-thread error?
这篇关于为ListBoxItems应用Foreground仅适用于列表框中的Task.Delay的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!