如何从Xamarin表单中的另一页的XAML访问自定义视图的字段或控件?

发布于 2025-01-18 22:54:02 字数 1278 浏览 4 评论 0原文

我有一个自定义控件,我想添加将使用它的页面中的一些元素。

就像这样的一样

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

转身泪倾城 2025-01-25 22:54:02

您不能直接通过 name 属性将新控件添加到 xaml 中的自定义控件的子布局控件中。

首先,您可以在page.cs中添加新的控件。例如:

 //declare the content view in the xaml
 <control:CustomView x:Name="customview">
 //add children control
 customview.ChildStackLayout.Children.Add(new Label() { Text = "hello"});

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:

 //declare the content view in the xaml
 <control:CustomView x:Name="customview">
 //add children control
 customview.ChildStackLayout.Children.Add(new Label() { Text = "hello"});
ゞ记忆︶ㄣ 2025-01-25 22:54:02

在自定义控件的代码后面添加一个公共属性,如您的情况所示,例如 DialogContent (而不是 ChildStackLayout)。现在添加一个BindableProperty来绑定它。然后使用其属性已更改

在自定义控件的 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 属于 Layout 类型,因为通常一个对话框将包含多个元素,因此您可以使用任何元素像 StackLayout 或其他布局这样的布局。所有布局都继承自 Layout,因此,您可以使用任何布局来显示对话框的内容。

现在,当您想要使用此自定义控件时,您可以按照您想要的方式将其内容嵌套在 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>

Add a public property in the Code behind of the Custom control, say DialogContent (instead of ChildStackLayout) as in your case. Now add a BindableProperty to Bind it. And then use its Property Changed.

In C# Code Behind of the Custom control:

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. And all layouts inherit from Layout, 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.

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文