我有这个按钮和工作的Switch指令。问题是,当我点击名为“Koala”的案例时,当我只想要其中一个时,就会发生2个动作。我该如何解决这个问题?
private void KinectTileButtonClick(object sender, RoutedEventArgs e)
{
var button = (KinectTileButton)e.Source;
switch (button.Label.ToString())
{
case "Koala2":
KinectTileButton_Click_1(sender, e);
break;
}
var wineModel = button.Tag as WineModel;
var selectionDisplay = new SelectionDisplay(wineModel);
this.kinectRegionGrid.Children.Add(selectionDisplay);
e.Handled = true;
}
答案 0 :(得分:2)
我相信您只是不希望执行测试后的操作:
所以你可以在测试中添加一个回报。
休息不会帮助你“走出方法”,它只是为了转换。
private void KinectTileButtonClick(object sender, RoutedEventArgs e)
{
var button = (KinectTileButton)e.Source;
if(button.Label.ToString() == "Koala2")
{
KinectTileButton_Click_1(sender, e);
return;//get out of this method.
}
var wineModel = button.Tag as WineModel;
var selectionDisplay = new SelectionDisplay(wineModel);
this.kinectRegionGrid.Children.Add(selectionDisplay);
e.Handled = true;
}
或使用if / else
private void KinectTileButtonClick(object sender, RoutedEventArgs e)
{
var button = (KinectTileButton)e.Source;
if(button.Label.ToString() == "Koala2")
KinectTileButton_Click_1(sender, e);
else
{
var wineModel = button.Tag as WineModel;
var selectionDisplay = new SelectionDisplay(wineModel);
this.kinectRegionGrid.Children.Add(selectionDisplay);
}
e.Handled = true;
}
顺便说一句,它可能更干净(好吧,我喜欢小方法)
private void KinectTileButtonClick(object sender, RoutedEventArgs e)
{
var button = (KinectTileButton)e.Source;
if(button.Label.ToString() == "Koala2")
Method1(<somearguments>);
else
Method2(button.Tag as WineModel);
e.Handled = true;//if it must happen for all label values.
}
public void Method1(<some parameters>) {
//DoSomething
}
public void Method2(WineModel wineModel) {
var selectionDisplay = new SelectionDisplay(wineModel);
this.kinectRegionGrid.Children.Add(selectionDisplay);
}