【发布时间】:2020-09-04 00:30:21
【问题描述】:
我对 Xamarin.Forms 还很陌生,并尝试用谷歌搜索我的问题。 我有一个 SwitchCells 的 ListView,ItemsSource 是一个简单数据类的集合。
现在,当我在 SwitchCell 中更改 Switch 的状态时,我希望调用一个方法...这应该不是问题,但我无法弄清楚我是如何知道哪个 SwitchCell,或者换句话说是哪个 Instance DataClass 的,哪个 Item 被切换了。
我希望它类似于我评论的代码,但我真的不确定......我认为我在这里混淆了命令和方法......
我的 XAML:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="slwAppTutorial.InterestsList">
<StackLayout>
<ListView x:Name="interestList">
<ListView.ItemTemplate>
<DataTemplate>
<SwitchCell Text="{Binding Text}" On="false" > <!--OnChanged="{Binding something}" -->
</SwitchCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button x:Name="submit" Text="Bestätigen"></Button>
</StackLayout>
</ContentPage>
我的代码隐藏文件:
namespace slwAppTutorial
{
public partial class InterestsList : ContentPage
{
//InterestListManager manager;
List<InterestsItem> myList = new List<InterestsItem>();
public InterestsList()
{
InitializeComponent();
InterestsItem myInterest = new InterestsItem() { Id = "1234", Text = "Volleyball", Kind = "Sport" };
for (int i=0; i < 25; i++) { myList.Add(myInterest); }
//manager = InterestListManager.DefaultManager;
// My Expected Command
// someSwitch.OnChanged+= (sender, args) =>
// {
// var selectedItem = args.Item as InterestsItem;
//
// Do something with my InterestsItem
//
// DisplayAlert(title: selectedItem.Text, message: selectedItem.Kind, cancel: "OK");
// if (selectedItem == null) return;
// };
protected override async void OnAppearing()
{
base.OnAppearing();
interestList.ItemsSource = myList;
//await RefreshItems(true, syncItems: false);
}
private async Task RefreshItems(bool showActivityIndicator, bool syncItems)
{
interestList.ItemsSource = myList; //await manager.GetInterestItemsAsync(syncItems);
}
}
}
【问题讨论】:
标签: c# listview xamarin.forms switch-statement