我这里有一堆代码很可能类似,但只是在一些静态(名称)上有所不同。
public void Windows() {
var win = new ListViewItem { Content = "Windows" };
Main.Items.Add(win);
win.Tapped += Windowstapp;
string[] lv= new string[20];
for (int x = 0; x <=19; x++) {
lv[x] = " Windows" + (x+1);
}
foreach (string val in lv) {
var lvi = new ListViewItem { Content = val };
Main.Items.Add(lvi);
lvi.Visibility = Visibility.Collapsed;
if (iden == false) {
lvi.Visibility = Visibility.Visible;
sv.ScrollToVerticalOffset(0);
}
}
}
public void Doors() {
var door = new ListViewItem { Content = "Door" };
Main.Items.Add(door);
door.Tapped += DoorTapp;
string[] lv = new string[20];
for (int x = 0; x <= 19; x++)
{
lv[x] = " Doors" + (x + 1);
}
foreach (string val in lv)
{
var lvi = new ListViewItem { Content = val };
Main.Items.Add(lvi);
lvi.Visibility = Visibility.Collapsed;
if (id == false)
{
lvi.Visibility = Visibility.Visible;
sv.ScrollToVerticalOffset(0);
}
}
}
我计划创建一个这样的方法:
public void House(string a) {
var win = new ListViewItem { Content = a };
Main.Items.Add(win);
win.Tapped += Windowstapp;
string[] lv= new string[20];
for (int x = 0; x <=19; x++) {
lv[x] = " " + a + (x+1);
}
foreach (string val in lv) {
var lvi = new ListViewItem { Content = val };
Main.Items.Add(lvi);
lvi.Visibility = Visibility.Collapsed;
if (iden == false) {
lvi.Visibility = Visibility.Visible;
sv.ScrollToVerticalOffset(0);
}
}
}
但我在胜利中遇到了问题。 Tapped += WindowsTapp;
因为他们没有分享同一事件,我该怎么做才能实现这一目标?也许像win.Tapped += a+WindowsTapp
?
提前致谢!
答案 0 :(得分:0)
只需传入您要用于订阅该活动的委托:
// I can't find the docs for ListViewItem.Tapped right now - adjust the
// type of the "handler" parameter to match the event
public void CreateItem(string contentName, TappedEventHandler handler)
{
var item = new ListViewItem { Content = a };
Main.Items.Add(item);
item.Tapped += handler;
...
}
然后用:
调用它CreateItem("Door", DoorTapped);
CreateItem("Window", WindowTapped);
(我将方法名称更改为更常规 - 当然,您也需要更改声明。)