I've created an custom AbstractDataEditor DataType that looks like this (without all the other code):
public class ImageResizerDataType : AbstractDataEditor { [DataEditorSetting("Width", description = "Max image width", type = typeof(TextField))] public string Width { get; set; } }
This works fine but now I want to access the setting in an ApplicationBase event handler like this:
if (property.PropertyType.DataTypeDefinition.DataType.Id == ImageResizerDataType.DataTypeId) { var dataType = (ImageResizerDataType) property.PropertyType.DataTypeDefinition.DataType;
if (dataType.Width.Contains(".")) { /* Do something */ } }
This throws a "Object references not set to an instance of an object." error because the dataType.Width is not initialized.
So how can I access the DataType settings within an event handler?
Getting DataType settings within event handler
Hey all,
I've created an custom AbstractDataEditor DataType that looks like this (without all the other code):
public class ImageResizerDataType : AbstractDataEditor
{
[DataEditorSetting("Width", description = "Max image width", type = typeof(TextField))]
public string Width { get; set; }
}
This works fine but now I want to access the setting in an ApplicationBase event handler like this:
if (property.PropertyType.DataTypeDefinition.DataType.Id == ImageResizerDataType.DataTypeId)
{
var dataType = (ImageResizerDataType) property.PropertyType.DataTypeDefinition.DataType;
if (dataType.Width.Contains("."))
{
/* Do something */
}
}
This throws a "Object references not set to an instance of an object." error because the dataType.Width is not initialized.
So how can I access the DataType settings within an event handler?
In what event are you calling this code?
In this wiki and topic you can see how a datatype is used on a custom page. Maybe you can use that to get your datatype in the event.
Jeroen
I'm calling this code in a Media.AfterSave event but I found a solution (all settings are stored as prevalues):
// Get datatype prevalues
var prevalues = PreValues.GetPreValues(property.PropertyType.DataTypeDefinition.Id);
// Get datatype settings
var width = Convert.ToInt32(((PreValue)prevalues[1]).Value);
I just hope the prevalues array indexes will be consistent because the AbstractDataEditor saves the DataEditorSetting instances automatically.
Thanks for your response!
Yes I think the array is consistent. In DAMP we also assume the array index will stay the same.
Jeroen
is working on a reply...