c# - Xamarin 表格 : How to access button name in XAML from C#?

标签 c# xaml xamarin xamarin.forms

我有以下 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/

相关文章:

wpf - 从 XAML 绑定(bind)到自定义依赖属性

c# - Xamarin Android - 隐藏 TextView

xamarin - XAML 智能感知在 VS15 中不起作用

c# - 将布局保存到位图(Xamarin)

c# - 所有华为设备上发生中止崩溃 - Google Play 管理中心

c# - 任务完成源 : When to use SetResult() versus TrySetResult(), 等

c# - 在输出窗口中打印堆栈跟踪

c# - 如何删除 <a :To s:mustUnderstand ="1"> from SOAP Request

c# - WPF如何更新CanExecute

windows-phone-7 - 带有 Windows Phone 工具包的多目标 WP7.1/WP8 应用程序 (Panorama/LongListSelector)