public DataModelInputViewModel GetDataModelInputViewModel(Type propertyType, DataModelPropertyAttribute description, object initialValue, Action <object, bool> updateCallback) { lock (_registeredDataModelEditors) { // Prefer a VM that natively supports the type DataModelVisualizationRegistration match = _registeredDataModelEditors.FirstOrDefault(d => d.SupportedType == propertyType); // Fall back on a VM that supports the type through conversion if (match == null) { match = _registeredDataModelEditors.FirstOrDefault(d => d.CompatibleConversionTypes.Contains(propertyType)); } // Lastly try getting an enum VM if the provided type is an enum if (match == null && propertyType.IsEnum) { match = _registeredDataModelEditors.FirstOrDefault(d => d.SupportedType == typeof(Enum)); } if (match != null) { DataModelInputViewModel viewModel = InstantiateDataModelInputViewModel(match, description, initialValue); viewModel.UpdateCallback = updateCallback; return(viewModel); } return(null); } }
private DataModelInputViewModel InstantiateDataModelInputViewModel(DataModelVisualizationRegistration registration, DataModelPropertyAttribute description, object initialValue) { // The view models expecting value types shouldn't be given null, avoid that if (registration.SupportedType.IsValueType) { if (initialValue == null) { initialValue = Activator.CreateInstance(registration.SupportedType); } } // This assumes the type can be converted, that has been checked when the VM was created if (initialValue != null && initialValue.GetType() != registration.SupportedType) { initialValue = Convert.ChangeType(initialValue, registration.SupportedType); } IParameter[] parameters = new IParameter[] { new ConstructorArgument("targetDescription", description), new ConstructorArgument("initialValue", initialValue) }; DataModelInputViewModel viewModel = (DataModelInputViewModel)registration.PluginInfo.Kernel.Get(registration.ViewModelType, parameters); viewModel.CompatibleConversionTypes = registration.CompatibleConversionTypes; return(viewModel); }
private DataModelInputViewModel InstantiateDataModelInputViewModel(DataModelVisualizationRegistration registration, DataModelPropertyAttribute?description, object?initialValue) { // This assumes the type can be converted, that has been checked when the VM was created if (initialValue != null && initialValue.GetType() != registration.SupportedType) { initialValue = Convert.ChangeType(initialValue, registration.SupportedType); } IParameter[] parameters = { new ConstructorArgument("targetDescription", description), new ConstructorArgument("initialValue", initialValue) }; // If this ever happens something is likely wrong with the plugin unload detection if (registration.Plugin.Kernel == null) { throw new ArtemisSharedUIException("Cannot InstantiateDataModelInputViewModel for a registration by an uninitialized plugin"); } DataModelInputViewModel viewModel = (DataModelInputViewModel)registration.Plugin.Kernel.Get(registration.ViewModelType, parameters); viewModel.CompatibleConversionTypes = registration.CompatibleConversionTypes; return(viewModel); }