예제 #1
0
        private void SetupServices()
        {
            string servicesAttribute = (string)Document.Body.GetAttribute(ServicesAttribute);

            if (String.IsNullOrEmpty(servicesAttribute))
            {
                return;
            }

            string[] serviceNames = servicesAttribute.Split(",");
            int      serviceCount = serviceNames.Length;

            for (int i = 0; i < serviceCount; i++)
            {
                string name = serviceNames[i];

                ServiceRegistration registration = _registeredServices[name];
                Debug.Assert(registration != null, "Unknown service '" + name + "'");

                // TODO: Add support for deferred loading of service implementation, where
                //       the service gets included later, even though it is declared at the
                //       page level.

                object service = GetObject(registration.ServiceImplementationType);

                IInitializable initializableService = service as IInitializable;
                if (initializableService != null)
                {
                    Dictionary <string, object> options = OptionsParser.GetOptions(Document.Body, name);
                    initializableService.BeginInitialization(options);
                    initializableService.EndInitialization();
                }

                RegisterObject(registration.ServiceType, service);
            }
        }
예제 #2
0
        public void ActivateFragment(Element element, bool contentOnly, object model)
        {
            Debug.Assert(element != null);

            if (element == Document.Body)
            {
                // Perform app-level activation (tied to body initialization)

                // Setup top-level services specified declaratively.
                SetupServices();

                // Create the model declaratively associated with the application.
                if (model == null)
                {
                    string modelTypeName = (string)Document.Body.GetAttribute(ModelTypeAttribute);
                    if (String.IsNullOrEmpty(modelTypeName) == false)
                    {
                        Type modelType = Type.GetType(modelTypeName);
                        Debug.Assert(modelType != null, "Could not resolve model '" + modelTypeName + "'");

                        model = GetObject(modelType);

                        IInitializable initializableModel = model as IInitializable;
                        if (initializableModel != null)
                        {
                            Dictionary <string, object> modelData = OptionsParser.GetOptions(element, "model");
                            initializableModel.BeginInitialization(modelData);
                            initializableModel.EndInitialization();
                        }
                    }
                }

                _model = model;
            }

            // Create expressions and bindings associated with the specified elements and
            // the contained elements. Do this first, as this allows behaviors to look at
            // values set as a result of bindings when they are attached.
            if ((contentOnly == false) && element.HasAttribute(Application.BindingsAttribute))
            {
                SetupBindings(element, model);
            }

            ElementCollection boundElements = element.QuerySelectorAll(Application.BindingsSelector);

            for (int i = 0, boundElementCount = boundElements.Length; i < boundElementCount; i++)
            {
                SetupBindings(boundElements[i], model);
            }

            // Attach behaviors associated declaratively with the specified element and the
            // contained elements.
            if ((contentOnly == false) && element.HasAttribute(Application.BehaviorsAttribute))
            {
                AttachBehaviors(element);
            }

            ElementCollection extendedElements = element.QuerySelectorAll(Application.BehaviorsSelector);

            for (int i = 0, extendedElementCount = extendedElements.Length; i < extendedElementCount; i++)
            {
                AttachBehaviors(extendedElements[i]);
            }
        }