Execute Command SwitchCell OnChanged in a ListView

流过昼夜 提交于 2020-01-07 05:50:28

问题


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 wich SwitchCell, or in other words wich Instance of the DataClass, wich 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);
    }


}
}

If anything is wrong with my question please tell me and I'll try to alter it or provide more Information


回答1:


There are better ways to do this, using full data binding.

But if you would want to implement it with the code you have now, the value you're looking for is in the sender parameter. The sender will be of type SwitchCell, so cast it and get the BindingContext. The final event will look like this:

private void Handle_OnChanged(object sender, ToggledEventArgs args)
{
    var selectedItem = ((SwitchCell)sender).BindingContext as Foo;

    DisplayAlert(title: selectedItem.Text, message: selectedItem.Bar.ToString(), cancel: "OK");                
}

A sample project, similar to your code can be found here.




回答2:


I think you should use Switch not SwitchCell

Switch has a IsToggledProperty

or a Toggled event



来源:https://stackoverflow.com/questions/44410601/execute-command-switchcell-onchanged-in-a-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!