Пример #1
0
        /// <summary>
        /// Runs the initialization sequence to configure the Crystal application.
        /// </summary>
        protected virtual void Initialize()
        {
            ContainerLocator.SetContainerExtension(CreateContainerExtension);
            _containerExtension = ContainerLocator.Current;
            _moduleCatalog      = CreateModuleCatalog();
            RegisterRequiredTypes(_containerExtension);
            RegisterTypes(_containerExtension);
            _containerExtension.FinalizeExtension();

            ConfigureModuleCatalog(_moduleCatalog);
            ConfigureRegionAdapterMappings(_containerExtension.Resolve <RegionAdapterMappings>());
            ConfigureDefaultRegionBehaviors(_containerExtension.Resolve <IRegionBehaviorFactory>());

            RegisterFrameworkExceptionTypes();

            var shell = CreateShell();

            if (shell != null)
            {
                MvvmHelpers.AutowireViewModel(shell);
                RegionManager.SetRegionManager(shell, _containerExtension.Resolve <IRegionManager>());
                RegionManager.UpdateRegions();
                InitializeShell(shell);
            }

            InitializeModules();
        }
Пример #2
0
        private void InvokeOnSynchronizedActiveAwareChildren(object item, Action <IActiveAware> invocation)
        {
            var dependencyObjectView = (DependencyObject)item;

            if (dependencyObjectView == null)
            {
                return;
            }

            // We are assuming that any scoped region managers are attached directly to the view.
            var regionManager = RegionManager.GetRegionManager(dependencyObjectView);

            // If the view's RegionManager attached property is different from the region's RegionManager,
            // then the view's region manager is a scoped region manager.
            if (regionManager == null || regionManager == Region.RManager)
            {
                return;
            }

            var activeViews     = regionManager.Regions.SelectMany(e => e.ActiveViews);
            var syncActiveViews = activeViews.Where(ShouldSyncActiveState);

            foreach (var syncActiveView in syncActiveViews)
            {
                MvvmHelpers.ViewAndViewModelAction(syncActiveView, invocation);
            }
        }
Пример #3
0
        private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                foreach (object item in e.NewItems)
                {
                    void Invocation(IActiveAware activeAware) => activeAware.IsActive = true;

                    MvvmHelpers.ViewAndViewModelAction(item, (Action <IActiveAware>)Invocation);
                    InvokeOnSynchronizedActiveAwareChildren(item, Invocation);
                }
            }
            else if (e.Action == NotifyCollectionChangedAction.Remove)
            {
                foreach (object item in e.OldItems)
                {
                    void Invocation(IActiveAware activeAware) => activeAware.IsActive = false;

                    MvvmHelpers.ViewAndViewModelAction(item, (Action <IActiveAware>)Invocation);
                    InvokeOnSynchronizedActiveAwareChildren(item, Invocation);
                }
            }

            // May need to handle other action values (reset, replace). Currently the ViewsCollection class does not raise CollectionChanged with these values.
        }
        /// <summary>
        /// Provides a new item for the region based on the supplied candidate target contract name.
        /// </summary>
        /// <param name="candidateTargetContract">The target contract to build.</param>
        /// <returns>An instance of an item to put into the <see cref="IRegion"/>.</returns>
        protected virtual object CreateNewRegionItem(string candidateTargetContract)
        {
            object newRegionItem;

            try
            {
                newRegionItem = _container.Resolve <object>(candidateTargetContract);
                MvvmHelpers.AutowireViewModel(newRegionItem);
            }
            catch (Exception e)
            {
                throw new InvalidOperationException(Format(CannotCreateNavigationTarget, candidateTargetContract), e);
            }
            return(newRegionItem);
        }
Пример #5
0
        private static bool ShouldKeepAlive(object inactiveView)
        {
            IRegionMemberLifetime lifetime = MvvmHelpers.GetImplementerFromViewOrViewModel <IRegionMemberLifetime>(inactiveView);

            if (lifetime != null)
            {
                return(lifetime.KeepAlive);
            }

            RegionMemberLifetimeAttribute lifetimeAttribute = GetItemOrContextLifetimeAttribute(inactiveView);

            if (lifetimeAttribute != null)
            {
                return(lifetimeAttribute.KeepAlive);
            }

            return(true);
        }
Пример #6
0
        /// <summary>
        /// Configure <see cref="IDialogWindow"/> content.
        /// </summary>
        /// <param name="dialogName">The name of the dialog to show.</param>
        /// <param name="window">The hosting window.</param>
        /// <param name="parameters">The parameters to pass to the dialog.</param>
        protected virtual void ConfigureDialogWindowContent(string dialogName, IDialogWindow window, IDialogParameters parameters)
        {
            var content = _containerExtension.Resolve <object>(dialogName);

            if (content is not FrameworkElement dialogContent)
            {
                throw new NullReferenceException("A dialog's content must be a FrameworkElement");
            }

            MvvmHelpers.AutowireViewModel(dialogContent);

            if (dialogContent.DataContext is not IDialogAware viewModel)
            {
                throw new NullReferenceException("A dialog's ViewModel must implement the IDialogAware interface");
            }
            ConfigureDialogWindowProperties(window, dialogContent, viewModel);
            MvvmHelpers.ViewAndViewModelAction <IDialogAware>(viewModel, d => d.OnDialogOpened(parameters));
        }