我有以下 XAML 标记:
<ListView x:Name="wordsListView">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout>
<Label Text="{Binding Keyword}" />
<Button Text="Click Me" x:Name="randomButton"></Button>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
在我的 .xaml.cs 文件中,random
按钮的按钮单击事件定义如下:
randomButton.Clicked += (object sender, EventArgs e) =>
{
a = rand.Next(words.Count);
word = words[a];
};
但不知何故,当我构建我的项目时,我不断收到错误:The name randomButton does not exists in the current context.
谁能告诉我我做错了什么以及应该如何做?
最佳答案
您不能通过名称访问任何控件,甚至不能给它命名,只要它在模板 View 上,命名它是不合适的,因为模板上的这个控件将在运行时重复!
但是您可以从单击事件的发送者访问整个控件...这取决于您的开发方法
关于c# - Xamarin 表格 : How to access button name in XAML from C#?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39182077/