【问题标题】:Execute Command SwitchCell OnChanged in a ListView在 ListView 中执行命令 SwitchCell OnChanged
【发布时间】: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


    【解决方案1】:

    有更好的方法来做到这一点,使用完整的数据绑定。

    但是,如果您想使用现有代码实现它,您要查找的值在 sender 参数中。 sender 将是 SwitchCell 类型,所以转换它并获得 BindingContext。最终事件将如下所示:

    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");                
    }
    

    可以找到与您的代码类似的示例项目here

    【讨论】:

      【解决方案2】:

      我认为你应该使用Switch 而不是SwitchCell

      Switch 有一个IsToggledProperty

      Toggled 事件

      【讨论】:

      • 感谢您的回答,这也是我想到的一种方法(实际上是因为您在另一个线程上的一个答案),但总的来说我喜欢使用原生 Elements/Cells 的想法。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-26
      • 2019-04-16
      • 2017-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多