[英]How to access fields or controls of Custom View from another Page's XAML in Xamarin Forms?
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.如这里,
Label
是预定义的,并且Label
的 Text 属性被添加到其他页面即。 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)这是我在 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)下面是我如何使用它 (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但是
ChildStackLayout
在其他Page中是访问不到的
You cannot add new control into the custom control's child layout control in the xaml by the name property directly. xaml中自定义控件的子布局控件不能通过name属性直接添加新控件。
At first, you can add new control in the page.cs.首先,您可以在 page.cs 中添加新控件。 Such as:
如:
//declare the content view in the xaml
<control:CustomView x:Name="customview">
//add children control
customview.ChildStackLayout.Children.Add(new Label() { Text = "hello"});
Add a public property in the Code behind of the Custom control, say DialogContent
(instead of ChildStackLayout
) as in your case.在自定义控件的代码后面添加一个公共属性,比如
DialogContent
(而不是ChildStackLayout
),就像您的情况一样。 Now add a BindableProperty
to Bind it.现在添加一个
BindableProperty
来绑定它。 And then use its Property Changed .然后使用它的Property Changed 。
In C# Code Behind of the Custom control:在自定义控件的 C# 代码后面:
public partial class DialogView : Dialog // Dialog inherits ContentView
{
public Layout DialogContent { get => (Layout)GetValue(DialogContentProperty); set => SetValue(DialogContentProperty, value); }
public static readonly BindableProperty DialogContentProperty = BindableProperty.Create(nameof(DialogContent), typeof(Layout), typeof(DialogView), propertyChanged: DialogContentPropertyChanged);
public static void DialogContentPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var control = (DialogView)bindable;
control.dialog.Content = newValue as Layout;
}
}
DialogContent
is of type Layout
because usually a dialog will contain more than one element hence you can use any Layout like StackLayout
or some other Layout. DialogContent
属于Layout
类型,因为通常一个对话框会包含多个元素,因此您可以使用任何布局,如StackLayout
或其他一些布局。 And all layouts inherit from Layout
, hence, you can use any Layout for the content of the dialog.并且所有布局都继承自
Layout
,因此,您可以为对话框的内容使用任何布局。
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.现在当你想使用这个自定义控件时,你可以将它的内容嵌套在父级 xaml 中,就像你想做的那样。
<controls:DialogView>
<controls:DialogView.DialogContent>
<StackLayout Padding="10" Spacing="10">
<Label Text="Hello, this is a custom dialog" />
<Button Text="OK" />
</StackLayout>
</controls:DialogView.DialogContent>
</controls:DialogView>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.