WP8.1动态创建的图像丢失

时间:2015-01-28 16:07:29

标签: vb.net windows-phone-8 .net-4.5 dispatcher

我正在使用Hub控件开发Windows Phone 8.1应用程序,以动态显示类别(如HubSections)和图像(包含在每个HubSection中的StackPanel中)。

该过程分两个阶段进行。第一部分从API下载XML并解析它以将Categories及其Images存储到Dictionary中。然后代码循环遍历类别列表并调用Dispatcher将HubSections添加到Hub,并以占位符图像开头。所有这些都在后台任务中运行。

Private Async Sub GetXML()
    For Each Category As String In MessageMgr.CategoryList.Keys

            Await Hub.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
                                                   Sub()
                                                       AddHubSection(Category, MessageMgr.CategoryList(Category), Hub)
                                                       RaiseEvent HubSectionAdded(Category, MessageMgr, Hub)

                                                   End Sub)
    Next
End Sub

Private Sub AddHubSection(Category As String, MessageList As List(Of MessageManager.Message), Hub As Windows.UI.Xaml.Controls.Hub)
    Dim HubSection As New Windows.UI.Xaml.Controls.HubSection

    Dim TemplateString As String = "<DataTemplate>"...."</DataTemplate>"

    HubSection.Header = Category
    HubSection.ContentTemplate = XamlReader.Load(TemplateString)
    Hub.Sections.Add(HubSection)
End Sub

由Dispatcher执行的Sub比引发一个事件,以便我知道代码已经完成。然后,此事件将触发另一个后台任务以下载所有图像并更新图像占位符。我使用递归循环函数(FindChildControl)来搜索Hub中的图像控件,然后更新它的源。

Private Sub OnHubSectionAdded(ByVal Category As String, ByVal MessageMgr As MessageManager, ByVal Hub As Hub)
    For Each Banner In MessageMgr.CategoryList(Category)
        Task.Run(Sub() GetImage(Banner, Hub))

    Next
End Sub

Private Async Sub GetImage(Banner As MessageManager.Message, Hub As Windows.UI.Xaml.Controls.Hub)
    Dim Declarations.....

    ''' Download image code goes here '''

            Await Hub.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
                                          Sub()
                                              imgBanner = FindChildControl(Of Windows.UI.Xaml.Controls.Image)(Hub, "img" & Banner.BannerID)

                                              If imgBanner IsNot Nothing Then
                                                  myBitmapImage = New BitmapImage
                                                  myBitmapImage.SetSource(RAS)
                                                  imgBanner.Source = myBitmapImage
                                              End If
                                          End Sub)

End Sub

我遇到的问题是FindChildControl函数并不总是立即返回Image控件。我现在所做的就是用一个简短的Task.Delay包围一段时间,并在10秒后退出。控制始终在1秒内可用。

我的问题是,当我立即通过时,为什么在Hub中找不到图像?我并不特别想在代码中求助于等待它变得可用。

0 个答案:

没有答案