Exemplo n.º 1
0
        public static void DisplayGuiScreen()
        {
            _currentScreen = new GUIScreen(_uvContentManager, _screen_entities);
            var screens = _context.GetUI().GetScreens();

            _context.GetUI().GetScreens().Open(_currentScreen, TimeSpan.Zero);
        }
        /// <summary>
        /// Creates a new instance of the <see cref="NavigationExpression"/> from the
        /// specified <see cref="UvssNavigationExpression"/> object.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        /// <param name="uvssexp">The UVSS navigation expression from which to create a new structure.</param>
        /// <returns>The <see cref="NavigationExpression"/> that was created.</returns>
        public static NavigationExpression?FromUvssNavigationExpression(UltravioletContext uv, UvssNavigationExpression uvssexp)
        {
            Contract.Require(uv, nameof(uv));

            if (uvssexp == null)
            {
                return(null);
            }

            var upf = uv.GetUI().GetPresentationFoundation();

            var navigationPropertyName  = new DependencyName(uvssexp.NavigationProperty);
            var navigationPropertyIndex = uvssexp.NavigationPropertyIndex;
            var navigationPropertyType  = default(Type);

            if (uvssexp.NavigationPropertyType != null)
            {
                if (!upf.GetKnownType(uvssexp.NavigationPropertyType, false, out navigationPropertyType))
                {
                    throw new UvssException(PresentationStrings.UnrecognizedType.Format(uvssexp.NavigationPropertyType));
                }
            }

            return(new NavigationExpression(navigationPropertyName, navigationPropertyType, navigationPropertyIndex));
        }
        /// <inheritdoc/>
        public override void Initialize(UltravioletContext uv, Object configuration)
        {
            var config = (PresentationFoundationConfiguration)configuration ?? new PresentationFoundationConfiguration();

            var upf = uv.GetUI().GetPresentationFoundation();
            upf.BindingExpressionCompilerAssemblyName = config.BindingExpressionCompilerAssembly;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Resolves the type that corresponds to the specified UVML name.
        /// </summary>
        private static Type ResolveTypeFromName(UltravioletContext uv,
                                                Type templatedParentType, Type templatedObjectType, String name)
        {
            if (String.Equals(name, "View", StringComparison.Ordinal))
            {
                return(templatedObjectType);
            }

            var knownType = default(Type);

            var placeholderAttr = templatedParentType?.GetCustomAttributes(typeof(UvmlPlaceholderAttribute), true).Cast <UvmlPlaceholderAttribute>()
                                  .Where(x => String.Equals(name, x.Placeholder, StringComparison.Ordinal)).SingleOrDefault();

            if (placeholderAttr != null)
            {
                knownType = placeholderAttr.Type;
            }
            else
            {
                if (!uv.GetUI().GetPresentationFoundation().GetKnownType(name, out knownType))
                {
                    throw new UvmlException(PresentationStrings.UnrecognizedType.Format(name));
                }
            }

            if (!templatedObjectType.IsAssignableFrom(knownType))
            {
                throw new UvmlException(PresentationStrings.TemplateTypeMismatch.Format(templatedObjectType.Name, knownType.Name));
            }

            return(knownType);
        }
Exemplo n.º 5
0
        /// <inheritdoc/>
        public override void Initialize(UltravioletContext uv, Object configuration)
        {
            var config = (PresentationFoundationConfiguration)configuration ?? new PresentationFoundationConfiguration();

            var upf = uv.GetUI().GetPresentationFoundation();

            upf.BindingExpressionCompilerAssemblyName = config.BindingExpressionCompilerAssembly;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Control"/> class.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        /// <param name="name">The element's identifying name within its namescope.</param>
        public Control(UltravioletContext uv, String name)
            : base(uv, name)
        {
            var upf = uv.GetUI().GetPresentationFoundation();
            dataSourceWrapper = upf.CreateDataSourceWrapperForControl(this);

            LoadComponentRoot();
        }
Exemplo n.º 7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Control"/> class.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        /// <param name="name">The element's identifying name within its namescope.</param>
        public Control(UltravioletContext uv, String name)
            : base(uv, name)
        {
            var upf = uv.GetUI().GetPresentationFoundation();

            dataSourceWrapper = upf.CreateDataSourceWrapperForControl(this);

            LoadComponentRoot();
        }
Exemplo n.º 8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ExpressionCompilerState"/> class.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        /// <param name="compiler">The compiler used to produce view model code.</param>
        public ExpressionCompilerState(UltravioletContext uv, CSharpCodeProvider compiler)
        {
            this.uv = uv;
            this.compiler = compiler;
            this.knownTypes = new Dictionary<String, Type>();
            this.knownDefaultProperties = new Dictionary<Type, String>();
            this.componentTemplateManager = (uv == null) ? null : uv.GetUI().GetPresentationFoundation().ComponentTemplates;

            LoadKnownTypes(uv);
        }
Exemplo n.º 9
0
        public ScreenManager(UltravioletContext ultraviolet)
        {
            Screens = new Stack <List <Screen> >();
            SetupScreens();

            _window = ultraviolet.GetUI().GetScreens().Window;
            ultraviolet.WindowDrawn += Ultraviolet_WindowDrawn;

            _previousWindowSize = WindowSize;
        }
Exemplo n.º 10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ExpressionCompilerState"/> class.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        /// <param name="compiler">The compiler used to produce view model code.</param>
        public ExpressionCompilerState(UltravioletContext uv, CSharpCodeProvider compiler)
        {
            this.uv                       = uv;
            this.compiler                 = compiler;
            this.knownTypes               = new Dictionary <String, Type>();
            this.knownDefaultProperties   = new Dictionary <Type, String>();
            this.componentTemplateManager = (uv == null) ? null : uv.GetUI().GetPresentationFoundation().ComponentTemplates;

            LoadKnownTypes(uv);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Retrieves the registered element type with the specified name.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        /// <param name="name">The name of the type to retrieve.</param>
        /// <returns>The registered element type with the specified name.</returns>
        private static Type ResolveKnownType(UltravioletContext uv, String name)
        {
            Type type;

            if (!uv.GetUI().GetPresentationFoundation().GetKnownType(name, false, out type))
            {
                throw new InvalidOperationException(PresentationStrings.UnrecognizedType.Format(name));
            }

            return(type);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Finds the dependency property with the specified styling name.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        /// <param name="dobj">The dependency object which is searching for a dependency property.</param>
        /// <param name="owner">The name of the dependency property's containing type.</param>
        /// <param name="name">The styling name of the dependency property.</param>
        /// <returns>A <see cref="DependencyProperty"/> instance which represents the specified dependency property,
        /// or <see langword="null"/> if no such dependency property exists.</returns>
        public static DependencyProperty FindByStylingName(UltravioletContext uv, DependencyObject dobj, String owner, String name)
        {
            Contract.Require(uv, nameof(uv));
            Contract.Require(dobj, nameof(dobj));
            Contract.RequireNotEmpty(name, nameof(name));

            var type = String.IsNullOrEmpty(owner) ? dobj.GetType() : null;

            if (type == null)
            {
                if (!uv.GetUI().GetPresentationFoundation().GetKnownType(owner, false, out type))
                {
                    throw new InvalidOperationException(PresentationStrings.UnrecognizedType.Format(owner));
                }
            }

            return(FindByStylingName(name, type));
        }
Exemplo n.º 13
0
        /// <summary>
        /// Performs initialization required by instances of the <see cref="ContentPresenter"/> class.
        /// </summary>
        private void InitializeContentPresenter(UltravioletContext uv, Object instance, UvmlInstantiationContext context)
        {
            var contentPresenter = instance as ContentPresenter;

            if (contentPresenter == null)
            {
                return;
            }

            if (contentPresenter.HasDefinedValue(ContentPresenter.ContentProperty) || contentPresenter.TemplatedParent == null)
            {
                return;
            }

            var alias = contentPresenter.ContentSource ?? "Content";

            if (alias == String.Empty)
            {
                return;
            }

            var templateType        = contentPresenter.TemplatedParent.GetType();
            var templateWrapperName = PresentationFoundationView.GetDataSourceWrapperNameForComponentTemplate(templateType);
            var templateWrapperType = uv.GetUI().GetPresentationFoundation().GetDataSourceWrapperTypeByName(templateWrapperName);

            var dpAliasedContent = DependencyProperty.FindByName(alias, templateType);

            if (dpAliasedContent != null)
            {
                contentPresenter.BindValue(ContentPresenter.ContentProperty, templateWrapperType,
                                           "{{" + dpAliasedContent.Name + "}}");
            }

            if (!contentPresenter.HasDefinedValue(ContentPresenter.ContentStringFormatProperty))
            {
                var dpAliasedContentStringFormat = DependencyProperty.FindByName(alias + "StringFormat", templateType);
                if (dpAliasedContentStringFormat != null)
                {
                    contentPresenter.BindValue(ContentPresenter.ContentStringFormatProperty, templateWrapperType,
                                               "{{" + dpAliasedContentStringFormat.Name + "}}");
                }
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// Loads the set of known types which are available to the compiler.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        private void LoadKnownTypes(UltravioletContext uv)
        {
            if (uv != null)
            {
                var upf = uv.GetUI().GetPresentationFoundation();
                foreach (var kvp in upf.GetKnownTypes())
                {
                    knownTypes[kvp.Key] = kvp.Value;
                }

                foreach (var kvp in knownTypes)
                {
                    String property;
                    if (upf.GetElementDefaultProperty(kvp.Value, out property))
                    {
                        knownDefaultProperties[kvp.Value] = property;
                    }
                }
            }
            else
            {
                var types = from type in typeof(PresentationFoundation).Assembly.GetTypes()
                            let attrKnownType = type.GetCustomAttributes(typeof(UvmlKnownTypeAttribute), false).Cast <UvmlKnownTypeAttribute>().SingleOrDefault()
                                                let attrDefaultProp = type.GetCustomAttributes(typeof(DefaultPropertyAttribute), true).Cast <DefaultPropertyAttribute>().SingleOrDefault()
                                                                      where attrKnownType != null
                                                                      select new { Type = type, AttrKnownType = attrKnownType, AttrDefaultProp = attrDefaultProp };

                foreach (var type in types)
                {
                    var name = type.AttrKnownType.Name ?? type.Type.Name;
                    knownTypes[name] = type.Type;

                    System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(type.Type.TypeHandle);

                    if (type.AttrDefaultProp != null)
                    {
                        knownDefaultProperties[type.Type] = type.AttrDefaultProp.Name;
                    }
                }
            }
        }
Exemplo n.º 15
0
        /// <summary>
        /// Loads a new instance of the <see cref="DataTemplate"/> class from the specified UVML element.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        /// <param name="root">The root element of the UVML template to load.</param>
        /// <param name="cultureInfo">The <see cref="CultureInfo"/> to use when parsing values.</param>
        /// <returns>The <see cref="DataTemplate"/> instance that was created from the specified UVML element.</returns>
        public static DataTemplate FromUvml(UltravioletContext uv, XElement root, CultureInfo cultureInfo = null)
        {
            Contract.Require(uv, nameof(uv));
            Contract.Require(root, nameof(root));

            var dataSourceAnnotation = root.Parent?.Annotation<FrameworkTemplateNameAnnotation>();
            var dataSourceWrapperName = dataSourceAnnotation?.Name;

            var templatedParentType = default(Type);

            var templatedObjectName = root.Name.LocalName;
            var templatedObjectType = default(Type);

            if (!uv.GetUI().GetPresentationFoundation().GetKnownType(templatedObjectName, out templatedObjectType))
                throw new UvmlException(PresentationStrings.UnrecognizedType.Format(templatedObjectName));

            var template = UvmlLoader.CreateTemplateFromXml(uv, root, templatedParentType, templatedObjectType, cultureInfo);
            var instance = new DataTemplate(template, dataSourceWrapperName);

            return instance;
        }
Exemplo n.º 16
0
        /// <summary>
        /// Loads a new instance of the <see cref="DataTemplate"/> class from the specified UVML element.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        /// <param name="root">The root element of the UVML template to load.</param>
        /// <param name="cultureInfo">The <see cref="CultureInfo"/> to use when parsing values.</param>
        /// <returns>The <see cref="DataTemplate"/> instance that was created from the specified UVML element.</returns>
        public static DataTemplate FromUvml(UltravioletContext uv, XElement root, CultureInfo cultureInfo = null)
        {
            Contract.Require(uv, nameof(uv));
            Contract.Require(root, nameof(root));

            var dataSourceAnnotation  = root.Parent?.Annotation <FrameworkTemplateNameAnnotation>();
            var dataSourceWrapperName = dataSourceAnnotation?.Name;

            var templatedParentType = default(Type);

            var templatedObjectName = root.Name.LocalName;
            var templatedObjectType = default(Type);

            if (!uv.GetUI().GetPresentationFoundation().GetKnownType(templatedObjectName, out templatedObjectType))
            {
                throw new UvmlException(PresentationStrings.UnrecognizedType.Format(templatedObjectName));
            }

            var template = UvmlLoader.CreateTemplateFromXml(uv, root, templatedParentType, templatedObjectType, cultureInfo);
            var instance = new DataTemplate(template, dataSourceWrapperName);

            return(instance);
        }
Exemplo n.º 17
0
        /// <summary>
        /// Creates a new mutator from the specified XML element.
        /// </summary>
        private static UvmlMutator CreateMutatorForXmlElement(UltravioletContext uv, 
            XElement parent, XElement element, Type templatedParentType, Type templatedObjectType, CultureInfo cultureInfo)
        {
            var name = element.Name.LocalName;
            var value = default(UvmlNode);

            var target = default(Object);
            var targetType = default(Type);
            var targetKind = default(UvmlMutatorTarget);

            var elementChildren = element.Elements().ToList();
            if (elementChildren.Any() || element.IsEmpty)
            {
                targetKind = GetMutatorTarget(uv,
                    element.Name.LocalName, null, templatedObjectType, out target, out targetType);

                if (typeof(FrameworkTemplate).IsAssignableFrom(targetType))
                {
                    if (elementChildren.Count() > 1)
                        throw new UvmlException(PresentationStrings.InvalidChildElements.Format(name));

                    var frameworkTemplateElement = elementChildren.Single();
                    var frameworkTemplateType = default(Type);
                    if (!uv.GetUI().GetPresentationFoundation().GetKnownType(frameworkTemplateElement.Name.LocalName, out frameworkTemplateType))
                        throw new UvmlException(PresentationStrings.UnrecognizedType.Format(frameworkTemplateElement.Name.LocalName));

                    if (!targetType.IsAssignableFrom(frameworkTemplateType))
                        throw new UvmlException(PresentationStrings.IncompatibleType.Format(targetType.Name, frameworkTemplateType.Name));

                    var frameworkTemplateChildren = frameworkTemplateElement.Elements().ToList();
                    if (frameworkTemplateChildren.Count() > 1)
                        throw new UvmlException(PresentationStrings.InvalidChildElements.Format(frameworkTemplateElement.Name));

                    var frameworkTemplateContent = frameworkTemplateChildren.SingleOrDefault();
                    value = CreateFrameworkTemplateUvmlNode(uv, frameworkTemplateType, frameworkTemplateContent, cultureInfo);
                }
                else
                {
                    var itemType = default(Type);
                    if (UvmlCollectionItemMutatorBase.IsSupportedCollectionType(targetType, out itemType))
                    {
                        var items = elementChildren.Select(x => CreateTemplateFromXml(uv, x, templatedParentType, itemType, cultureInfo)).ToList();
                        return CreateMutatorForCollection(uv, targetKind, target, items);
                    }
                    else
                    {
                        if (elementChildren.Count() > 1)
                            throw new UvmlException(PresentationStrings.InvalidChildElements.Format(name));

                        value = CreateTemplateFromXml(uv, elementChildren.Single(), templatedParentType, targetType, cultureInfo);
                    }
                }
            }
            else
            {
                targetKind = GetMutatorTarget(uv,
                    element.Name.LocalName, element.Value, templatedObjectType, out target, out targetType);

                value = new UvmlLiteral(element.Value, targetType, cultureInfo);
            }

            return CreateMutatorForTarget(targetKind, templatedParentType, templatedObjectType, target, value);
        }
Exemplo n.º 18
0
        /// <summary>
        /// Determines whether the specified target is a dependency property, routed event, or standard property/event.
        /// </summary>
        private static UvmlMutatorTarget GetMutatorTarget(UltravioletContext uv, 
            String name, String value, Type type, out Object target, out Type targetType)
        {
            var upf = uv.GetUI().GetPresentationFoundation();

            // If this is an attached property/event, find the owner type.
            var depname = new DependencyName(name);
            if (depname.IsAttached)
            {
                var attachedOwnerType = default(Type);
                if (!upf.GetKnownType(depname.Owner, out attachedOwnerType))
                    throw new UvmlException(PresentationStrings.UnrecognizedType.Format(depname.Owner));

                type = attachedOwnerType;
            }

            // Is it a dependency property?
            var dprop = DependencyProperty.FindByName(depname.Name, type);
            if (dprop != null)
            {
                target = dprop;
                if (value != null && BindingExpressions.IsBindingExpression(value))
                {
                    targetType = typeof(String);
                    return UvmlMutatorTarget.DependencyPropertyBinding;
                }
                targetType = dprop.PropertyType;
                return UvmlMutatorTarget.DependencyProperty;
            }

            // Is it a routed event?
            var revt = EventManager.FindByName(depname.Name, type);
            if (revt != null)
            {
                target = revt;
                targetType = typeof(String);
                return UvmlMutatorTarget.RoutedEvent;
            }

            // Is it a standard property?
            var clrprop = type.GetProperty(depname.Name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            if (clrprop != null)
            {
                target = clrprop;
                targetType = clrprop.PropertyType;
                return UvmlMutatorTarget.StandardProperty;
            }

            // Is it a standard event?
            var clrevt = type.GetEvent(depname.Name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            if (clrevt != null)
            {
                target = clrevt;
                targetType = typeof(String);
                return UvmlMutatorTarget.StandardEvent;
            }

            throw new UvmlException(PresentationStrings.EventOrPropertyDoesNotExist.Format(depname.Name, type.Name));
        }
Exemplo n.º 19
0
        /// <summary>
        /// Performs initialization required by instances of the <see cref="ContentPresenter"/> class.
        /// </summary>
        private void InitializeContentPresenter(UltravioletContext uv, Object instance, UvmlInstantiationContext context)
        {
            var contentPresenter = instance as ContentPresenter;
            if (contentPresenter == null)
                return;

            if (contentPresenter.HasDefinedValue(ContentPresenter.ContentProperty) || contentPresenter.TemplatedParent == null)
                return;

            var alias = contentPresenter.ContentSource ?? "Content";
            if (alias == String.Empty)
                return;

            var templateType = contentPresenter.TemplatedParent.GetType();
            var templateWrapperName = PresentationFoundationView.GetDataSourceWrapperNameForComponentTemplate(templateType);
            var templateWrapperType = uv.GetUI().GetPresentationFoundation().GetDataSourceWrapperTypeByName(templateWrapperName);

            var dpAliasedContent = DependencyProperty.FindByName(alias, templateType);
            if (dpAliasedContent != null)
            {
                contentPresenter.BindValue(ContentPresenter.ContentProperty, templateWrapperType,
                    "{{" + dpAliasedContent.Name + "}}");
            }

            if (!contentPresenter.HasDefinedValue(ContentPresenter.ContentStringFormatProperty))
            {
                var dpAliasedContentStringFormat = DependencyProperty.FindByName(alias + "StringFormat", templateType);
                if (dpAliasedContentStringFormat != null)
                {
                    contentPresenter.BindValue(ContentPresenter.ContentStringFormatProperty, templateWrapperType,
                        "{{" + dpAliasedContentStringFormat.Name + "}}");
                }
            }
        }
Exemplo n.º 20
0
        /// <summary>
        /// Loads an instance of the <see cref="PresentationFoundationView"/> from an XML node.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        /// <param name="uiPanel">The <see cref="UIPanel"/> that is creating the panel.</param>
        /// <param name="uiPanelDefinition">The <see cref="UIPanelDefinition"/> that defines the view.</param>
        /// <param name="vmfactory">A view model factory which is used to create the view's initial view model, or <see langword="null"/> to skip view model creation.</param>
        /// <returns>The <see cref="PresentationFoundationView"/> that was loaded from the specified XML element.</returns>
        public static PresentationFoundationView Load(UltravioletContext uv, UIPanel uiPanel, UIPanelDefinition uiPanelDefinition, UIViewModelFactory vmfactory)
        {
            Contract.Require(uv, nameof(uv));
            Contract.Require(uiPanel, nameof(uiPanel));
            Contract.Require(uiPanelDefinition, nameof(uiPanelDefinition));

            var viewElement = uiPanelDefinition.ViewElement;
            if (viewElement == null)
                return null;

            // Determine which culture to use when parsing values.
            var cultureRequested = (String)uiPanelDefinition.RootElement.Attribute("Culture");
            var cultureInfo = CultureInfo.GetCultureInfo(cultureRequested ?? String.Empty);

            // Determine the type of view model used by this view.
            var viewModelType = default(Type);
            var viewModelTypeName = (String)viewElement.Attribute("ViewModelType");
            if (viewModelTypeName != null)
            {
                viewModelType = Type.GetType(viewModelTypeName, false);

                if (viewModelType == null)
                    throw new UvmlException(PresentationStrings.ViewModelTypeNotFound.Format(viewModelTypeName));

                var viewModelWrapperAttr = viewModelType.GetCustomAttributes(typeof(ViewModelWrapperAttribute), false)
                    .Cast<ViewModelWrapperAttribute>().SingleOrDefault();
                if (viewModelWrapperAttr != null)
                {
                    viewModelType = viewModelWrapperAttr.WrapperType;
                }
                else
                {
                    var viewModelWrapperName = PresentationFoundationView.GetDataSourceWrapperNameForView(uiPanelDefinition.AssetFilePath);
                    var viewModelWrapperType = uv.GetUI().GetPresentationFoundation().GetDataSourceWrapperTypeByName(viewModelWrapperName);
                    if (viewModelWrapperType == null)
                        throw new UvmlException(PresentationStrings.CannotFindViewModelWrapper.Format(viewModelWrapperName));

                    viewModelType = viewModelWrapperType;
                }
            }

            // Create a UVML template which will instantiate the view.
            AddUvmlAnnotations(viewModelType.Name, viewElement);
            var viewTemplate = new UvmlTemplate(viewElement, typeof(PresentationFoundationView), (puv, pname) =>
            {
                var view = new PresentationFoundationView(puv, uiPanel, viewModelType);
                var viewModel = vmfactory == null ? null : vmfactory(view);
                if (viewModel != null)
                {
                    view.SetViewModel(viewModel);
                }

                var root = view.LayoutRoot;
                root.BeginInit();

                var rootAdornerDecorator = new AdornerDecorator(puv, null);
                rootAdornerDecorator.BeginInit();
                root.Child = rootAdornerDecorator;

                var rootGridTemplate = CreateTemplateFromXml(puv, viewElement, null, typeof(Grid), cultureInfo);
                var rootGridContext = UvmlInstantiationContext.ForView(puv, view);

                var rootGridTemplateInstance = (UvmlTemplateInstance)rootGridTemplate.Instantiate(puv, rootGridContext);
                var rootGrid = (Grid)rootGridTemplateInstance.Finalize();

                rootAdornerDecorator.Child = rootGrid;

                return view;
            });

            // Instantiate the view template.
            var viewTemplateInstance = (UvmlTemplateInstance)viewTemplate.Instantiate(uv, null);
            var viewInstance = (PresentationFoundationView)viewTemplateInstance.Finalize();

            var viewRoot = viewInstance.LayoutRoot;
            var viewRootAdornerDecorator = (AdornerDecorator)viewRoot.Child;
            viewRootAdornerDecorator.EndInit();
            viewRoot.EndInit();
            viewRoot.CacheLayoutParameters();

            var viewInstanceViewModel = viewInstance.ViewModel;
            if (viewInstanceViewModel != null)
                viewInstance.Namescope.PopulateFieldsFromRegisteredElements(viewInstanceViewModel);

            return viewInstance;
        }
Exemplo n.º 21
0
        /// <summary>
        /// Determines whether the specified target is a dependency property, routed event, or standard property/event.
        /// </summary>
        private static UvmlMutatorTarget GetMutatorTarget(UltravioletContext uv,
                                                          String name, String value, Type type, out Object target, out Type targetType)
        {
            var upf = uv.GetUI().GetPresentationFoundation();

            // If this is an attached property/event, find the owner type.
            var depname = new DependencyName(name);

            if (depname.IsAttached)
            {
                var attachedOwnerType = default(Type);
                if (!upf.GetKnownType(depname.Owner, out attachedOwnerType))
                {
                    throw new UvmlException(PresentationStrings.UnrecognizedType.Format(depname.Owner));
                }

                type = attachedOwnerType;
            }

            // Is it a dependency property?
            var dprop = DependencyProperty.FindByName(depname.Name, type);

            if (dprop != null)
            {
                target = dprop;
                if (value != null && BindingExpressions.IsBindingExpression(value))
                {
                    targetType = typeof(String);
                    return(UvmlMutatorTarget.DependencyPropertyBinding);
                }
                targetType = dprop.PropertyType;
                return(UvmlMutatorTarget.DependencyProperty);
            }

            // Is it a routed event?
            var revt = EventManager.FindByName(depname.Name, type);

            if (revt != null)
            {
                target     = revt;
                targetType = typeof(String);
                return(UvmlMutatorTarget.RoutedEvent);
            }

            // Is it a standard property?
            var clrprop = type.GetProperty(depname.Name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

            if (clrprop != null)
            {
                target     = clrprop;
                targetType = clrprop.PropertyType;
                return(UvmlMutatorTarget.StandardProperty);
            }

            // Is it a standard event?
            var clrevt = type.GetEvent(depname.Name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

            if (clrevt != null)
            {
                target     = clrevt;
                targetType = typeof(String);
                return(UvmlMutatorTarget.StandardEvent);
            }

            throw new UvmlException(PresentationStrings.EventOrPropertyDoesNotExist.Format(depname.Name, type.Name));
        }
Exemplo n.º 22
0
        /// <summary>
        /// Loads an instance of the <see cref="PresentationFoundationView"/> from an XML node.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        /// <param name="uiPanel">The <see cref="UIPanel"/> that is creating the panel.</param>
        /// <param name="uiPanelDefinition">The <see cref="UIPanelDefinition"/> that defines the view.</param>
        /// <param name="vmfactory">A view model factory which is used to create the view's initial view model, or <see langword="null"/> to skip view model creation.</param>
        /// <returns>The <see cref="PresentationFoundationView"/> that was loaded from the specified XML element.</returns>
        public static PresentationFoundationView Load(UltravioletContext uv, UIPanel uiPanel, UIPanelDefinition uiPanelDefinition, UIViewModelFactory vmfactory)
        {
            Contract.Require(uv, nameof(uv));
            Contract.Require(uiPanel, nameof(uiPanel));
            Contract.Require(uiPanelDefinition, nameof(uiPanelDefinition));

            var viewElement = uiPanelDefinition.ViewElement;

            if (viewElement == null)
            {
                return(null);
            }

            // Determine which culture to use when parsing values.
            var cultureRequested = (String)uiPanelDefinition.RootElement.Attribute("Culture");
            var cultureInfo      = CultureInfo.GetCultureInfo(cultureRequested ?? String.Empty);

            // Determine the type of view model used by this view.
            var viewModelType     = default(Type);
            var viewModelTypeName = (String)viewElement.Attribute("ViewModelType");

            if (viewModelTypeName != null)
            {
                viewModelType = Type.GetType(viewModelTypeName, false);

                if (viewModelType == null)
                {
                    throw new UvmlException(PresentationStrings.ViewModelTypeNotFound.Format(viewModelTypeName));
                }

                var viewModelWrapperAttr = viewModelType.GetCustomAttributes(typeof(ViewModelWrapperAttribute), false)
                                           .Cast <ViewModelWrapperAttribute>().SingleOrDefault();
                if (viewModelWrapperAttr != null)
                {
                    viewModelType = viewModelWrapperAttr.WrapperType;
                }
                else
                {
                    var viewModelWrapperName = PresentationFoundationView.GetDataSourceWrapperNameForView(uiPanelDefinition.AssetFilePath);
                    var viewModelWrapperType = uv.GetUI().GetPresentationFoundation().GetDataSourceWrapperTypeByName(viewModelWrapperName);
                    if (viewModelWrapperType == null)
                    {
                        throw new UvmlException(PresentationStrings.CannotFindViewModelWrapper.Format(viewModelWrapperName));
                    }

                    viewModelType = viewModelWrapperType;
                }
            }

            // Create a UVML template which will instantiate the view.
            var viewTemplate = new UvmlTemplate(viewElement, typeof(PresentationFoundationView), (puv, pname) =>
            {
                var view      = new PresentationFoundationView(puv, uiPanel, viewModelType);
                var viewModel = vmfactory == null ? null : vmfactory(view);
                if (viewModel != null)
                {
                    view.SetViewModel(viewModel);
                }

                var root = view.LayoutRoot;
                root.BeginInit();

                var rootAdornerDecorator = new AdornerDecorator(puv, null);
                rootAdornerDecorator.BeginInit();
                root.Child = rootAdornerDecorator;

                var rootGridTemplate = CreateTemplateFromXml(puv, viewElement, null, typeof(Grid), cultureInfo);
                var rootGridContext  = UvmlInstantiationContext.ForView(puv, view);

                var rootGridTemplateInstance = (UvmlTemplateInstance)rootGridTemplate.Instantiate(puv, rootGridContext);
                var rootGrid = (Grid)rootGridTemplateInstance.Finalize();

                rootAdornerDecorator.Child = rootGrid;

                return(view);
            });

            // Instantiate the view template.
            var viewTemplateInstance = (UvmlTemplateInstance)viewTemplate.Instantiate(uv, null);
            var viewInstance         = (PresentationFoundationView)viewTemplateInstance.Finalize();

            var viewRoot = viewInstance.LayoutRoot;
            var viewRootAdornerDecorator = (AdornerDecorator)viewRoot.Child;

            viewRootAdornerDecorator.EndInit();
            viewRoot.EndInit();
            viewRoot.CacheLayoutParameters();

            var viewInstanceViewModel = viewInstance.ViewModel;

            if (viewInstanceViewModel != null)
            {
                viewInstance.Namescope.PopulateFieldsFromRegisteredElements(viewInstanceViewModel);
            }

            return(viewInstance);
        }
 /// <summary>
 /// Resolves the specified dependency object type name into a <see cref="Type"/> object.
 /// </summary>
 /// <param name="uv">The Ultraviolet context.</param>
 /// <param name="name">The dependency object type name to resolve.</param>
 /// <returns>The resolved <see cref="Type"/> object.</returns>
 private static Type ResolveKnownType(UltravioletContext uv, String name)
 {
     Type type;
     if (!uv.GetUI().GetPresentationFoundation().GetKnownType(name, false, out type))
     {
         throw new InvalidOperationException(PresentationStrings.UnrecognizedType.Format(name));
     }
     return type;
 }
Exemplo n.º 24
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SimpleClockPool"/> class.
 /// </summary>
 /// <param name="uv">The Ultraviolet context.</param>
 private SimpleClockPool(UltravioletContext uv)
     : base(uv)
 {
     uv.GetUI().Updating += SimpleClockPool_Updating;
 }
Exemplo n.º 25
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StoryboardClockPool"/> class.
 /// </summary>
 /// <param name="uv">The Ultraviolet context.</param>
 private StoryboardClockPool(UltravioletContext uv)
     : base(uv)
 {
     uv.GetUI().Updating += StoryboardClockPool_Updating;
 }
Exemplo n.º 26
0
        /// <summary>
        /// Creates a new mutator from the specified XML element.
        /// </summary>
        private static UvmlMutator CreateMutatorForXmlElement(UltravioletContext uv,
                                                              XElement parent, XElement element, Type templatedParentType, Type templatedObjectType, CultureInfo cultureInfo)
        {
            var name  = element.Name.LocalName;
            var value = default(UvmlNode);

            var target     = default(Object);
            var targetType = default(Type);
            var targetKind = default(UvmlMutatorTarget);

            var elementChildren = element.Elements().ToList();

            if (elementChildren.Any() || element.IsEmpty)
            {
                targetKind = GetMutatorTarget(uv,
                                              element.Name.LocalName, null, templatedObjectType, out target, out targetType);

                if (typeof(FrameworkTemplate).IsAssignableFrom(targetType))
                {
                    if (elementChildren.Count() > 1)
                    {
                        throw new UvmlException(PresentationStrings.InvalidChildElements.Format(name));
                    }

                    var frameworkTemplateElement = elementChildren.Single();
                    var frameworkTemplateType    = default(Type);
                    if (!uv.GetUI().GetPresentationFoundation().GetKnownType(frameworkTemplateElement.Name.LocalName, out frameworkTemplateType))
                    {
                        throw new UvmlException(PresentationStrings.UnrecognizedType.Format(frameworkTemplateElement.Name.LocalName));
                    }

                    if (!targetType.IsAssignableFrom(frameworkTemplateType))
                    {
                        throw new UvmlException(PresentationStrings.IncompatibleType.Format(targetType.Name, frameworkTemplateType.Name));
                    }

                    var frameworkTemplateChildren = frameworkTemplateElement.Elements().ToList();
                    if (frameworkTemplateChildren.Count() > 1)
                    {
                        throw new UvmlException(PresentationStrings.InvalidChildElements.Format(frameworkTemplateElement.Name));
                    }

                    var frameworkTemplateContent = frameworkTemplateChildren.SingleOrDefault();
                    value = CreateFrameworkTemplateUvmlNode(uv, frameworkTemplateType, frameworkTemplateContent, cultureInfo);
                }
                else
                {
                    var itemType = default(Type);
                    if (UvmlCollectionItemMutatorBase.IsSupportedCollectionType(targetType, out itemType))
                    {
                        var items = elementChildren.Select(x => CreateTemplateFromXml(uv, x, templatedParentType, itemType, cultureInfo)).ToList();
                        return(CreateMutatorForCollection(uv, targetKind, target, items));
                    }
                    else
                    {
                        if (elementChildren.Count() > 1)
                        {
                            throw new UvmlException(PresentationStrings.InvalidChildElements.Format(name));
                        }

                        value = CreateTemplateFromXml(uv, elementChildren.Single(), templatedParentType, targetType, cultureInfo);
                    }
                }
            }
            else
            {
                targetKind = GetMutatorTarget(uv,
                                              element.Name.LocalName, element.Value, templatedObjectType, out target, out targetType);

                value = new UvmlLiteral(element.Value, targetType, cultureInfo);
            }

            return(CreateMutatorForTarget(targetKind, templatedParentType, templatedObjectType, target, value));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="SimpleClockPool"/> class.
 /// </summary>
 /// <param name="uv">The Ultraviolet context.</param>
 private SimpleClockPool(UltravioletContext uv)
     : base(uv)
 {
     uv.GetUI().Updating += SimpleClockPool_Updating;
 }
Exemplo n.º 28
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StoryboardClockPool"/> class.
 /// </summary>
 /// <param name="uv">The Ultraviolet context.</param>
 private StoryboardClockPool(UltravioletContext uv)
     : base(uv)
 {
     uv.GetUI().Updating += StoryboardClockPool_Updating;
 }
Exemplo n.º 29
0
        /// <summary>
        /// Resolves the type that corresponds to the specified UVML name.
        /// </summary>
        private static Type ResolveTypeFromName(UltravioletContext uv, 
            Type templatedParentType, Type templatedObjectType, String name)
        {
            if (String.Equals(name, "View", StringComparison.Ordinal))
                return templatedObjectType;

            var knownType = default(Type);

            var placeholderAttr = templatedParentType?.GetCustomAttributes(typeof(UvmlPlaceholderAttribute), true).Cast<UvmlPlaceholderAttribute>()
                .Where(x => String.Equals(name, x.Placeholder, StringComparison.Ordinal)).SingleOrDefault();
            if (placeholderAttr != null)
            {
                knownType = placeholderAttr.Type;
            }
            else
            {
                if (!uv.GetUI().GetPresentationFoundation().GetKnownType(name, out knownType))
                    throw new UvmlException(PresentationStrings.UnrecognizedType.Format(name));
            }

            if (!templatedObjectType.IsAssignableFrom(knownType))
                throw new UvmlException(PresentationStrings.TemplateTypeMismatch.Format(templatedObjectType.Name, knownType.Name));

            return knownType;
        }
Exemplo n.º 30
0
        /// <summary>
        /// Finds the dependency property with the specified styling name.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        /// <param name="dobj">The dependency object which is searching for a dependency property.</param>
        /// <param name="owner">The name of the dependency property's containing type.</param>
        /// <param name="name">The styling name of the dependency property.</param>
        /// <returns>A <see cref="DependencyProperty"/> instance which represents the specified dependency property, 
        /// or <c>null</c> if no such dependency property exists.</returns>
        public static DependencyProperty FindByStylingName(UltravioletContext uv, DependencyObject dobj, String owner, String name)
        {
            Contract.Require(uv, "uv");
            Contract.Require(dobj, "dobj");
            Contract.RequireNotEmpty(name, "name");

            var type = String.IsNullOrEmpty(owner) ? dobj.GetType() : null;
            if (type == null)
            {
                if (!uv.GetUI().GetPresentationFoundation().GetKnownType(owner, false, out type))
                    throw new InvalidOperationException(PresentationStrings.UnrecognizedType.Format(owner));
            }

            return FindByStylingName(name, type);
        }
Exemplo n.º 31
0
        /// <summary>
        /// Loads the set of known types which are available to the compiler.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        private void LoadKnownTypes(UltravioletContext uv)
        {
            if (uv != null)
            {
                var upf = uv.GetUI().GetPresentationFoundation();
                foreach (var kvp in upf.GetKnownTypes())
                    knownTypes[kvp.Key] = kvp.Value;

                foreach (var kvp in knownTypes)
                {
                    String property;
                    if (upf.GetElementDefaultProperty(kvp.Value, out property))
                        knownDefaultProperties[kvp.Value] = property;
                }
            }
            else
            {
                var types = from type in typeof(PresentationFoundation).Assembly.GetTypes()
                            let attrKnownType = type.GetCustomAttributes(typeof(UvmlKnownTypeAttribute), false).Cast<UvmlKnownTypeAttribute>().SingleOrDefault()
                            let attrDefaultProp = type.GetCustomAttributes(typeof(DefaultPropertyAttribute), true).Cast<DefaultPropertyAttribute>().SingleOrDefault()
                            where attrKnownType != null
                            select new { Type = type, AttrKnownType = attrKnownType, AttrDefaultProp = attrDefaultProp };

                foreach (var type in types)
                {
                    var name = type.AttrKnownType.Name ?? type.Type.Name;
                    knownTypes[name] = type.Type;

                    System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(type.Type.TypeHandle);

                    if (type.AttrDefaultProp != null)
                        knownDefaultProperties[type.Type] = type.AttrDefaultProp.Name;
                }
            }
        }