0
votes

I have a problem. I have a TabbedPage, with page1 and page2. On page2 is a Grid with a few images. When I click on the Image, I add the image to a Collection, so I use a OnChange on page1. On page1 I want to add the image that is new to the StackLayout on that page.

Here is my code so far: This is the page2 code:

private void LoadTemplateList()
{
    string[] imageArray = { "Good_Question.png", "Excuse_Me.png"};
    int imageIndex = 0;
    for (var i = 0; i <= TemplateGrid.RowDefinitions.Count; i++)
    {
        if (i == 2)
        {
            for (var j = 0; j < TemplateGrid.ColumnDefinitions.Count; j++)
            {
                if (j == 1 || j == 3)
                {
                    Image image = new Image { Source = imageArray[imageIndex], VerticalOptions = LayoutOptions.StartAndExpand };

                    var SelectImage = new TapGestureRecognizer();
                    SelectImage.Tapped += AddImageToCollection(image);
                    image.GestureRecognizers.Add(SelectImage);

                    TemplateGrid.Children.Add(image,j,i);
                    imageIndex++;
                }
            }
        }
    }
}

private EventHandler AddImageToCollection(Image image)
{
    HomePage.SelectedImageCollection.Add(image);
    return null;
}

But the GestureRecognizer doesn't seem to work when I click the image...

What am I doing wrong?

1
ObservableCollection has an Add method, did you try it? It also has a constructor that allows initialize its contents with the contents of another ObservableCollectionTony
Yeah, I maybe have an idea how I can make what I want, but what about the errorA. Vreeswijk
Change the signature to: private void AddImageToCollection(Image image)Tony

1 Answers

1
votes

I think you want to be notified in page1 about the addition of the new Image.

If this is the case you have to add an event handler to HomePage.SelectedImageCollection.CollectionChanged, so:

public MemeBuilder()
{
   InitializeComponent();

   SizeChanged += (s, a) =>
   {
       myLayout.HeightRequest = this.Width;
       myLayout.WidthRequest = this.Width;
   };

   HomePage.SelectedImageCollection.CollectionChanged += AddImageToPreview;
}

private void AddImageToPreview(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
           if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
           {
               var image = (Image)e.NewItems[e.NewItems.Count - 1];
           }
   ....
}