问题
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;
}
}
回答1:
If you add a background color to stack1
, you will find the problem easily. Here is a screenshot:
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>
来源:https://stackoverflow.com/questions/58920807/xamarin-forms-c-sharp-stacklayout