请看下面的代码。如果我点击“啤酒”或“烈酒”, private void MyButton_Clicked(object sender, EventArgs e)
将被调用。但是,如果我单击“葡萄酒”或“苹果酒”,则没有任何反应,有人知道这是为什么吗?
<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;
}
}