我是移动应用开发新手。我正在使用 Xamarin 开发 Android 应用程序。在 hello world 应用程序的 OnCreate 方法中,我看到以下代码:
Button button = FindViewById<Button>(Resource.Id.MyButton);
所以我尝试以同样的方式创建我自己的按钮。我在设计器中创建按钮,并在 OnCreate 方法中添加以下行:
Button myOwnBtn = FindViewById<Button>(Resource.Id.MyOwnBtn);
这给了我一个错误,没有 MyOwnBtn。然后我查看 Id 类的代码,看到这样一行:
public const int MyButton=2123344112;
如果我把这行放在那里:
public const int MyOwnBtn=2123344113;
一切正常。但据我了解,它应该自动生成,否则为每个控件放置一个唯一的编号会有点困难。
谁能告诉我我做错了什么? FindViewById() 是如何工作的?
最佳答案
您必须将 id
MyOwnBtn
提供给您在设计器中创建的 Button
。
findViewById
是 View
类的一种方法,它会查找具有您在参数中提供的 id
的 subview 。
Look for a child view with the given id. If this view has the given id, return this view.
关于c# - FindViewById() 如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25278612/