C# 给 grid childs 添加点击事件

C# add a click event to grid childs

如何向我在代码隐藏中创建的子对象添加点击事件?

String[] imageNames = System.IO.Directory.GetFiles("Assets/Images/Gallery");

foreach (String image in imageNames)
{
    Uri source = new Uri("ms-appx:///" + image);

    ImageSource imgSource = new BitmapImage(source);

    Image myImage = new Image();
    myImage.Source = imgSource;

    gallery.Children.Add(myImage);    
}

以上代码从图库文件夹中获取所有图像并将它们添加到 variablesizedwrapgrid。我想在其中的每一个上添加一个点击事件(基本上允许用户将它们保存到他们正在使用的设备上)

谢谢

1)

myImage.PointerReleased += Img_PointerReleased;

private void Img_PointerReleased(object sender, PointerRoutedEventArgs e)
{
    // do your job
}

2) 或者编写继承自 Button 的 UserControl 包装图像,如果您不想使用默认按钮动画,则添加适当的 VisualStates - 这样您就可以使用 Clicked 事件