简体   繁体   English

复选框不可单击| Xamarin.forms

[英]Checkbox is not clickable | Xamarin.forms

Basically, I have a checkbox in a content page, and I can't click it. 基本上,我在内容页面上有一个复选框,但无法单击它。 It shows it correctly, but when I click on it, nothing happens. 它可以正确显示它,但是当我单击它时,什么也没有发生。 It doesn't even check the checkbox, nothing happens as if its an image. 它甚至不选中该复选框,什么也没有发生,就好像它是一个图像一样。

I've tried: 我试过了:

IsEnabled="True"
IsChecked="True"

This is one of my 3 checkboxes: 这是我的3个复选框之一:

<CheckBox x:Name="cbop"
          Grid.Row="1"
          CheckedChanged="Cbop_CheckedChanged"
          IsEnabled="True"
          Grid.Column="3" />

This is the cbop_CheckedChange: 这是cbop_CheckedChange:

        private void Cbop_CheckedChanged(object sender, CheckedChangedEventArgs e)
        {
            cbhardcore.IsChecked = false;
            cbnormal.IsChecked = false;
        }

Full code of xaml: 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"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="App3.Views.GameSettings">
    <ContentPage.Content>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="120" />
                <RowDefinition Height="120" />
                <RowDefinition Height="120" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="25*" />
                <ColumnDefinition Width="20*" />
                <ColumnDefinition Width="20*" />
                <ColumnDefinition Width="23*" />
                <ColumnDefinition Width="10*" />
            </Grid.ColumnDefinitions>
            <CheckBox x:Name="cbhardcore"
                      Grid.Row="1"
                      CheckedChanged="Cbhardcore_CheckedChanged"
                      IsEnabled="True"
                      Grid.Column="1" />
            <CheckBox x:Name="cbnormal"
                      Grid.Row="1"
                      CheckedChanged="Cbnormal_CheckedChanged"
                      IsEnabled="True"
                      Grid.Column="2" />
            <CheckBox x:Name="cbop"
                      Grid.Row="1"
                      CheckedChanged="Cbop_CheckedChanged"
                      IsEnabled="True"
                      Grid.Column="3" />
            <Label x:Name="lblharcore"
                   Grid.Row="1"
                   Grid.Column="1"
                   Text="Hardcore" />
            <Label x:Name="lblnormal"
                   Grid.Row="1"
                   Grid.Column="2"
                   Text="Normal" />
            <Label x:Name="lblop"
                   Grid.Row="1"
                   Grid.Column="3"
                   Text="Overpowered" />
        </Grid>
    </ContentPage.Content>
</ContentPage>

Full code of behind: 后面的完整代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace App3.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class GameSettings : ContentPage
    {
        public GameSettings()
        {
            InitializeComponent();
        }

        private void Cbop_CheckedChanged(object sender, CheckedChangedEventArgs e)
        {
            cbhardcore.IsChecked = false;
            cbnormal.IsChecked = false;
        }

        private void Cbnormal_CheckedChanged(object sender, CheckedChangedEventArgs e)
        {
            cbhardcore.IsChecked = false;
            cbop.IsChecked = false;
        }

        private void Cbhardcore_CheckedChanged(object sender, CheckedChangedEventArgs e)
        {
            cbop.IsChecked = false;
            cbnormal.IsChecked = false;
        }
    }
}

Cause: You put the Label and CheckBox in the same cell of grid(the same row and column). 原因:您将LabelCheckBox放置在网格的同一单元格中(相同的行和列)。 So the checkbox will be covered by the label .You can set the BackgroundColor of Label to check it . 因此该复选框将被标签覆盖。您可以设置Label的BackgroundColor进行检查。

<Label x:Name="lblharcore"
               BackgroundColor="red"
               Grid.Row="0"
               Grid.Column="1"
               Text="Hardcore" />

Solution: Improve your layout. 解决方案:改善布局。

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="120" />
            <RowDefinition Height="120" />
            <RowDefinition Height="120" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="25*" />
            <ColumnDefinition Width="20*" />
            <ColumnDefinition Width="20*" />
            <ColumnDefinition Width="23*" />
            <ColumnDefinition Width="10*" />
        </Grid.ColumnDefinitions>

        <StackLayout Grid.Column="1" Grid.Row="1" VerticalOptions="Center" HorizontalOptions="Center">

            <Label x:Name="lblharcore"
                   BackgroundColor="red"                  
                   Text="Hardcore" />
            <CheckBox x:Name="cbhardcore"                     
                      CheckedChanged="Cbhardcore_CheckedChanged"
                      IsEnabled="True"
                       />
        </StackLayout>


        <StackLayout Grid.Column="2" Grid.Row="1" VerticalOptions="Center" HorizontalOptions="Center">
            <Label x:Name="lblnormal"                 
                   Text="Normal" />

            <CheckBox x:Name="cbnormal"                      
                      CheckedChanged="Cbnormal_CheckedChanged"
                      IsEnabled="True"
                       />
        </StackLayout>

        <StackLayout Grid.Column="3" Grid.Row="1" VerticalOptions="Center" HorizontalOptions="Center">
            <Label x:Name="lblop"                 
                   Text="Overpowered" />

            <CheckBox x:Name="cbop"                    
                      CheckedChanged="Cbop_CheckedChanged"
                      IsEnabled="True"
                       />

        </StackLayout>
</Grid>

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

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