如何从Xamarin表单中的另一页的XAML访问自定义视图的字段或控件?
我有一个自定义控件,我想添加将使用它的页面中的一些元素。
就像这样的一样
<Label>
<Label.Text>First Name</Label.Text>
</Label>
,label
是预定的,label
的文本属性在其他页面IE中添加。在使用它的位置,我想添加将从另一个页面分配值的控件,将使用它。
)中的自定义控制
<?xml version="1.0" encoding="UTF-8"?>
<ContentView ...>
<ContentView.Content>
<Frame x:Name="dialogContainer" Padding="0" BackgroundColor="Transparent">
<!--I want to use this StackLayout below and add controls inside it from other Page's XAML-->
<StackLayout x:Name="ChildStackLayout" x:FieldModifier="public" />
</Frame>
</ContentView.Content>
</ContentView>
这是我在XAML (
<controls:DialogView>
<controls:DialogView.ChildStackLayout>
<!--Here I want to add controls in my Custom Control-->
<Label Text="Hello, this is a custom dialog" />
</controls:DialogView.ChildStackLayout>
</controls:DialogView>
dialogview.xaml 页
I am having a Custom Control, I want to add some elements from the Page in which it will be used.
Just like this
<Label>
<Label.Text>First Name</Label.Text>
</Label>
As here, Label
is predefined, and Label
's Text property is added in other Page ie. where it is being used, I want to add controls whose values will be assigned from another Page in which, it will be used.
Here's my Custom Control in XAML (DialogView.xaml)
<?xml version="1.0" encoding="UTF-8"?>
<ContentView ...>
<ContentView.Content>
<Frame x:Name="dialogContainer" Padding="0" BackgroundColor="Transparent">
<!--I want to use this StackLayout below and add controls inside it from other Page's XAML-->
<StackLayout x:Name="ChildStackLayout" x:FieldModifier="public" />
</Frame>
</ContentView.Content>
</ContentView>
Here's how I am utilizing it (MainPage.xaml)
<controls:DialogView>
<controls:DialogView.ChildStackLayout>
<!--Here I want to add controls in my Custom Control-->
<Label Text="Hello, this is a custom dialog" />
</controls:DialogView.ChildStackLayout>
</controls:DialogView>
But ChildStackLayout
is not accessible in other Page
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能直接通过 name 属性将新控件添加到 xaml 中的自定义控件的子布局控件中。
首先,您可以在page.cs中添加新的控件。例如:
You cannot add new control into the custom control's child layout control in the xaml by the name property directly.
At first, you can add new control in the page.cs. Such as:
在自定义控件的代码后面添加一个公共属性,如您的情况所示,例如
DialogContent
(而不是ChildStackLayout
)。现在添加一个BindableProperty
来绑定它。然后使用其属性已更改。在自定义控件的 C# 代码隐藏中:
DialogContent
属于Layout
类型,因为通常一个对话框将包含多个元素,因此您可以使用任何元素像StackLayout
或其他布局这样的布局。所有布局都继承自Layout
,因此,您可以使用任何布局来显示对话框的内容。现在,当您想要使用此自定义控件时,您可以按照您想要的方式将其内容嵌套在 xaml 的父级中。
Add a public property in the Code behind of the Custom control, say
DialogContent
(instead ofChildStackLayout
) as in your case. Now add aBindableProperty
to Bind it. And then use its Property Changed.In C# Code Behind of the Custom control:
DialogContent
is of typeLayout
because usually a dialog will contain more than one element hence you can use any Layout likeStackLayout
or some other Layout. And all layouts inherit fromLayout
, hence, you can use any Layout for the content of the dialog.Now when you want to use this Custom Control, you can nest its content within the parent in xaml just as you wanted to do.