C# 如何将传递值的多个单击事件合并为一个?
我有以下代码,为我的游戏级菜单的每个按钮点击事件传递一个参数C# 如何将传递值的多个单击事件合并为一个?,c#,xaml,button,windows-phone-8,windows-phone,C#,Xaml,Button,Windows Phone 8,Windows Phone,我有以下代码,为我的游戏级菜单的每个按钮点击事件传递一个参数 private void btnLevelVeryEasy_Click(object sender, RoutedEventArgs e) { NavigationService.Navigate(new Uri("/GamePlay.xaml?parameter=0", UriKind.Relative)); } private void btnLevelEasy_Click(obj
private void btnLevelVeryEasy_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/GamePlay.xaml?parameter=0", UriKind.Relative));
}
private void btnLevelEasy_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/GamePlay.xaml?parameter=1", UriKind.Relative));
}
private void btnLevelMedium_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/GamePlay.xaml?parameter=2", UriKind.Relative));
}
private void btnLevelHard_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/GamePlay.xaml?parameter=3", UriKind.Relative));
}
private void btnLevelInsane_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/GamePlay.xaml?parameter=4", UriKind.Relative));
}
我的问题是,如何通过让所有按钮触发一键事件并传递唯一参数来更优雅地执行此操作?差不多
private void btnLevel_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/GamePlay.xaml?parameter=[buttontag]", UriKind.Relative));
}
进一步说,根据按钮的命名方式,您可能可以使用
string buttonName = ((Button)sender).Name;
button是你的button类
然后解析这个字符串,得到包含在你名字中的数字
比如说
string lastChar = buttonName[buttonName.length - 1];
编辑如果希望保持名称不变,则可以使用switch语句
string s;
switch(((Button)sender).Name)
{
case "btnLevelEasy":
s = "1";
break;
进一步说,根据按钮的命名方式,您可能可以使用
string buttonName = ((Button)sender).Name;
button是你的button类
然后解析这个字符串,得到包含在你名字中的数字
比如说
string lastChar = buttonName[buttonName.length - 1];
编辑如果希望保持名称不变,则可以使用switch语句
string s;
switch(((Button)sender).Name)
{
case "btnLevelEasy":
s = "1";
break;
Sayse几乎做对了,除了.Name应该在()后面: 编辑:
哦,你知道如何在每个按钮上链接事件吗?(在“事件”中,只需单击下拉菜单并选择上一个创建的事件)说,除了.Name应该在()后面之外,几乎正确了: 编辑:
哦,你知道如何在每个按钮上链接事件吗?(在事件中,只需单击下拉菜单并选择上一个创建的事件)true,该死的清晨@Sayse Yeah mornings是最糟糕的:如果你不想将按钮名称与逻辑联系起来,你可以使用Tag属性而不是Name属性True,该死的,你每天早上都很早@Sayse Yeah mornings是最糟糕的:如果您不想将按钮名称与逻辑绑定,那么可以使用Tag属性而不是Name属性