Ejemplo n.º 1
0
        private async Task OnViewModelCommandExecutedAsync(object sender, CommandExecutedEventArgs e)
        {
            CommandHandler[]      syncHandlers;
            AsyncCommandHandler[] asyncHandlers;

            lock (_lock)
            {
                syncHandlers  = _commandHandlers.ToArray();
                asyncHandlers = _asyncCommandHandlers.ToArray();
            }

            foreach (var handler in syncHandlers)
            {
                if (handler != null)
                {
                    handler(_viewModel, _commands[e.Command], e.Command, e.CommandParameter);
                }
            }

            foreach (var handler in asyncHandlers)
            {
                if (handler != null)
                {
                    await handler(_viewModel, _commands[e.Command], e.Command, e.CommandParameter);
                }
            }
        }
Ejemplo n.º 2
0
#pragma warning disable AvoidAsyncVoid // Avoid async void
        private async void OnViewModelCommandExecuted(object sender, CommandExecutedEventArgs e)
#pragma warning restore AvoidAsyncVoid // Avoid async void
        {
            CommandHandler[]      syncHandlers;
            AsyncCommandHandler[] asyncHandlers;

            lock (_lock)
            {
                syncHandlers  = _commandHandlers.ToArray();
                asyncHandlers = _asyncCommandHandlers.ToArray();
            }

            foreach (var handler in syncHandlers)
            {
                if (handler != null)
                {
                    handler(_viewModel, _commands[e.Command], e.Command, e.CommandParameter);
                }
            }

            foreach (var handler in asyncHandlers)
            {
                if (handler != null)
                {
                    await handler(_viewModel, _commands[e.Command], e.Command, e.CommandParameter);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Called when the <see cref="ViewModelBase.CommandExecuted"/> event is raised.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Catel.MVVM.CommandExecutedEventArgs"/> instance containing the event data.</param>
        private Task OnViewModelCommandExecutedAsync(object sender, CommandExecutedEventArgs e)
        {
            lock (_lock)
            {
                var viewModels = (from viewModel in _interestedViewModels
                                  select viewModel.Value).ToList();

                foreach (var viewModel in viewModels)
                {
                    try
                    {
                        var notifyableViewModel = viewModel as INotifyableViewModel;
                        if (notifyableViewModel != null)
                        {
                            notifyableViewModel.ViewModelCommandExecuted((IViewModel)sender, e.Command, e.CommandParameter);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex, "Failed to let an interested view model know that a view model has changed. Probably the view model is not correctly cleaned up");
                    }
                }
            }

            return(TaskHelper.Completed);
        }
Ejemplo n.º 4
0
#pragma warning disable AvoidAsyncVoid // Avoid async void
        private async void OnViewModelCommandExecuted(object sender, CommandExecutedEventArgs e)
#pragma warning restore AvoidAsyncVoid // Avoid async void
        {
            CommandHandler[]      syncHandlers;
            AsyncCommandHandler[] asyncHandlers;
            var commandName = string.Empty;

            lock (_lock)
            {
                syncHandlers  = _commandHandlers.ToArray();
                asyncHandlers = _asyncCommandHandlers.ToArray();

                if (!_commands.TryGetValue(e.Command, out commandName))
                {
                    commandName = "unknown";
                }
            }

            foreach (var handler in syncHandlers)
            {
                if (handler != null)
                {
                    handler(_viewModel, commandName, e.Command, e.CommandParameter);
                }
            }

            foreach (var handler in asyncHandlers)
            {
                if (handler != null)
                {
                    await handler(_viewModel, commandName, e.Command, e.CommandParameter);
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Raises the <see cref="Executed" /> event.
        /// </summary>
        /// <param name="parameter">The parameter.</param>
        /// <returns>Task.</returns>
        protected virtual void RaiseExecuted(object parameter)
        {
            var action = new Action(() =>
            {
                var eventArgs = new CommandExecutedEventArgs(this, parameter);
                Executed?.Invoke(this, eventArgs);
            });

            AutoDispatchIfRequired(action);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Raises the <see cref="Executed" /> event.
        /// </summary>
        /// <param name="parameter">The parameter.</param>
        /// <returns>Task.</returns>
        protected Task RaiseExecutedAsync(object parameter)
        {
            var action = new Func <Task>(async() =>
            {
                var eventArgs = new CommandExecutedEventArgs(this, parameter);
                Executed.SafeInvoke(this, eventArgs);
                await ExecutedAsync.SafeInvokeAsync(this, eventArgs);
            });

            return(AutoDispatchIfRequiredAsync(action));
        }
Ejemplo n.º 7
0
 private void OnViewModelCommandExecuted(object sender, CommandExecutedEventArgs e)
 {
     lock (_lock)
     {
         foreach (var handler in _commandHandlers)
         {
             if (handler != null)
             {
                 handler(_viewModel, _commands[e.Command], e.Command, e.CommandParameter);
             }
         }
     }
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Called when the <see cref="ViewModelBase.CommandExecuted"/> event is raised.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Catel.MVVM.CommandExecutedEventArgs"/> instance containing the event data.</param>
        private Task OnViewModelCommandExecutedAsync(object sender, CommandExecutedEventArgs e)
        {
            lock (_lock)
            {
                var viewModels = (from viewModel in _interestedViewModels
                                  select viewModel.Value).ToList();

                foreach (var viewModel in viewModels)
                {
                    try
                    {
                        var notifyableViewModel = viewModel as INotifyableViewModel;
                        if (notifyableViewModel != null)
                        {
                            notifyableViewModel.ViewModelCommandExecuted((IViewModel)sender, e.Command, e.CommandParameter);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex, "Failed to let an interested view model know that a view model has changed. Probably the view model is not correctly cleaned up");
                    }
                }
            }

            return TaskHelper.Completed;
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ViewModelBase"/> class.
        /// <para/>
        /// This constructor allows the injection of a custom <see cref="IServiceLocator"/>.
        /// </summary>
        /// <param name="serviceLocator">The service locator to inject. If <c>null</c>, the <see cref="Catel.IoC.ServiceLocator.Default"/> will be used.</param>
        /// <param name="supportIEditableObject">if set to <c>true</c>, the view model will natively support models that
        /// implement the <see cref="IEditableObject"/> interface.</param>
        /// <param name="ignoreMultipleModelsWarning">if set to <c>true</c>, the warning when using multiple models is ignored.</param>
        /// <param name="skipViewModelAttributesInitialization">if set to <c>true</c>, the initialization will be skipped and must be done manually via <see cref="InitializeViewModelAttributes"/>.</param>
        /// <exception cref="ModelNotRegisteredException">A mapped model is not registered.</exception>
        /// <exception cref="PropertyNotFoundInModelException">A mapped model property is not found.</exception>
        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);
        }
Ejemplo n.º 10
0
 private void OnViewModelCommandExecuted(object sender, CommandExecutedEventArgs e)
 {
     lock (_lock)
     {
         foreach (var handler in _commandHandlers)
         {
             if (handler != null)
             {
                 handler(_viewModel, _commands[e.Command], e.Command, e.CommandParameter);
             }
         }
     }
 }
        private async Task OnViewModelCommandExecutedAsync(object sender, CommandExecutedEventArgs e)
        {
            CommandHandler[] syncHandlers;
            AsyncCommandHandler[] asyncHandlers;

            lock (_lock)
            {
                syncHandlers = _commandHandlers.ToArray();
                asyncHandlers = _asyncCommandHandlers.ToArray();
            }

            foreach (var handler in syncHandlers)
            {
                if (handler != null)
                {
                    handler(_viewModel, _commands[e.Command], e.Command, e.CommandParameter);
                }
            }

            foreach (var handler in asyncHandlers)
            {
                if (handler != null)
                {
                    await handler(_viewModel, _commands[e.Command], e.Command, e.CommandParameter);
                }
            }
        }