Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin从ContentView获取子对象_Xamarin_Xamarin.forms_Xamarin.android_Xamarin.ios - Fatal编程技术网

Xamarin从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

我为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>
但是现在我想编辑标签上的文本,所以我需要再次访问它。问题是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中总是只有一个标签。也许我可以通过知道只有一个来访问它?