在ListView中执行命令SwitchCell OnChanged [英] Execute Command SwitchCell OnChanged in a ListView

查看:113
本文介绍了在ListView中执行命令SwitchCell OnChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Xamarin.Forms相当陌生,并尝试使用谷歌搜索我的问题. 我有一个SwitchCells的ListView,ItemsSource是一个简单的数据类的集合. 现在,当我在SwitchCell中更改Switch的状态时,我想调用一个Method ...这应该不是问题,但是我无法弄清楚如何知道SwitchCell,换句话说,如何知道DataClass的实例,其中项目已切换. 我希望它就像我注释过的代码一样,但是我真的不确定...我还认为我在这里混淆了命令和方法....

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....

我的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>

我的CodeBehind文件:

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

推荐答案

使用完整的数据绑定,还有更好的方法.

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

但是,如果您想使用现在的代码来实现它,则所需的值在sender参数中. sender的类型为SwitchCell,因此将其强制转换为BindingContext.最终事件将如下所示:

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.

这篇关于在ListView中执行命令SwitchCell OnChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆