对于我的Tizen .net Wearable应用程序,我希望能够在按下/切换选中图标时删除列表视图项。但是问题是check元素在CustomClass中,而listview在AppClass中。
我尝试将检查元素设置为全局,但不幸的是没有成功。
namespace TizenWearableApp5
{
public class App : Application
{
public App()
{
// The root page of your application
CirclePage circlePage = new CirclePage();
CircleListView listView = new CircleListView();
listView.ItemTemplate = new DataTemplate(typeof(CustomCell));
listView.ItemsSource = getTasks.Taken;
listView.HasUnevenRows = true;
MainPage = circlePage;
circlePage.Content = listView;
CustomCell.check.Toggled += (s, e) =>
{
};
}
}
public class Taken
{
public string Name { get; set; }
public string Team { get; set; }
}
public class CustomCell : ViewCell
{
public static Check check = new Check();
public ObservableCollection<TaskViewModel> Taken { get; set; }
public CustomCell()
{
Taken = new ObservableCollection<TaskViewModel>();
StackLayout cell = new StackLayout()
{
HeightRequest = 120,
HorizontalOptions = LayoutOptions.FillAndExpand,
Orientation = StackOrientation.Horizontal,
VerticalOptions = LayoutOptions.FillAndExpand,
WidthRequest = 360,
};
StackLayout left = new StackLayout()
{
HorizontalOptions = LayoutOptions.CenterAndExpand,
Orientation = StackOrientation.Vertical,
VerticalOptions = LayoutOptions.Center,
};
Label name = new Label()
{
FontSize = 8,
HorizontalOptions = LayoutOptions.Center,
HorizontalTextAlignment = TextAlignment.Center,
VerticalOptions = LayoutOptions.Center,
VerticalTextAlignment = TextAlignment.Center,
};
Label team = new Label()
{
FontSize = 5,
HorizontalOptions = LayoutOptions.Center,
HorizontalTextAlignment = TextAlignment.Center,
VerticalOptions = LayoutOptions.Center,
VerticalTextAlignment = TextAlignment.Center,
};
check.DisplayStyle = CheckDisplayStyle.Default;
check.HorizontalOptions = LayoutOptions.End;
check.VerticalOptions = LayoutOptions.Center;
//Set Binding
name.SetBinding(Label.TextProperty, new Binding("Name"));
team.SetBinding(Label.TextProperty, new Binding("Team"));
View = cell;
cell.Children.Add(left);
left.Children.Add(name);
left.Children.Add(team);
cell.Children.Add(check);
}
}
public static class getTasks
{
public static IList<Taken> Taken { get; set; }
static getTasks()
{
Taken = new ObservableCollection<Taken>() {
new Taken
{
Name = "Martin",
Team = "Red"
},
new Taken
{
Name = "John",
Team = "Blue"
}
};
}
}
}
我希望能够从检查事件中删除项目
答案 0 :(得分:2)
您可以使用getTasks.Taken
访问ListView的Items源,因为它是静态的
您可以从实例化customcell的BindingContext中获取项目
因此,现在您可以使用IList的Remove
方法从Items来源中删除一个项目
这是解决方案,请用以下代码替换CustomCell
public class CustomCell : ViewCell
{
public ObservableCollection<TaskViewModel> Taken { get; set; }
public CustomCell()
{
Taken = new ObservableCollection<TaskViewModel>();
StackLayout cell = new StackLayout()
{
HeightRequest = 120,
HorizontalOptions = LayoutOptions.FillAndExpand,
Orientation = StackOrientation.Horizontal,
VerticalOptions = LayoutOptions.FillAndExpand,
WidthRequest = 360,
};
StackLayout left = new StackLayout()
{
HorizontalOptions = LayoutOptions.CenterAndExpand,
Orientation = StackOrientation.Vertical,
VerticalOptions = LayoutOptions.Center,
};
Label name = new Label()
{
FontSize = 8,
HorizontalOptions = LayoutOptions.Center,
HorizontalTextAlignment = TextAlignment.Center,
VerticalOptions = LayoutOptions.Center,
VerticalTextAlignment = TextAlignment.Center,
};
Label team = new Label()
{
FontSize = 5,
HorizontalOptions = LayoutOptions.Center,
HorizontalTextAlignment = TextAlignment.Center,
VerticalOptions = LayoutOptions.Center,
VerticalTextAlignment = TextAlignment.Center,
};
Check check = new Check();
check.DisplayStyle = CheckDisplayStyle.Default;
check.HorizontalOptions = LayoutOptions.End;
check.VerticalOptions = LayoutOptions.Center;
check.Toggled += (s, e) =>
{
getTasks.Taken.Remove((s as Check).BindingContext as Taken);
};
//Set Binding
name.SetBinding(Label.TextProperty, new Binding("Name"));
team.SetBinding(Label.TextProperty, new Binding("Team"));
View = cell;
cell.Children.Add(left);
left.Children.Add(name);
left.Children.Add(team);
cell.Children.Add(check);
}
}
答案 1 :(得分:1)
您可以像这样在CustomCell
中自定义事件:
public class CustomCell : ViewCell
{
public event EventHandler OnToggled;
public CustomCell()
{
Check check = new Check();
...
check.Toggled += Check_Toggled;
}
private void Check_Toggled(object sender, EventArgs e)
{
OnToggled?.Invoke(sender, e);
}
}
然后您可以处理CustomCell
的事件:
CustomCell.OnToggled += (s, e) =>
{
};