コード例 #1
0
ファイル: TestUtils.cs プロジェクト: nebosite/MonoVarmint
        public static VarmintWidget LoadFromText(IVarmintWidgetInjector injector, string vwml, string defaultName)
        {
            var widgetSpace = new VarmintWidgetSpace(injector);

            widgetSpace.AddContent(defaultName, vwml);
            return(widgetSpace.FindWidgetByName(defaultName));
        }
コード例 #2
0
 public static VarmintWidget LoadFromText(IVarmintWidgetInjector injector, string vwml, string defaultName)
 {
     using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(vwml)))
     {
         var layout = VarmintWidget.PreloadFromVwml(memoryStream, defaultName);
         return(VarmintWidget.HydrateLayout(injector, layout, new Dictionary <string, VarmintWidget.LayoutItem>()));
     }
 }
コード例 #3
0
        //--------------------------------------------------------------------------------------
        /// <summary>
        /// ctor
        /// </summary>
        //--------------------------------------------------------------------------------------
        public VarmintWidgetSpace(IVarmintWidgetInjector injector, object bindingContext = null)
        {
            _injector = injector;
            var assembly = injector.GetType().GetTypeInfo().Assembly;

            VarmintWidget.DeclareAssembly(assembly);

            // automatically add embedded resources
            foreach (var resourceName in assembly.GetManifestResourceNames())
            {
                if (resourceName.ToLower().EndsWith(".vwml"))
                {
                    System.Diagnostics.Debug.WriteLine("---- " + resourceName);

                    var nameParts   = resourceName.Split('.');
                    var defaultName = nameParts[nameParts.Length - 2];
                    AddContent(defaultName, assembly.GetManifestResourceStream(resourceName), bindingContext);
                }
            }
        }
コード例 #4
0
        //--------------------------------------------------------------------------------------
        /// <summary>
        /// ctor
        /// </summary>
        //--------------------------------------------------------------------------------------
        public VarmintWidgetSpace(IVarmintWidgetInjector injector)
        {
            _injector = injector;
            var assembly = injector.GetType().GetTypeInfo().Assembly;

            VarmintWidget.DeclareAssembly(assembly);

            // automatically add embedded layout by preloading the raw layout without hydrating it
            foreach (var resourceName in assembly.GetManifestResourceNames())
            {
                if (resourceName.ToLower().EndsWith(".vwml"))
                {
                    Debug.WriteLine("---- Injesting " + resourceName);

                    var nameParts   = resourceName.Split('.');
                    var defaultName = nameParts[nameParts.Length - 2];
                    AddContent(defaultName, assembly.GetManifestResourceStream(resourceName));
                }
            }
        }
コード例 #5
0
        //--------------------------------------------------------------------------------------
        /// <summary>
        /// HydrateLayout - Take a layout item and turn it into a VarmintWidget
        /// </summary>
        //--------------------------------------------------------------------------------------
        public static VarmintWidget HydrateLayout(IVarmintWidgetInjector injector, LayoutItem widgetLayout, Dictionary <string, LayoutItem> controlLibrary)
        {
            VarmintWidget output = null;

            Action <LayoutItem> applyLayout = (layout) =>
            {
                foreach (var name in layout.Settings.Keys)
                {
                    switch (name)
                    {
                    case "Style":
                    case "ApplyTo":
                        output.SetValue(name, layout.Settings[name], true);
                        break;

                    default:
                        output.AddSetting(name, layout.Settings[name]);
                        break;
                    }
                }
                foreach (var childItem in layout.Children)
                {
                    if (childItem.VwmlTag.Contains("."))
                    {
                        var parts = childItem.VwmlTag.Split('.');
                        if (parts.Length > 2)
                        {
                            throw new ApplicationException("Property setter specification is too deep.  Only one dot allowed! (" + childItem.VwmlTag + ")");
                        }

                        var propertyType = output.GetType().GetProperty(parts[1]);
                        if (childItem.Children.Count == 1) // Only add items with content
                        {
                            var hydratedLayout = HydrateLayout(injector, childItem.Children[0], controlLibrary);
                            if (!propertyType.PropertyType.IsAssignableFrom(hydratedLayout.GetType()))
                            {
                                throw new ApplicationException("Property " + childItem.VwmlTag + " cannot be assigned Type " + hydratedLayout.GetType().Name);
                            }
                            propertyType.SetValue(output, hydratedLayout);
                        }
                        else if (childItem.Children.Count > 1)
                        {
                            throw new ApplicationException("Too many child nodes on a property setter.  You only get one! (" + childItem.VwmlTag + ")");
                        }
                    }
                    else
                    {
                        output.AddChild(HydrateLayout(injector, childItem, controlLibrary), true);
                    }
                }
            };


            if (controlLibrary.ContainsKey(widgetLayout.VwmlTag))
            {
                var controlLayout = controlLibrary[widgetLayout.VwmlTag];

                if (controlLayout.Settings.ContainsKey("Class"))
                {
                    var controlType = GetWidgetType(controlLayout.Settings["Class"]);
                    if (!controlType.IsSubclassOf(typeof(VarmintWidgetControl)))
                    {
                        throw new ApplicationException("The Class attribute must point to a VarmintWidgetControl");
                    }
                    output = (VarmintWidget)Activator.CreateInstance(controlType);
                    output.EventBindingContext = output;
                }
                else
                {
                    output = new VarmintWidgetControl();
                }

                // Controls will get properties from the control layout, but these can
                // be overridden later by the local instace of the control
                applyLayout(controlLayout);
            }
            else
            {
                var nodeType = GetWidgetType(widgetLayout.VwmlTag);
                output = (VarmintWidget)Activator.CreateInstance(nodeType);
            }
            if (widgetLayout.Name != null)
            {
                output.Name = widgetLayout.Name;
            }

            foreach (var propertyType in output.GetType().GetProperties())
            {
                var injectAttribute = (VarmintWidgetInjectAttribute)propertyType.GetCustomAttribute(typeof(VarmintWidgetInjectAttribute));
                if (injectAttribute != null)
                {
                    propertyType.SetValue(output,
                                          injector.GetInjectedValue(injectAttribute, propertyType));
                }
            }

            applyLayout(widgetLayout);

            return(output);
        }