如何使图像可见和崩溃,当我点击网格(它是图像的父)

本文关键字:图像 网格 何使 崩溃 | 更新日期: 2023-09-27 18:11:58

我是windows phone开发新手。我有一个网格,其中包含一个图像。我需要使图像可见,当我点击网格(父图像)和崩溃,当我点击第二次。

我的设计如下

<Grid x:Name="gridThriller" HorizontalAlignment="Left" Height="172" Margin="33,85,0,0" VerticalAlignment="Top" Width="123"  Background="Transparent" Tap="selectThiller" >
            <Image Source="/Images/right.png" HorizontalAlignment="Right" VerticalAlignment="Top" Stretch="None"  />
        </Grid>
        <Grid x:Name="gridAction" HorizontalAlignment="Left" Height="172" Margin="186,85,0,0" VerticalAlignment="Top" Width="123"  Background="Transparent" Tap="selectThiller">
            <Image Source="/Images/right.png"  HorizontalAlignment="Right" VerticalAlignment="Top" Stretch="None"/>
        </Grid>
        <Grid x:Name="gridHorror" HorizontalAlignment="Left" Height="172" Margin="340,85,0,0" VerticalAlignment="Top" Width="123"  Background="Transparent" Tap="selectThiller">
            <Image Source="/Images/right.png"  HorizontalAlignment="Right" VerticalAlignment="Top" Stretch="None"/>
        </Grid>
        <Grid x:Name="gridSciFi" HorizontalAlignment="Left" Height="172" Margin="33,283,0,0" VerticalAlignment="Top" Width="123"  Background="Transparent" Tap="selectThiller">
            <Image Source="/Images/right.png"  HorizontalAlignment="Right" VerticalAlignment="Top" Stretch="None"/>
        </Grid>
        <Grid x:Name="gridRomantic" HorizontalAlignment="Left" Height="172" Margin="186,283,0,0" VerticalAlignment="Top" Width="123"  Background="Transparent" Tap="selectThiller">
            <Image Source="/Images/right.png" HorizontalAlignment="Right" VerticalAlignment="Top" Stretch="None"/>
        </Grid>
        <Grid x:Name="gridComedy" HorizontalAlignment="Left" Height="172" Margin="340,283,0,0" VerticalAlignment="Top" Width="123"  Background="Transparent" Tap="selectThiller">
            <Image Source="/Images/right.png"  HorizontalAlignment="Right" VerticalAlignment="Top" Stretch="None"/>
        </Grid>
        <Grid x:Name="gridDrama" HorizontalAlignment="Left" Height="172" Margin="33,481,0,0" VerticalAlignment="Top" Width="123"  Background="Transparent" Tap="selectThiller">
            <Image Source="/Images/right.png"  HorizontalAlignment="Right" VerticalAlignment="Top" Stretch="None"/>
        </Grid>
        <Grid x:Name="gridFantacy" HorizontalAlignment="Left" Height="172" Margin="186,481,0,0" VerticalAlignment="Top" Width="123"  Background="Transparent" Tap="selectThiller">
            <Image Source="/Images/right.png"  HorizontalAlignment="Right" VerticalAlignment="Top" Stretch="None"/>
        </Grid>
        <Grid x:Name="gridAnimated" HorizontalAlignment="Left" Height="172" Margin="340,481,0,0" VerticalAlignment="Top" Width="123"  Background="Transparent" Tap="selectThiller">
            <Image Source="/Images/right.png"   HorizontalAlignment="Right" VerticalAlignment="Top" Stretch="None"/>
        </Grid>

如何解决这个问题?

如何使图像可见和崩溃,当我点击网格(它是图像的父)

看起来所有的tap事件都指向同一个方法这是一个好的开始。如果你想继续沿着这条路走下去,那么我要做的就是:

在tap事件中

        foreach(Image item in (sender as Grid).Children)
        {
            if(item.Visibility == Visibility.Visible)
                item.Visibility = Visibility.Collapsed;
            else
                item.Visibility = Visibility.Visible;
        }

注#如果你在这些网格元素中放置了除图像以外的任何东西,将会爆炸。如果你需要添加其他东西,那么你需要在上面添加单独的逻辑来清除图像。