locked
c++, updating the flipview's itemssource

    Question

  • Hello,
    I have a flipview, and I am setting its itemssource to a Vector (let's call it m_items) that contains a few pages. I listen to the selectionChanged event to manage my pages layout (add pages near the selection and remove remanent pages). The problem is that the itemssource is in read-only and I need that when I make changes to m_items the flipview updates itself. I see that there is a lot of solutions with silverlight but I'm developping a metro app in c++/cx.

    Attempt 0:
    I first tried a workaround: in the selectionChanged handler, I set the itemssource back to nullptr and reassign it to the updated vector, and then I change flipview->selectedIndex so that the current page stays the same, but despite the selectionChanged events seem to be fired in the proper order, the result on the screen is that after ~1 second the item is reselected back to the first one of the vector. Any idea ?

    Attempt 1:
    In this attempt I try to get an updatable vector. It seems I need my collection to implement IBindableVector, so I created a bindable vector :

    ref class BindableVector : public IBindableVector
    {
    protected:
      std::vector<Object^> items;
      VectorView<Object^> ^roItems;
    
    public:
        BindableVector(){
          roItems = ref new VectorView<Object^>(items);
        }
        
        virtual IBindableIterator^ First(){
          throw ref new Platform::NotImplementedException;
          return nullptr;
        }
    
        virtual void Append(Object ^item){
          items.push_back(item);
        }
    
        virtual void Clear(){
          items.clear();
        }
    
        virtual Object^ GetAt(unsigned int index){
          return items.at(index);
        }
    
        virtual IBindableVectorView ^GetView(){
          return roItems;
        }
    
        virtual bool IndexOf(Object^ item, unsigned int *_index){
          if (!_index) _index = new unsigned int;
          *_index = std::find(items.begin(), items.end(), item) - items.begin();
          return *_index == items.size();
        }
    
        virtual void InsertAt(unsigned int index, Object^ item) {
          items.insert(items.begin() + index, item);
        }
    
        virtual void RemoveAt(unsigned int index){
          items.erase(items.begin() + index);
        }
    
        virtual void RemoveAtEnd(){
          items.erase(items.begin() + (items.size() - 1));
        }
    
        virtual void SetAt(unsigned int index, Object^ item){
          RemoveAt(index);
          InsertAt(index, item);
        }
    
        virtual property unsigned int Size{ 
           unsigned int get(){
             return items.size();
           }
        }
    };

    This time I do not reassign the itemssource.
    Result: it doesn't display the page that the selectionChanged event says, and it can crash.
    I try to set the flipview->selectedIndex after updating the bindableVector, or let it decide, but nothing gets better.
    It clearly seems that when I modify the bindableVector, the flipview is not updating to the ui.

    Any idea of what is causing the issue ?

    Wednesday, May 23, 2012 3:49 PM

All replies

  •  

    Hello,

    Would you please upload the whole project to skydriver, so that we can reproduce this issue on our side.

    skydriver

    It is not necessary that you send out the whole of your project. We just need a simplest sample to reproduce the problem. You can remove any confidential information or business details from it.

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us


    Thursday, May 24, 2012 2:57 AM
  • Jesse,

    I uploaded a simple sample project based on a simplification of the D2d Magazine App sample. The project is comparable to my attempt 1: just updating the collection and let it update the ui. The initial page is 0, when flipped to page 1, the page 2 is loaded and added to the collection. When flipped to the page 2, a black page is displayed, moving back of forth in the pages causes an exception to occur.

    Thank you for your help

    Jo

    Thursday, May 24, 2012 9:43 AM
  • Also, concerning the attempt 0, is it safe to run the code below Inside a SelectionChanged event handler ?

    Friday, May 25, 2012 2:11 PM
  • It seems that modifying the ItemsSource during the selectionChanged event is unsafe. But it seems safe to modify it during the SizeChanged event. I found the workaround of keeping a fixed-size ItemsSource list with all the pages, and (un)loading the content when (un)needed. When a rotation occurs, the size of the list has to be changed, and I change it in place (during the SizeChanged event). That gets round of my problem but doesn't explain what is the procedure to change the ItemsSource during a selectionChanged.
    Wednesday, May 30, 2012 8:57 AM