如何从XAML设置我的自定义类的成员View的属性? (Xamarin.forms) [英] How to set my custom class's member View's property from XAML? (Xamarin.forms)
问题描述
我正在使用Xamarin.forms制作应用程序.
I'm making app with using Xamarin.forms.
我已经在这里问过问题了. 如何设置班级子级'使用XAML的财产? (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.
我要做的是从ContentPage的XAML设置类的视图的属性. 我班上有一些像图像的视图,其他的都有.
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.
我搜索了发现"ControlTemplete".但是我不确定这是我要找的东西.
I searched and found that there is 'ControlTemplete'. But I'm not sure it's what I'm looking for.
我也不认为将BindableProperty和OnPropertyChangedDelegate代码放在我要设置的每个属性中都是最好的方法.
And I also don't think putting BindableProperty and OnPropertyChangedDelegate codes for every property that I want to set is a best way.
还有另一个更好的解决方案吗?
Is there another better solution?
谢谢.
推荐答案
您可以使用ContentProperty属性将控件内部的XAML映射到属性.
You can map XAML that is inside your control to a property using ContentProperty attribute.
[ContentProperty("MyContent")]
public class MyControl : ContentView
{
public View MyContent { get; set; }
}
在XAML中这样的东西
And in XAML somthing like this
<local:MyControl>
<Grid></Grid>
</local:MyControl>
这将您限制为仅一个属性,但应可用于任何类型.
this limits you to only one property but should work with any types.
这篇关于如何从XAML设置我的自定义类的成员View的属性? (Xamarin.forms)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!