I am attempting to create a custom BindableProperty
for a custom view which takes in a string when consumed through XAML but is internally implemented as a custom structure as follows:
struct X {
public Y A;
public Z B;
// Y and Z are data types
// pertaining to a struct
}
public class MyView : CustomView {
public static readonly BindableProperty
MyValueProperty = BindableProperty.Create
(propertyName: "MyValue",
returnType: typeof(string),
declaringType: typeof(MyView),
defaultValue: null,
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: MyValuePropertyChanged);
static void MyValuePropertyChanged(BindableObject obj, object oldValue, object newValue) {
MyView view = (MyView)obj;
string enteredStringValue = (string)newValue;
// Set the MyValue field as required
}
public X MyValue {
// Implement getters and setters
}
}
My initial instinct to create the aforementioned property was to declare the above fields and generate the required instance of struct X
with respect to the string
passed in XAML in the MyValuePropertyChanged
method, but it is deemed impossible as an exception is thrown during runtime denoting the illegality of such declaration.
In synopsis, I am attempting to replicate the behavior of the Xamarin.Forms.GridLength
structure whose string representations in XAML code is converted to the required structure when accessed through the getters and setters.
Thanks in advance.
The said behavior can be implemented by defining a custom type converter via inheritance of the
Xamarin.Forms.TypeConverter
class and decorating the aforementioned custom converter by theXamarin.Forms.Xaml.TypeConversion
attribute and the concerned structure by theXamarin.Forms.TypeConverter
attribute as follows:A predefined converter by Xamarin can also be used when needed as follows: