0

我想在点击集合视图的单元格(在cs中创建)时调用一个函数。

这是代码:

new StackLayout { Orientation = StackOrientation.Horizontal,
                  HorizontalOptions = LayoutOptions.FillAndExpand,
                  VerticalOptions = LayoutOptions.FillAndExpand,
                  HeightRequest = 200,
                  Children = {
                       new CollectionView {
                           HorizontalOptions = LayoutOptions.FillAndExpand,
                           SelectionMode = SelectionMode.Single,
                           ItemTemplate = MainPage.cv.ItemTemplate,
                           ItemsLayout = MainPage.cv.ItemsLayout,
                           ItemsSource = MainPage.jsonList.Where(x => x.Giorno == MainPage.day && x.Pasto == Pasto_Array[i]),
                       }
                           //.SelectionChanged +=CheckDuplicazioni_SelectionChanged;
                   }
                 }

如您所见,我尝试使用注释的代码进行操作,但它不起作用。在 XAML 文件中,我使用了 SelectionChanged 函数并且它有效,为什么在 cs id 中没有?

等待您的反馈,谢谢。

4

1 回答 1

2

您可以通过将CollectionView实例保存在变量中来做到这一点

var collectionView = new CollectionView()
{
    HorizontalOptions = LayoutOptions.FillAndExpand,
    SelectionMode = SelectionMode.Single,
    ItemTemplate = MainPage.cv.ItemTemplate,
    ItemsLayout = MainPage.cv.ItemsLayout,
    ItemsSource = MainPage.jsonList.Where(x => x.Giorno == MainPage.day && x.Pasto == Pasto_Array[i]),
};

collectionView.SelectionChanged += CheckDuplicazioni_SelectionChanged;

new StackLayout
{
    Orientation = StackOrientation.Horizontal,
    HorizontalOptions = LayoutOptions.FillAndExpand,
    VerticalOptions = LayoutOptions.FillAndExpand,
    HeightRequest = 200,
    Children = {
        collectionView
   }
};
于 2021-06-11T13:13:53.830 回答