android 我的应用程序中有两个图像按钮,但我无法单击第二个(xaml C#)

2vuwiymt  于 2022-11-03  发布在  Android
关注(0)|答案(1)|浏览(125)

我正在为一个拼贴项目制作一个应用程序,它是wack a mole,我必须有两个网格,我把它们都设置好了,沿着在两个网格中都有一个图像按钮。在第一个网格中,图像按钮工作,当用户点击mole时,他们得到10分,但在第二个网格中,5x5的ikgbutton由于某种原因不会点击。
我已经尝试和搜索的人有类似的问题,但我似乎不能找到,我已经尝试了很多次删除和重新编码,看看我是否可以让它工作,也试图通过它一步一步的工作,但我所做的似乎没有工作.
XAML代码

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="WackAFlower.MainPage"
             Title="Wack A Flower"
             BackgroundImageSource="garden_field.png">

    <!--Main page-->
    <StackLayout>

            <!--Titile-->
        <StackLayout>
            <Label FontSize="Title"
                   BackgroundColor="Black"
                   TextColor="#ffeb3b"
                   HorizontalTextAlignment="Center"
                   Margin="0,25,0,0"
                   FontFamily="times"
                   >
                Wack A Flower        
            </Label>
        </StackLayout>

            <!--End game button-->
        <StackLayout>
            <Button Text="Start Game"
                    Margin="50,10,50,0"
                    TextColor="#ffeb3b"
                    BackgroundColor="Black"
                    FontFamily="times"
                    FontSize="Large"
                    Padding="20"
                    >
            </Button>
        </StackLayout>

            <!--Change Grid, Restart and socre-->
        <StackLayout Orientation="Horizontal">
            <Button Text="Change Grid"
                    TextColor="#ffeb3b"
                    BackgroundColor="Black"
                    FontSize="Small"
                    FontFamily="times"
                    Margin="50,25,15,0"
                    ></Button>

            <Button x:Name="BtnSwitch"
                    Clicked="BtnSwitch_Clicked"
                    Text="5x5"
                    TextColor="#ffeb3b"
                    BackgroundColor="Black"
                    FontSize="Small"
                    FontFamily="times"
                    Margin="5,25,15,0"
                ></Button>
            <Label x:Name="LblPoints"
                TextColor="#ffeb3b"
                VerticalTextAlignment="Center"
                Margin="10,25,15,0"
                Padding="15"
                BackgroundColor="Black"
                Text="00"
                ></Label>

        </StackLayout>

            <!--intro to the game-->
        <StackLayout>
            <Label Text="Welcome to Wack a Flower, You are a bee on the hunt for flowers, you will have to get a score of 100 to win, good luck have fun"
                   TextColor="#ffeb3b"
                   BackgroundColor="Black"
                   HorizontalTextAlignment="Center"
                   FontFamily="times"
                   FontSize="Medium"
                   FontAttributes="Bold"
                   Margin="25"

                />
        </StackLayout>

            <!--Grid for game-->
        <StackLayout>

            <!--4x4 grid-->
            <Grid x:Name="GridMode4" IsVisible="true" IsEnabled="True"  
                HeightRequest="300" WidthRequest="300" HorizontalOptions="Center" VerticalOptions="Center" Padding="10">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                    <!--Mole Holes-->
                <!--first row-->
                <!--<Button BorderWidth="5" BorderColor="Black" HeightRequest="50" WidthRequest="5" Grid.Row="0" Grid.Column="0" BackgroundColor="red" Text="0,0"/> this is how i was making the grid before-->
                <BoxView Grid.Row="0" Grid.Column="0"  WidthRequest="60" HeightRequest="60"  Margin="10" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="0" Grid.Column="1"  WidthRequest="60" HeightRequest="60"  Margin="10" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="0" Grid.Column="2"  WidthRequest="60" HeightRequest="60"  Margin="10" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="0" Grid.Column="3"  WidthRequest="60" HeightRequest="60"  Margin="10" CornerRadius="30" Color="Black" Opacity="0.8"/>                
                <!--second row-->
                <BoxView Grid.Row="1" Grid.Column="0"  WidthRequest="60" HeightRequest="60" Margin="10" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="1" Grid.Column="1"  WidthRequest="60" HeightRequest="60" Margin="10" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="1" Grid.Column="2"  WidthRequest="60" HeightRequest="60" Margin="10" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="1" Grid.Column="3"  WidthRequest="60" HeightRequest="60" Margin="10" CornerRadius="30" Color="Black" Opacity="0.8"/>                
                <!--third row-->
                <BoxView Grid.Row="2" Grid.Column="0" WidthRequest="60" HeightRequest="60" Margin="10" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="2" Grid.Column="1" WidthRequest="60" HeightRequest="60" Margin="10" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="2" Grid.Column="2" WidthRequest="60" HeightRequest="60" Margin="10" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="2" Grid.Column="3" WidthRequest="60" HeightRequest="60" Margin="10" CornerRadius="30" Color="Black" Opacity="0.8"/>                
                <!--fourth row-->
                <BoxView Grid.Row="3" Grid.Column="0" WidthRequest="60" HeightRequest="60" Margin="10" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="3" Grid.Column="1" WidthRequest="60" HeightRequest="60" Margin="10" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="3" Grid.Column="2" WidthRequest="60" HeightRequest="60" Margin="10" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="3" Grid.Column="3" WidthRequest="60" HeightRequest="60" Margin="10" CornerRadius="30" Color="Black" Opacity="0.8"/>

                <!--Imagbutton for grid-->
            <ImageButton x:Name="ImgBtnFlower4x4" Source="mole.png" Grid.Row="1" Grid.Column="1" HeightRequest="60" WidthRequest="60" HorizontalOptions="Center"
                         VerticalOptions="Center" Clicked="ImgBtnFlower_Clicked"></ImageButton>

            </Grid><!--end of grid 4x4-->

            <!--5x5 grid-->
             <Grid x:Name="GridMode5" IsVisible="False" IsEnabled="False"
                HeightRequest="300" WidthRequest="300" HorizontalOptions="Center" VerticalOptions="Center"  Padding="10"> 
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                    <!--Mole Holes-->
                <!--first row-->
                <!--<Button BorderWidth="5" BorderColor="Black" HeightRequest="50" WidthRequest="5" Grid.Row="0" Grid.Column="0" BackgroundColor="red" Text="0,0"/> this is how i was making the grid before-->
                <BoxView Grid.Row="0" Grid.Column="0"  WidthRequest="60" HeightRequest="60" Margin="5" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="0" Grid.Column="1"  WidthRequest="60" HeightRequest="60" Margin="5" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="0" Grid.Column="2"  WidthRequest="60" HeightRequest="60" Margin="5" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="0" Grid.Column="3"  WidthRequest="60" HeightRequest="60" Margin="5" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="0" Grid.Column="4"  WidthRequest="60" HeightRequest="60" Margin="5" CornerRadius="30" Color="Black" Opacity="0.8"/>                

                <!--second row-->
                <BoxView Grid.Row="1" Grid.Column="0"  WidthRequest="60" HeightRequest="60" Margin="5" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="1" Grid.Column="1"  WidthRequest="60" HeightRequest="60" Margin="5" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="1" Grid.Column="2"  WidthRequest="60" HeightRequest="60" Margin="5" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="1" Grid.Column="3"  WidthRequest="60" HeightRequest="60" Margin="5" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="1" Grid.Column="4"  WidthRequest="60" HeightRequest="60" Margin="5" CornerRadius="30" Color="Black" Opacity="0.8"/>                

                <!--third row-->
                <BoxView Grid.Row="2" Grid.Column="0" WidthRequest="60" HeightRequest="60" Margin="5" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="2" Grid.Column="1" WidthRequest="60" HeightRequest="60" Margin="5" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="2" Grid.Column="2" WidthRequest="60" HeightRequest="60" Margin="5" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="2" Grid.Column="3" WidthRequest="60" HeightRequest="60" Margin="5" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="2" Grid.Column="4" WidthRequest="60" HeightRequest="60" Margin="5" CornerRadius="30" Color="Black" Opacity="0.8"/>                

                <!--fourth row-->
                <BoxView Grid.Row="3" Grid.Column="0" WidthRequest="60" HeightRequest="60" Margin="5" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="3" Grid.Column="1" WidthRequest="60" HeightRequest="60" Margin="5" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="3" Grid.Column="2" WidthRequest="60" HeightRequest="60" Margin="5" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="3" Grid.Column="3" WidthRequest="60" HeightRequest="60" Margin="5" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="3" Grid.Column="4" WidthRequest="60" HeightRequest="60" Margin="5" CornerRadius="30" Color="Black" Opacity="0.8"/>

                <!--fith row-->
                <BoxView Grid.Row="4" Grid.Column="0" WidthRequest="60" HeightRequest="60" Margin="5" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="4" Grid.Column="1" WidthRequest="60" HeightRequest="60" Margin="5" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="4" Grid.Column="2" WidthRequest="60" HeightRequest="60" Margin="5" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="4" Grid.Column="3" WidthRequest="60" HeightRequest="60" Margin="5" CornerRadius="30" Color="Black" Opacity="0.8"/>
                <BoxView Grid.Row="4" Grid.Column="4" WidthRequest="60" HeightRequest="60" Margin="5" CornerRadius="30" Color="Black" Opacity="0.8"/>  

                <!--Imgbutton for grid-->
                <ImageButton x:Name="ImgBtnFlower5x5" Source="mole_two.png" Grid.Row="1" Grid.Column="1" HeightRequest="60" WidthRequest="60" HorizontalOptions="Center"
                         VerticalOptions="Center" Clicked="ImgBtnFlower_Clicked"></ImageButton>

            </Grid><!--end of grid 5x5-->
        </StackLayout>

            <!--how to start game-->
        <StackLayout>
            <Label TextColor="#ffeb3b" FontFamily="times" FontSize="Medium" HorizontalTextAlignment="Center" Margin="25" Background="black">
                Once you press start game the flowers will beging to apear so be ready
            </Label>
        </StackLayout>

    </StackLayout> <!--End of main page-->

</ContentPage>

C#程式码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace WackAFlower
{
    public partial class MainPage : ContentPage
    {

        //gobal variables
        Random _random;//random for use in the movement of the mole

        public MainPage()
        {
            InitializeComponent();
            _random = new Random();

        }//end of main page

        //rest set method
        private void gridrest()
        {
            LblPoints.Text = "0";
        }//end of gridrest

        //4x4 gird
        private void MoleMovement4x4()
        {
            //mole we get giving random rows and cols to move to
            //when the player clicks the mole it will move to a new random postions
            int r = 0, c = 0;
            r = _random.Next(0, 3);
            c = _random.Next(0, 3);

            //move mole to new loc
            ImgBtnFlower4x4.SetValue(Grid.RowProperty, r);
            ImgBtnFlower4x4.SetValue(Grid.ColumnProperty, c);

            //make mole visiable
            ImgBtnFlower4x4.IsVisible = true;
        }//end of MoleMovement 4x4

        //5x5 grid
        private void MoleMovement5x5()
        {
            int r = 0, c = 0;
            r = _random.Next(0, 4);
            c = _random.Next(0, 4);

            //move mole to new loc
            ImgBtnFlower4x4.SetValue(Grid.RowProperty, r);
            ImgBtnFlower4x4.SetValue(Grid.ColumnProperty, c);

            //make mole visiable
            ImgBtnFlower4x4.IsVisible = true;
        }//end of MoleMovement5x5

        public void BtnSwitch_Clicked(System.Object sender, System.EventArgs e)
        {
            //check test on button 
            //if its a 5x5, then make 5x grid visible and make the 3x grid invisible
            //if 3x3 then opposite
            //also need to change the text on the button when it swtichs 
            //use switch stament

            switch (BtnSwitch.Text)
            {
                case "5x5"://make 5x5 visible
                    {
                        GridMode4.IsVisible = false;
                        GridMode5.IsVisible = true;
                        BtnSwitch.Text = "4x4";
                        gridrest();
                        break;
                    }

                case "4x4"://make 4x4 visable
                    {
                        GridMode4.IsVisible = true;
                        GridMode5.IsVisible = false;
                        BtnSwitch.Text = "5x5";
                        gridrest();
                        break;
                    }

                default:
                    break;
            }//end of switch

        }//end of btnswtich 

        private void ImgBtnFlower_Clicked(System.Object sender, System.EventArgs e)
        {
        /*
        * making score
        * i. needs to get current socre
        * 2. needs to update score if play hits mole
        */
        int currentPoints = Convert.ToInt32(LblPoints.Text);
        currentPoints += 10;
        LblPoints.Text = currentPoints.ToString();

        /*make the image button dissipear when cliked 
            * use the send obj
            * 
            */
        //MoleMovement
        if (BtnSwitch.Text.Equals("5x5"))
        {
            MoleMovement4x4();
        }
        else if (BtnSwitch.Text.Equals("4x4"))
        {
            MoleMovement5x5();
        }

        ImageButton i = (ImageButton)sender;//save the sender

        }//end of ImgBtnflower4x4

    }//end of public class

}//end of namespce
v1l68za4

v1l68za41#

您可以在BtnSwitch_Clicked方法中进行一些更改。设置***IsEnabled***属性以及***IsVisible***属性。

public void BtnSwitch_Clicked(System.Object sender, System.EventArgs e)
    {
        //check test on button 
        //if its a 5x5, then make 5x grid visible and make the 3x grid invisible
        //if 3x3 then opposite
        //also need to change the text on the button when it swtichs 
        //use switch stament

        switch (BtnSwitch.Text)
        {
            case "5x5"://make 5x5 visible
                {
                    GridMode4.IsVisible = false;
                    GridMode4.IsEnabled = false;
                    GridMode5.IsVisible = true;
                    GridMode5.IsEnabled = true;
                    BtnSwitch.Text = "4x4";
                    gridrest();
                    break;
                }

            case "4x4"://make 4x4 visable
                {
                    GridMode4.IsVisible = true;
                    GridMode4.IsEnabled = true;
                    GridMode5.IsVisible = false;
                    GridMode5.IsEnabled = false;
                    BtnSwitch.Text = "5x5";
                    gridrest();
                    break;
                }

            default:
                break;
        }//end of switch

    }

我希望我的回答能帮助你。

相关问题