在代码中设置了属性的按钮未触发点击事件

时间:2018-11-05 16:09:18

标签: c# xaml xamarin.forms

在后面的代码中设置按钮的translationX和translationY时,不会触发其单击事件:该按钮似乎无法被单击。这些按钮是StackLayout的一部分,我使用x:Name从后面的代码中访问它们!

除翻译外,按钮属性均在XAML中设置,前者的设置如下:

element.TranslationX = array[i].xPos;
element.TranslationY = array[i].yPos;

更新

隐藏代码

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        button.Clicked += Button_Clicked;

        CircleLayout(132.0, Math.PI, 7, "menuGrid");
    }

    public void CircleLayout(double r, double theta, int nbElements, String currentMenu)
    {
        CircleObject[] array = new CircleObject[nbElements];

        for (int i = 1; i < nbElements; i++)
        {
            if (i == 0)
            {
                array[i] = new CircleObject(r * Math.Sin(theta), r * Math.Cos(theta));
            } else
            {
                theta += (Math.PI / 180) * 360 / nbElements;
                array[i] = new CircleObject(r * Math.Sin(theta), r * Math.Cos(theta));
            }

            StackLayout element = this.FindByName<StackLayout>(currentMenu + i);
            element.TranslationX = array[i].xPos;
            element.TranslationY = array[i].yPos;
        }
    }

    public class CircleObject
    {
        public double xPos;
        public double yPos;

        public CircleObject(double x, double y)
        {
            xPos = x;
            yPos = y;
        }
    }

    private void Button_Clicked(object sender, EventArgs e)
    {
        //do something
    }
}

XAML中的按钮示例

<StackLayout x:Name="menuGrid0">
        <Button x:Name="button"
                BackgroundColor="#FF6633"
                CornerRadius="70"
                HeightRequest="65"
                WidthRequest="65"
                HorizontalOptions="Center"/>

        <Label x:Name="textDetection"
               Text="D E T E C T I O N"
               FontSize="9"
               TextColor="#FF6633"
               HeightRequest="10"
               HorizontalOptions="Center"/>
</StackLayout>

0 个答案:

没有答案