Xamarin Forms:如何通过 属性 名称获取 BindableProperty?
Xamarin Forms: How to get the BindableProperty by the property name?
Hi, i need to get the BindableProperty by the property name.
public BindableProperty GetBindableProperty(BindableObject bindableObj, string propertyName) {
if(typeof(Entry) == bindableObj.GetType()) {
if("Text" == propertyName) {
return (Entry.TextProperty);
}
if("TextColor" == propertyName) {
return (Entry.TextColorProperty);
}
}
return (null);
}
But i do not want to use this "if else" style.
Is there a general way that I don't need to judge the type and name?
Hi, I found the solution now:
public BindableProperty GetBindableProperty(BindableObject bindableObj, string propertyName) {
Type type = bindableObj.GetType();
FieldInfo fieldInfo;
while(null == (fieldInfo = type.GetField(propertyName + "Property", BindingFlags.Static | BindingFlags.Public))) {
type = type.BaseType;
}
if(null == fieldInfo) {
throw (new Exception("Can not find the BindableProperty for " + propertyName));
}
return ((BindableProperty)(fieldInfo.GetValue(bindableObj)));
}
Hi, i need to get the BindableProperty by the property name.
public BindableProperty GetBindableProperty(BindableObject bindableObj, string propertyName) {
if(typeof(Entry) == bindableObj.GetType()) {
if("Text" == propertyName) {
return (Entry.TextProperty);
}
if("TextColor" == propertyName) {
return (Entry.TextColorProperty);
}
}
return (null);
}
But i do not want to use this "if else" style. Is there a general way that I don't need to judge the type and name?
Hi, I found the solution now:
public BindableProperty GetBindableProperty(BindableObject bindableObj, string propertyName) {
Type type = bindableObj.GetType();
FieldInfo fieldInfo;
while(null == (fieldInfo = type.GetField(propertyName + "Property", BindingFlags.Static | BindingFlags.Public))) {
type = type.BaseType;
}
if(null == fieldInfo) {
throw (new Exception("Can not find the BindableProperty for " + propertyName));
}
return ((BindableProperty)(fieldInfo.GetValue(bindableObj)));
}