I'm fairly new to Xamarin.Forms and tried googling my question.
I have a ListView of SwitchCells, the ItemsSource is a Collection of a simple Data-Class.
Now when I change the Switch's state in the SwitchCell I want to have a Method being called...that should not be the problem, but I can't figure out how I know which SwitchCell, or in other words which Instance of the DataClass, which Item was switched.
I expect it to be something like the code I commented, but am really not sure...also I think that I'm mixing up Commands and Methods here....
My 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>
My CodeBehind file:
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);
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…