xamarin.forms - Xamarin - 如何在集合视图中调用函数而不出错?
问题描述
我正在尝试使用 SelectionChanged 属性调用 Xamarin 项目中的函数。在这个属性中,我调用了一个在 cs 文件中声明的函数。这是 XAML 代码:
<CollectionView x:Name="PCCollection" SelectionMode="Single" SelectionChanged="Cell_Tapped" AutomationId="{Binding Tipologia_Alimento}">
这是 CS 函数:
private async void Cell_Tapped(object sender, System.EventArgs e) {
Console.WriteLine("Tapped");
Console.WriteLine((sender as Cell).AutomationId.ToString());
}
当我单击 Collection View 单元格时,它会打印值“Tapped”,但它也给了我中断模式错误:“应用程序处于中断模式”。
你能帮我解决这个错误吗?提前致谢。
解决方案
您的语法无效。集合视图控件没有 AutomationId 属性。
样本
<CollectionView ItemsSource="{Binding Monkeys}"
SelectionChanged="Cell_Tapped"
SelectionMode="Single">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid Padding="10">
<Label Grid.Column="1"
Text="{Binding Name}"
FontAttributes="Bold" />
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
void Cell_Tapped(object sender, SelectionChangedEventArgs e)
{
if (((CollectionView)sender).SelectedItem == null)
return;
string current = (e.CurrentSelection.FirstOrDefault() as Monkey)?.Name;
}
你可以在这里找到更多
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview/selection
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview/populate-data
推荐阅读
- postgresql - Neo-4j ETL 工具不会连接到本地 postgres 服务器。org.postgresql.util.PSQLException:不支持身份验证类型 10
- python - 如何在一个类中绘制多个精灵
- c++ - 在菱形继承问题之外使用虚拟继承来允许跨越
- javascript - 无法在 JavaScript Date() 对象中增加小时并获得预期的对象输出
- r - R data.table - 使用.SD时如何通过引用进行修改?
- android - Espresso 单击回收站视图中的两个项目
- python - 学习 Python 并需要帮助在不破坏其余代码的情况下移动符号
- javascript - 我想在删除按钮旁边添加编辑按钮,这样我就可以编辑我已经插入的值
- laravel - Laravel 在子域和主域的情况下处理路由
- go - 如何使用 Kubernetes e2e 框架构建自定义 kubernetes 测试