如何从XAML设置我的自定义类的成员View的属性? (Xamarin.forms)

时间:2022-06-30 00:21:47

I'm making app with using Xamarin.forms.

我正在使用Xamarin.forms制作应用程序。

I already asked question here. How to set child of class' property with using xaml? (Xamarin.forms) But I couldn't get right answer for this, or there may be no solution for that.

我已经在这里提问了。如何使用xaml设置类'属性的子项? (Xamarin.forms)但我无法得到正确的答案,或者可能没有解决方案。

What I want to do is setting my class's view's property from ContentPage's XAML. my class has some view like Image and else.

我想要做的是从ContentPage的XAML设置我的类的视图属性。我的班级有一些像Image和其他人的观点。

I searched and found that there is 'ControlTemplete'. But I'm not sure it's what I'm looking for.

我搜索并发现有'ControlTemplete'。但我不确定这是我在寻找什么。

And I also don't think putting BindableProperty and OnPropertyChangedDelegate codes for every property that I want to set is a best way.

而且我也不认为为我想要设置的每个属性设置BindableProperty和OnPropertyChangedDelegate代码是最好的方法。

Is there another better solution?

有没有更好的解决方案?

Thanks.

谢谢。

1 个解决方案

#1


0  

You can map XAML that is inside your control to a property using ContentProperty attribute.

您可以使用ContentProperty属性将控件内的XAML映射到属性。

[ContentProperty("MyContent")]
public class MyControl : ContentView 
{
    public View MyContent { get; set; }
}

And in XAML somthing like this

在XAML这样的事情

<local:MyControl>
    <Grid></Grid>
</local:MyControl>

this limits you to only one property but should work with any types.

这限制了您只有一个属性,但应该适用于任何类型。

#1


0  

You can map XAML that is inside your control to a property using ContentProperty attribute.

您可以使用ContentProperty属性将控件内的XAML映射到属性。

[ContentProperty("MyContent")]
public class MyControl : ContentView 
{
    public View MyContent { get; set; }
}

And in XAML somthing like this

在XAML这样的事情

<local:MyControl>
    <Grid></Grid>
</local:MyControl>

this limits you to only one property but should work with any types.

这限制了您只有一个属性,但应该适用于任何类型。