简体   繁体   English

Xamarin 将图像添加到 StackLayout OnChange

[英]Xamarin Add Image to StackLayout OnChange

I have a problem.我有个问题。 I have a TabbedPage, with page1 and page2.我有一个 TabbedPage,带有 page1 和 page2。 On page2 is a Grid with a few images.在 page2 上是一个带有一些图像的网格。 When I click on the Image, I add the image to a Collection, so I use a OnChange on page1.当我单击图像时,我将图像添加到集合中,因此我在第 1 页上使用了 OnChange。 On page1 I want to add the image that is new to the StackLayout on that page.在第 1 页上,我想将新图像添加到该页面上的 StackLayout。

Here is my code so far: This is the page2 code:到目前为止,这是我的代码: 这是 page2 代码:

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...但是当我单击图像时 GestureRecognizer 似乎不起作用...

What am I doing wrong?我究竟做错了什么?

I think you want to be notified in page1 about the addition of the new Image.我认为您希望在第 1 页中收到有关添加新图像的通知。

If this is the case you have to add an event handler to HomePage.SelectedImageCollection.CollectionChanged, so:如果是这种情况,您必须向 HomePage.SelectedImageCollection.CollectionChanged 添加一个事件处理程序,因此:

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];
           }
   ....
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM