locked
FlipView with ItemsSource and dynamic items RRS feed

  • Question

  • I'm using the Grid App sample (XAML / Visual Basic, included with Visual Studio) and want to make it dynamic

    As you can see, the "content" is all the same (is a var). I can change this easily but I don't want to read all the data at once. I want to read every content when loading it on the ItemDetailPage, I mean, when you scroll the FlipView to see next item

    I see there is a SelectionChanged event, and I can get the SelectedItem, but now I have two problems:

    1- The first time the event fires, it comes blank, no SelectedItem

    2- When SelectedItem, then now I need to read the values, I mean (in this case) the UniqueId

    Any help?

    The code:

    Dim fv As FlipView = sender
    Dim item = fv.ItemContainerGenerator.ContainerFromItem(fv.SelectedItem)

    Wednesday, July 18, 2012 7:24 PM

Answers

  • Ok, my main question was to get the UniqueId, now I got it, and in fact, your url helps me a lot

    My code to retrieve the UniqueId

    Private Async Function flipView_SelectionChanged_1(sender As Object, e As SelectionChangedEventArgs) As Task
            Dim fv As FlipView = sender
    
            If fv.SelectedItem Is Nothing Then Exit Function
    
            Dim fvi As FlipViewItem = fv.ItemContainerGenerator.ContainerFromItem(fv.SelectedItem)
    
            If fvi Is Nothing Then
                System.Diagnostics.Debug.WriteLine("Error 1")
    
                Await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
                                    Sub()
                                        SecondAttempt(fv)
                                    End Sub)
            Else
                System.Diagnostics.Debug.WriteLine("Ok 1")
    
                Dim item As Data.SampleDataItem = fvi.Content
    
                ' item.UniqueId
            End If
    End Function
    
    Private Sub SecondAttempt(ByVal fv As FlipView)
            Dim fvi As FlipViewItem = fv.ItemContainerGenerator.ContainerFromItem(fv.SelectedItem)
    
            If fvi Is Nothing Then
                System.Diagnostics.Debug.WriteLine("Error 2")
            Else
                System.Diagnostics.Debug.WriteLine("Ok 2")
    
                Dim item As Data.SampleDataItem = fvi.Content
    
                ' item.UniqueId
            End If
    End Sub

    This code is useful just when you change the actual item, I mean, when you click on the scrolls

    If you want to retrieve the UniqueId when click on an item from outside ItemDetailPage, then use the LoadState event and set a flag so the SelectionChange event does nothing (because will throw error)

    • Marked as answer by SuNcO Thursday, July 19, 2012 6:16 PM
    Thursday, July 19, 2012 6:05 PM

All replies

  • I already saw that post, but didn't solve my question

    That's a common problem with Microsoft samples, all the stuff is preloaded in vars and sometimes is a little complicated change to dinamyc, at least certain parts of the info

    Wednesday, July 18, 2012 10:32 PM
  • Ok, my main question was to get the UniqueId, now I got it, and in fact, your url helps me a lot

    My code to retrieve the UniqueId

    Private Async Function flipView_SelectionChanged_1(sender As Object, e As SelectionChangedEventArgs) As Task
            Dim fv As FlipView = sender
    
            If fv.SelectedItem Is Nothing Then Exit Function
    
            Dim fvi As FlipViewItem = fv.ItemContainerGenerator.ContainerFromItem(fv.SelectedItem)
    
            If fvi Is Nothing Then
                System.Diagnostics.Debug.WriteLine("Error 1")
    
                Await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
                                    Sub()
                                        SecondAttempt(fv)
                                    End Sub)
            Else
                System.Diagnostics.Debug.WriteLine("Ok 1")
    
                Dim item As Data.SampleDataItem = fvi.Content
    
                ' item.UniqueId
            End If
    End Function
    
    Private Sub SecondAttempt(ByVal fv As FlipView)
            Dim fvi As FlipViewItem = fv.ItemContainerGenerator.ContainerFromItem(fv.SelectedItem)
    
            If fvi Is Nothing Then
                System.Diagnostics.Debug.WriteLine("Error 2")
            Else
                System.Diagnostics.Debug.WriteLine("Ok 2")
    
                Dim item As Data.SampleDataItem = fvi.Content
    
                ' item.UniqueId
            End If
    End Sub

    This code is useful just when you change the actual item, I mean, when you click on the scrolls

    If you want to retrieve the UniqueId when click on an item from outside ItemDetailPage, then use the LoadState event and set a flag so the SelectionChange event does nothing (because will throw error)

    • Marked as answer by SuNcO Thursday, July 19, 2012 6:16 PM
    Thursday, July 19, 2012 6:05 PM