Xamarin从ContentView获取子对象
我为ContentView创建了自定义呈现程序,并使用以下代码向ContentView添加标签:Xamarin从ContentView获取子对象,xamarin,xamarin.forms,xamarin.android,xamarin.ios,Xamarin,Xamarin.forms,Xamarin.android,Xamarin.ios,我为ContentView创建了自定义呈现程序,并使用以下代码向ContentView添加标签: Label text = new Label { Text = entry.Text, TextColor = Color.FromHex(SelectedTextColor), FontSize = 30 }; draggableView.Content = text; <ContentView x:Class="demo2.simplecontrol.View1" x
Label text = new Label { Text = entry.Text, TextColor = Color.FromHex(SelectedTextColor), FontSize = 30 };
draggableView.Content = text;
<ContentView
x:Class="demo2.simplecontrol.View1"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<ContentView.Content>
<StackLayout>
<Entry x:Name="label1" />
<Entry x:Name="label2" />
</StackLayout>
</ContentView.Content>
但是现在我想编辑标签上的文本,所以我需要再次访问它。问题是ContentView没有属性Children
,所以我不能这样访问它
如何仍然编辑标签?正如Jason所说,您需要在ContentView上创建可绑定属性,然后在其他contentpage中使用此ContentView,将属性绑定到ContentView的可绑定属性,然后您可以通过可绑定属性更新ContentView中的标签文本,我做了一个示例,您可以查看: ContentView:
Label text = new Label { Text = entry.Text, TextColor = Color.FromHex(SelectedTextColor), FontSize = 30 };
draggableView.Content = text;
<ContentView
x:Class="demo2.simplecontrol.View1"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<ContentView.Content>
<StackLayout>
<Entry x:Name="label1" />
<Entry x:Name="label2" />
</StackLayout>
</ContentView.Content>
内容页:
<ContentPage
x:Class="demo2.simplecontrol.Page10"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:demo2.simplecontrol">
<ContentPage.Content>
<StackLayout>
<Label
HorizontalOptions="CenterAndExpand"
Text="Welcome to Xamarin.Forms!"
VerticalOptions="CenterAndExpand" />
<local:View1 Label1="{Binding text1}" Label2="{Binding text2}" />
</StackLayout>
</ContentPage.Content>
您可以更改text1和text2值以更新Contentview中的标签文本。根据您提供的代码,内容等于标签,因此您可以直接使用类型为Xamarin.Forms.Label的内容属性访问它:
Label text = new Label { Text = entry.Text, TextColor = Color.FromHex(SelectedTextColor), FontSize = 30 };
draggableView.Content = text;
((Label)draggableView.Content).Text = "my updated text";
在自定义ContentView上公开将更新标签内容的公共属性或方法我猜
内容
是您要查找的属性,而不是子项
。不,因为这样我需要执行类似于mContentView.Content.Text的操作,而这是不允许的谢谢,但在我的情况下,有些事情是不同的。我以编程方式将标签添加到contentview内部的布局中,因此我没有x:name,但在每个contentview中总是只有一个标签。也许我可以通过知道只有一个来访问它?