对于我的 Tizen .net Wearable 应用程序,我希望能够在按下/切换复选图标时删除 ListView 项。然而,问题在于检查元素在 CustomClass 中,而 ListView 在 AppClass 中。
我试过将 check 元素设为全局,但不幸的是没有成功。
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"
}
};
}
}
}
我希望能够从检查事件中删除一个项目
您可以使用 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);
}
}
我是一名优秀的程序员,十分优秀!