//Slight tweak on the above method to make it async and allow use with a serviceProvider instead of a host, and with a type argument instead of generic argument so it can be called at run time
        //The above methos only needs host for it's services so it could be refactored.
        //The above method only needs <t> so it can be used as a type in an overload of the method it calls.
        //This version also allows an optional set of parameters
        //The only downside is you can't have design/compiletime type safety
        //There's a lot of duplicate code between the two, can probably refactor the core of the method into a separate method that they both call
        public static async Task <IComponent> AddComponent(this IServiceProvider services, XF.Element parent, Type type, System.Collections.Generic.Dictionary <string, object> parameters = null)
        {
            if (services is null)
            {
                throw new ArgumentNullException(nameof(services));
            }
            if (parent is null)
            {
                throw new ArgumentNullException(nameof(parent));
            }
            if (type is null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (!typeof(IComponent).IsAssignableFrom(type))
            {
                throw new InvalidOperationException($"Cannot add {type.Name} to {parent.GetType().Name}. {type.Name} is not an IComponent. If you are trying to add a Xamarin.Forms type, try adding the Mobile Blazor Bindings equivalent instead.");
            }

#pragma warning disable CA2000 // Dispose objects before losing scope
            var renderer = new MobileBlazorBindingsRenderer(services, services.GetRequiredService <ILoggerFactory>());
#pragma warning restore CA2000 // Dispose objects before losing scope

            return(await renderer.AddComponent(type, CreateHandler(parent, renderer), parameters).ConfigureAwait(false));
        }
Ejemplo n.º 2
0
        void IXamarinFormsContainerElementHandler.AddChild(XF.Element child, int physicalSiblingIndex)
        {
            if (!(child is TItemType typedChild))
            {
                throw new NotSupportedException($"Cannot add item of type {child?.GetType().Name} to a {typeof(TItemType)} collection.");
            }

            _propertyItems.Insert(physicalSiblingIndex, typedChild);
        }
Ejemplo n.º 3
0
 protected sealed override void OnAddChild(VisualNode widget, Xamarin.Forms.Element nativeControl)
 {
     if (nativeControl is View view)
     {
         _itemTemplatePreseter.Content = view;
     }
     else
     {
         throw new InvalidOperationException($"Type '{nativeControl.GetType()}' not supported under '{GetType()}'");
     }
 }
Ejemplo n.º 4
0
        protected override void OnAddChild(VisualNode widget, Xamarin.Forms.Element childControl)
        {
            if (childControl is View view)
            {
                NativeControl.Content = view;
            }
            else
            {
                throw new InvalidOperationException($"Type '{childControl.GetType()}' not supported under '{GetType()}'");
            }

            base.OnAddChild(widget, childControl);
        }
        protected override void OnAddChild(VisualNode widget, Xamarin.Forms.Element childControl)
        {
            if (childControl is View view)
            {
                //System.Diagnostics.Debug.WriteLine($"StackLayout ({Key ?? GetType()}) inserting {widget.Key ?? widget.GetType()} at index {widget.ChildIndex}");
                NativeControl.Children.Insert(widget.ChildIndex, view);
            }
            else
            {
                throw new InvalidOperationException($"Type '{childControl.GetType()}' not supported under '{GetType()}'");
            }

            base.OnAddChild(widget, childControl);
        }
Ejemplo n.º 6
0
        public static IEnumerable <Xamarin.Forms.Element> GetChildren(this Xamarin.Forms.Element element)
        {
            if (element == null)
            {
                yield break;
            }

            var typ = element.GetType();

            if (typeof(Cell).IsAssignableFrom(typ))
            {
                var cell = element as Cell;
                if (typeof(ViewCell).IsAssignableFrom(typ))
                {
                    yield return((element as ViewCell).View);
                }
            }
            else if (typeof(ContentPage).IsAssignableFrom(typ))
            {
                var page = element as ContentPage;
                yield return(page.Content);
            }
            else if (typeof(Layout <View>).IsAssignableFrom(typ))
            {
                var layout = element as Layout <View>;
                foreach (var child in layout.Children)
                {
                    yield return(child);
                }
            }
            else if (typeof(Layout).IsAssignableFrom(typ))
            {
                var layout = element as Layout;
                foreach (var child in layout.Children)
                {
                    yield return(child);
                }
            }
            else if (typeof(ITemplatedItemsView <Cell>).IsAssignableFrom(typ))
            {
                var tiv = element as ITemplatedItemsView <Cell>;
                foreach (var cell in tiv.TemplatedItems)
                {
                    yield return(cell);
                }
            }
            yield break;
        }
        public static IEnumerable <T> GetChildrenOfType <T>(this Element element) where T : Element
        {
            var properties = element.GetType().GetRuntimeProperties();

            var contentProperty = properties.FirstOrDefault(w => w.Name == "Content");

            if (contentProperty != null)
            {
                if (contentProperty.GetValue(element) is Element content)
                {
                    if (content is T)
                    {
                        yield return(content as T);
                    }

                    foreach (var child in content.GetChildrenOfType <T>())
                    {
                        yield return(child);
                    }
                }
            }
            else
            {
                var childrenProperty = properties.FirstOrDefault(w => w.Name == "Children");

                if (childrenProperty != null)
                {
                    IEnumerable children = childrenProperty.GetValue(element) as IEnumerable;

                    foreach (var child in children)
                    {
                        if (child is Element childVisualElement)
                        {
                            if (childVisualElement is T)
                            {
                                yield return(childVisualElement as T);
                            }

                            foreach (var childVisual in childVisualElement.GetChildrenOfType <T>())
                            {
                                yield return(childVisual);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 8
0
        protected override void OnAddChild(VisualNode widget, Xamarin.Forms.Element childControl)
        {
            if (childControl is View view)
            {
                Grid.SetRow(childControl, widget.GetMetadata <int>("Grid.Row"));
                Grid.SetRowSpan(childControl, widget.GetMetadata <int>("Grid.RowSpan", 1));
                Grid.SetColumn(childControl, widget.GetMetadata <int>("Grid.Column"));
                Grid.SetColumnSpan(childControl, widget.GetMetadata <int>("Grid.ColumnSpan", 1));

                NativeControl.Children.Insert(widget.ChildIndex, view);
            }
            else
            {
                throw new InvalidOperationException($"Type '{childControl.GetType()}' not supported under '{GetType()}'");
            }

            base.OnAddChild(widget, childControl);
        }
Ejemplo n.º 9
0
        public override void AddChild(XF.Element child, int physicalSiblingIndex)
        {
            if (child is null)
            {
                throw new ArgumentNullException(nameof(child));
            }

            if (child is TwoPaneViewPane1View pane1View)
            {
                TwoPaneViewControl.Pane1 = pane1View;
            }
            else if (child is TwoPaneViewPane2View pane2View)
            {
                TwoPaneViewControl.Pane2 = pane2View;
            }
            else
            {
                throw new InvalidOperationException($"Unknown child type {child.GetType().FullName} being added to parent element type {GetType().FullName}.");
            }
        }
        public virtual void AddChild(XF.Element child, int physicalSiblingIndex)
        {
            if (child is null)
            {
                throw new ArgumentNullException(nameof(child));
            }

            if (child is MasterDetailMasterPageContentPage masterPage)
            {
                MasterDetailPageControl.Master = masterPage;
            }
            else if (child is MasterDetailDetailPageContentPage detailPage)
            {
                MasterDetailPageControl.Detail = detailPage;
            }
            else
            {
                throw new InvalidOperationException($"Unknown child type {child.GetType().FullName} being added to parent element type {GetType().FullName}.");
            }
        }
        public virtual void RemoveChild(XF.Element child)
        {
            if (child is null)
            {
                throw new ArgumentNullException(nameof(child));
            }

            if (child == MasterDetailPageControl.Master)
            {
                MasterDetailPageControl.Master = new XF.Page()
                {
                    Title = "Title"
                };
            }
            else if (child == MasterDetailPageControl.Detail)
            {
                MasterDetailPageControl.Detail = new XF.Page();
            }
            else
            {
                throw new InvalidOperationException($"Unknown child type {child.GetType().FullName} being removed from parent element type {GetType().FullName}.");
            }
        }
Ejemplo n.º 12
0
        bool ElementFitsAncestorTypeAndLevel(Element element, ref int level, ref object lastPotentialBctx)
        {
            if (!(Source is RelativeBindingSource relativeSource))
            {
                return(false);
            }

            bool fitsElementType =
                relativeSource.Mode == RelativeBindingSourceMode.FindAncestor &&
                relativeSource.AncestorType.GetTypeInfo().IsAssignableFrom(element.GetType().GetTypeInfo());

            bool fitsBindingContextType =
                element.BindingContext != null &&
                relativeSource.Mode == RelativeBindingSourceMode.FindAncestorBindingContext &&
                relativeSource.AncestorType.GetTypeInfo().IsAssignableFrom(element.BindingContext.GetType().GetTypeInfo());

            if (!fitsElementType && !fitsBindingContextType)
            {
                return(false);
            }

            if (fitsBindingContextType)
            {
                if (!object.ReferenceEquals(lastPotentialBctx, element.BindingContext))
                {
                    lastPotentialBctx = element.BindingContext;
                    level++;
                }
            }
            else
            {
                level++;
            }

            return(level >= relativeSource.AncestorLevel);
        }
        public void RemoveChild(XF.Element child)
        {
            if (!(child is XF.GradientStop gradientStopChild))
            {
                throw new ArgumentException($"GradientBrush support GradientStop child elements only, but {child?.GetType()} found instead.", nameof(child));
            }

            GradientBrushControl.GradientStops.Remove(gradientStopChild);
        }
Ejemplo n.º 14
0
        public void AddChild(XF.Element child, int physicalSiblingIndex)
        {
            if (!(child is XF.View childView))
            {
                throw new ArgumentException($"Expected parent to be of type {typeof(XF.View).FullName} but it is of type {child?.GetType().FullName}.", nameof(child));
            }

            XF.Grid.SetColumn(childView, Column);
            XF.Grid.SetColumnSpan(childView, ColumnSpan);
            XF.Grid.SetRow(childView, Row);
            XF.Grid.SetRowSpan(childView, RowSpan);

            _children.Add(childView);
            _parentGrid.Children.Add(childView);
        }
Ejemplo n.º 15
0
        public void RemoveChild(XF.Element child)
        {
            if (!(child is XF.View childView))
            {
                throw new ArgumentException($"Expected parent to be of type {typeof(XF.View).FullName} but it is of type {child?.GetType().FullName}.", nameof(child));
            }

            _children.Remove(childView);
            _parentGrid.Children.Remove(childView);
        }
Ejemplo n.º 16
0
        public override void AddChild(XF.Element child, int physicalSiblingIndex)
        {
            if (child is null)
            {
                throw new ArgumentNullException(nameof(child));
            }

            switch (child)
            {
            case XF.TemplatedPage childAsTemplatedPage:
                ShellSectionControl.Items.Add(childAsTemplatedPage);     // Implicit conversion
                break;

            case XF.ShellContent childAsShellContent:
                ShellSectionControl.Items.Add(childAsShellContent);
                break;

            default:
                throw new NotSupportedException($"Handler of type '{GetType().FullName}' representing element type '{TargetElement?.GetType().FullName ?? "<null>"}' doesn't support adding a child (child type is '{child.GetType().FullName}').");
            }
        }
Ejemplo n.º 17
0
        public virtual void RemoveChild(XF.Element child)
        {
            if (child is null)
            {
                throw new ArgumentNullException(nameof(child));
            }

            var sectionToRemove = GetSectionForElement(child)
                                  ?? throw new NotSupportedException($"Handler of type '{GetType().FullName}' representing element type '{TargetElement?.GetType().FullName ?? "<null>"}' doesn't support removing a child (child type is '{child.GetType().FullName}').");

            ShellItemControl.Items.Remove(sectionToRemove);
        }
Ejemplo n.º 18
0
        public void SetParent(XF.Element parent)
        {
            if (parent is null)
            {
                throw new ArgumentNullException(nameof(parent));
            }
            if (!(parent is XF.VisualElement parentVisualElement))
            {
                throw new ArgumentNullException(nameof(parent), $"Expected parent to be of type '{typeof(XF.VisualElement).FullName}' but it is of type '{parent.GetType().FullName}'.");
            }
            _parentVisualElement = parentVisualElement;

            UpdateParentStyleSheetIfPossible();
        }
Ejemplo n.º 19
0
        public override void AddChild(XF.Element child, int physicalSiblingIndex)
        {
            if (child is null)
            {
                throw new ArgumentNullException(nameof(child));
            }

            var removedDummyChild = ClearDummyChild();

            switch (child)
            {
            case XF.TemplatedPage childAsTemplatedPage:
                ShellControl.Items.Add(childAsTemplatedPage);     // Implicit conversion
                break;

            case XF.ShellContent childAsShellContent:
                ShellControl.Items.Add(childAsShellContent);     // Implicit conversion
                break;

            case XF.ShellSection childAsShellSection:
                ShellControl.Items.Add(childAsShellSection);     // Implicit conversion
                break;

            case XF.MenuItem childAsMenuItem:
                ShellControl.Items.Add(childAsMenuItem);     // Implicit conversion
                break;

            case XF.ShellItem childAsShellItem:
                ShellControl.Items.Add(childAsShellItem);
                break;

            default:
                throw new NotSupportedException($"Handler of type '{GetType().FullName}' representing element type '{TargetElement?.GetType().FullName ?? "<null>"}' doesn't support adding a child (child type is '{child.GetType().FullName}').");
            }
            // TODO: If this was the first item added, mark it as the current item
            // But this code seems to cause a NullRef...
            //if (removedDummyChild)
            //{
            //    ShellControl.CurrentItem = parentAsShell.Items[0];
            //}
        }
        public int GetChildIndex(XF.Element child)
        {
            if (!(child is XF.GradientStop gradientStopChild))
            {
                throw new ArgumentException($"GradientBrush support GradientStop child elements only, but {child?.GetType()} found instead.", nameof(child));
            }

            return(GradientBrushControl.GradientStops.IndexOf(gradientStopChild));
        }
        public void AddChild(XF.Element child, int physicalSiblingIndex)
        {
            if (!(child is XF.GradientStop gradientStopChild))
            {
                throw new ArgumentException($"GradientBrush support GradientStop child elements only, but {child?.GetType()} found instead.", nameof(child));
            }

            if (physicalSiblingIndex <= GradientBrushControl.GradientStops.Count)
            {
                GradientBrushControl.GradientStops.Insert(physicalSiblingIndex, gradientStopChild);
            }
            else
            {
                Debug.WriteLine($"WARNING: {nameof(AddChild)} called with {nameof(physicalSiblingIndex)}={physicalSiblingIndex}, but GradientBrushControl.GradientStops.Count={GradientBrushControl.GradientStops}");
                GradientBrushControl.GradientStops.Add(gradientStopChild);
            }
        }
Ejemplo n.º 22
0
 public static double GetNamedSize(NamedSize size, Element targetElement)
 {
     return(GetNamedSize(size, targetElement.GetType()));
 }
Ejemplo n.º 23
0
        public virtual void RemoveChild(XF.Element child)
        {
            if (child is null)
            {
                throw new ArgumentNullException(nameof(child));
            }

            XF.ShellContent contentToRemove = child switch
            {
                XF.TemplatedPage childAsTemplatedPage => GetContentForTemplatePage(childAsTemplatedPage),
                XF.ShellContent childAsShellContent => childAsShellContent,
                _ => throw new NotSupportedException($"Handler of type '{GetType().FullName}' representing element type '{TargetElement?.GetType().FullName ?? "<null>"}' doesn't support removing a child (child type is '{child.GetType().FullName}').")
            };

            ShellSectionControl.Items.Remove(contentToRemove);
        }
Ejemplo n.º 24
0
        public virtual void AddChild(XF.Element child, int physicalSiblingIndex)
        {
            if (child is null)
            {
                throw new ArgumentNullException(nameof(child));
            }

            throw new NotSupportedException($"Handler of type '{GetType().FullName}' representing element type '{TargetElement?.GetType().FullName ?? "<null>"}' doesn't support adding a child (child type is '{child.GetType().FullName}').");
        }
Ejemplo n.º 25
0
        public virtual void AddChild(XF.Element child, int physicalSiblingIndex)
        {
            if (child is null)
            {
                throw new ArgumentNullException(nameof(child));
            }

            XF.ShellSection sectionToAdd = child switch
            {
                XF.TemplatedPage childAsTemplatedPage => childAsTemplatedPage,          // Implicit conversion
                            XF.ShellContent childAsShellContent => childAsShellContent, // Implicit conversion
                            XF.ShellSection childAsShellSection => childAsShellSection,
                            _ => throw new NotSupportedException($"Handler of type '{GetType().FullName}' representing element type '{TargetElement?.GetType().FullName ?? "<null>"}' doesn't support adding a child (child type is '{child.GetType().FullName}').")
            };

            if (ShellItemControl.Items.Count >= physicalSiblingIndex)
            {
                ShellItemControl.Items.Insert(physicalSiblingIndex, sectionToAdd);
            }
            else
            {
                Debug.WriteLine($"WARNING: {nameof(AddChild)} called with {nameof(physicalSiblingIndex)}={physicalSiblingIndex}, but ShellItemControl.Items.Count={ShellItemControl.Items.Count}");
                ShellItemControl.Items.Add(sectionToAdd);
            }
        }