protected ViewModelBase(IServiceLocator serviceLocator, bool supportIEditableObject = true, bool ignoreMultipleModelsWarning = false,
bool skipViewModelAttributesInitialization = false)
{
UniqueIdentifier = UniqueIdentifierHelper.GetUniqueIdentifier<ViewModelBase>();
ViewModelConstructionTime = FastDateTime.Now;
if (CatelEnvironment.IsInDesignMode)
{
return;
}
Log.Debug("Creating view model of type '{0}' with unique identifier {1}", GetType().Name, UniqueIdentifier);
_ignoreMultipleModelsWarning = ignoreMultipleModelsWarning;
if (serviceLocator == null)
{
serviceLocator = ServiceLocator.Default;
}
#if !XAMARIN
DependencyResolver = serviceLocator.ResolveType<IDependencyResolver>();
_dispatcherService = DependencyResolver.Resolve<IDispatcherService>();
#endif
// In silverlight, automatically invalidate commands when property changes
#if !WINDOWS_PHONE && !NET35
ValidateModelsOnInitialization = true;
#endif
ViewModelCommandManager = MVVM.ViewModelCommandManager.Create(this);
ViewModelCommandManager.AddHandler(async (viewModel, propertyName, command, commandParameter) =>
{
var eventArgs = new CommandExecutedEventArgs((ICatelCommand)command, commandParameter, propertyName);
CommandExecuted.SafeInvoke(this, eventArgs);
await CommandExecutedAsync.SafeInvokeAsync(this, eventArgs);
});
InvalidateCommandsOnPropertyChanged = true;
SupportIEditableObject = supportIEditableObject;
// Temporarily suspend validation, will be enabled at the end of constructor again
SuspendValidation = true;
if (!skipViewModelAttributesInitialization)
{
InitializeViewModelAttributes();
}
ViewModelManager.RegisterViewModelInstance(this);
object[] interestedInAttributes = GetType().GetCustomAttributesEx(typeof(InterestedInAttribute), true);
foreach (InterestedInAttribute interestedInAttribute in interestedInAttributes)
{
ViewModelManager.AddInterestedViewModelInstance(interestedInAttribute.ViewModelType, this);
}
InitializeThrottling();
// Enable validation again like we promised some lines of code ago
SuspendValidation = false;
// As a last step, enable the auditors (we don't need change notifications of previous properties, etc)
AuditingHelper.RegisterViewModel(this);
}
#endregion