0

Please see below code. If I click on 'Beer' or 'Spirit' private void MyButton_Clicked(object sender, EventArgs e) will be called. However nothing happens if I click 'Wine' Or 'Cider' does anyone know why this is?

 <StackLayout VerticalOptions="StartAndExpand" HorizontalOptions="StartAndExpand">

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="100"/>
                <RowDefinition Height="100"/>
                <RowDefinition Height="100"/>
            </Grid.RowDefinitions>

            <StackLayout Grid.Row="0" VerticalOptions="StartAndExpand" HorizontalOptions="StartAndExpand">
                <Button Text="Products" Clicked="Button_Clicked"/>
            </StackLayout>

            <StackLayout x:Name="stack1" Grid.Row="1" Orientation="Vertical" VerticalOptions="StartAndExpand" HorizontalOptions="StartAndExpand">

            </StackLayout>

            <StackLayout x:Name="stack2" Grid.Row="1" Orientation="Vertical" VerticalOptions="StartAndExpand" HorizontalOptions="StartAndExpand">

            </StackLayout>

            <StackLayout x:Name="stack3" Grid.Row="1" Orientation="Vertical" VerticalOptions="StartAndExpand" HorizontalOptions="StartAndExpand">

            </StackLayout>

            <StackLayout x:Name="stack4" Grid.Row="1" Orientation="Vertical" VerticalOptions="StartAndExpand" HorizontalOptions="StartAndExpand">

            </StackLayout>

        </Grid>

    </StackLayout>




    private void Button_Clicked(object sender, EventArgs e)
    {
        List<MyButton> myButtons = new List<MyButton>() { new MyButton("Beer", MyButton_Clicked), new MyButton("Spirit", MyButton_Clicked), new MyButton("Wine", MyButton_Clicked), new MyButton("Cider", MyButton_Clicked)};

        stack1.Children.Clear();
        stack2.Children.Clear();
        stack3.Children.Clear();

        foreach (MyButton myButton in myButtons)
        {
            stack1.Children.Add(myButton);
        }
    }

    private void MyButton_Clicked(object sender, EventArgs e)
    {
        var mybutton = sender as MyButton;
        var title = mybutton.Text;
        List<MyButton> myButtons = new List<MyButton>();
        string stackList = "";

        if (title == "Beer")
        {
            stack1.Children.Clear();
            stackList = "stack2";
            myButtons = new List<MyButton>() { new MyButton("Beer", MyButton_Clicked), new MyButton("Guinness", MyButton_Clicked), new MyButton("Coors", MyButton_Clicked), new MyButton("Bud", MyButton_Clicked) };
        }
        else if (title == "Spirit")
        {
            stack1.Children.Clear();
            stackList = "stack2";
            myButtons = new List<MyButton>() { new MyButton("Spirit", MyButton_Clicked), new MyButton("Vodka", MyButton_Clicked), new MyButton("Whiskey", MyButton_Clicked), new MyButton("Gin", MyButton_Clicked) };
        }
        else if (title == "Wine")
        {
            stack1.Children.Clear();
            stackList = "stack2";
            myButtons = new List<MyButton>() { new MyButton("Wine", MyButton_Clicked), new MyButton("Bin55", MyButton_Clicked), new MyButton("Hardys", MyButton_Clicked), new MyButton("120", MyButton_Clicked) };
        }
        else if (title == "Cider")
        {
            stack1.Children.Clear();
            stackList = "stack2";
            myButtons = new List<MyButton>() { new MyButton("Cider", MyButton_Clicked), new MyButton("Magners", MyButton_Clicked), new MyButton("Frosty_Jack", MyButton_Clicked) };
        }
        //Second list
        else if (title == "Vodka")
        {
            stack1.Children.Clear();
            stackList = "stack3";
            myButtons = new List<MyButton>() { new MyButton("Spirit", MyButton_Clicked), new MyButton("Vodka", MyButton_Clicked), new MyButton("Smirnoff", MyButton_Clicked), new MyButton("Glens", MyButton_Clicked), new MyButton("Vladivar", MyButton_Clicked) };
        }
        else if (title == "Whiskey")
        {
            stack1.Children.Clear();
            stackList = "stack3";
            myButtons = new List<MyButton>() { new MyButton("Spirit", MyButton_Clicked), new MyButton("Whiskey", MyButton_Clicked), new MyButton("Jameson", MyButton_Clicked), new MyButton("Bushmills", MyButton_Clicked), new MyButton("Powers", MyButton_Clicked) };
        }
        else if (title == "Gin")
        {
            stack1.Children.Clear();
            stackList = "stack3";
            myButtons = new List<MyButton>() { new MyButton("Spirit", MyButton_Clicked), new MyButton("Gin", MyButton_Clicked), new MyButton("Gordons", MyButton_Clicked), new MyButton("Bombay", MyButton_Clicked), new MyButton("Hendricks's", MyButton_Clicked), new MyButton("Beefeater", MyButton_Clicked) };
        }
        //Next list
        else if (title == "Bin55")
        {
            //myButtons = new List<MyButton>() { new MyButton("Smirnoff", MyButton_Clicked), new MyButton("Glens", MyButton_Clicked), new MyButton("Vladivar", MyButton_Clicked) };
        }
        else if (title == "Hardys")
        {
           // myButtons = new List<MyButton>() { new MyButton("Jameson", MyButton_Clicked), new MyButton("Bushmills", MyButton_Clicked), new MyButton("Powers", MyButton_Clicked) };
        }
        else if (title == "120")
        {
          //  myButtons = new List<MyButton>() { new MyButton("Jameson", MyButton_Clicked), new MyButton("Bushmills", MyButton_Clicked), new MyButton("Powers", MyButton_Clicked) };
        }
        if (stackList == "stack2")
        {
            stack2.Children.Clear();

            foreach (MyButton myButton in myButtons)
            {
                stack3.Children.Add(myButton);
            }
        }

        if (stackList == "stack3")
        {
            stack3.Children.Clear();

            foreach (MyButton myButton in myButtons)
            {
                stack4.Children.Add(myButton);
            }
        }
    }
}

public class MyButton : Button
{
    public MyButton(string title, EventHandler clicked)
    {
        this.Text = title;
        Clicked += clicked;
    }
}

enter image description here

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
John
  • 3,965
  • 21
  • 77
  • 163
  • If it is a POC kinda project it might work but just to mention the way you are writing all code base logics - that's not a good way to do it. You may have used Bindable layout or Listview, gridview and bind all your drinks as a String then in the UI you could have generated the buttons and bind all event to commands.. – N Subedi Nov 18 '19 at 19:37
  • ahh yes that sounds like a bit of refactoring...could you perhaps point me in the direction of a tutorial for this? thanks – John Nov 19 '19 at 12:44
  • This is a huge topic however you can start searching for Xamarin Forms Mvvm Pattern. For this project you have right now, i would end up using bindable layout to achieve the same https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/bindable-layouts – N Subedi Nov 19 '19 at 15:12
  • ok cool thanks for the help – John Nov 19 '19 at 15:33

1 Answers1

1

If you add a background color to stack1, you will find the problem easily. Here is a screenshot:

enter image description here

The height of stack1 is 100 and you are adding 4 four buttons there. Wine and Cider button are added out of the bounds so it won't response to any event.

You can give a larger height to make the button work:

   <Grid.RowDefinitions>
        <RowDefinition Height="100"/>
        <RowDefinition Height="200"/>
        <RowDefinition Height="100"/>
    </Grid.RowDefinitions>
nevermore
  • 15,432
  • 1
  • 12
  • 30