Bob*_*tor 8 xamarin xamarin.forms
我正在尝试为标签添加名称,以便我可以使用Content.FindByName <>()方法找到它
据我所知,Xamarin.Forms.Label()类没有Name属性
这是我的页面类
public class HelloWordCodePage : ContentPage
{
public Label HelloWorldLabel;
public HelloWordCodePage()
{
HelloWorldLabel = new Label{Text = "Hello World", };
ConstructView();
}
public void ConstructView()
{
Content = new StackLayout
{
Children =
{
HelloWorldLabel
}
};
}
}
Run Code Online (Sandbox Code Playgroud)
Ido*_*doT 10
如果您在Xaml中定义标签,则可以使用此syntex:
<Label x:Name="YourLableName" Text="A simple Label" />
Run Code Online (Sandbox Code Playgroud)
然后像这样在后面的代码中访问它
YourLableName.Text = "bla bla"
Run Code Online (Sandbox Code Playgroud)
但是,如果您不使用Xaml,并且所有页面定义都在后面的代码中找到,那么您应该保留对该标签的引用,以便访问它而不是使用Content.FindByName<>()@Sten Petrov评论来查找它
我相信该名称仅在 XAML 中创建元素时使用。如果您查看继承,您会发现Element在底部,它显式实现了 INameScope。您不能将 Label 强制转换为 INameScope,因为这是一个内部接口:
namespace Xamarin.Forms
{
internal interface INameScope
{
object FindByName(string name);
void RegisterName(string name, object scopedElement);
void UnregisterName(string name);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13149 次 |
| 最近记录: |