Beispiel #1
0
        /// <summary>
        /// Safely init layout control element and sighn up on internal hadler events.
        /// </summary>
        /// <param name="control">The GUI control that will be instiniated.</param>
        /// <param name="member">The member that will be binded to the GUI.</param>
        /// <param name="value">Value that will applied as default.</param>
        public void ControlSignUp(IGUIField control, MemberInfo member, object value)
        {
            try
            {
                // Registrate member in auto layout handler.
                control.RegistrateField(this, member, value);

                // Adding instiniated element to the layout.
                activeLayer?.ApplyControl(control as FrameworkElement);
            }
            catch (Exception ex)
            { MessageBox.Show("Constrol sign up failed.\n\nDetails:\n" + ex.Message); }
        }
        /// <summary>
        /// Insiniate UI by descriptor's attributes map and add it as child to parent element.
        /// </summary>
        /// <param name="layer">UI layer that will used as a root for the descriptor's elements.</param>
        public void BindTo(LayoutLayer layer)
        {
            // Storing layer.
            RootLayer   = layer;
            activeLayer = layer;

            #region Getting descripting data
            // Getting relevant type.
            var selfType = this.GetType();

            // Getting options applied to every memeber.
            var globalOptions = Attribute.GetCustomAttributes(selfType, typeof(Attribute)).Where
                                    (f => f.GetType().GetInterface(typeof(IGUILayoutOption).FullName) != null);

            // Get all memebers.
            var members = selfType.GetMembers();

            // Sorting by order.
            var orderedMembers = members.Where(f => f.GetCustomAttribute <OrderAttribute>() != null).
                                 OrderBy(f => f.GetCustomAttribute <OrderAttribute>().Order);

            // Sorting disordered members by metadata.
            var disorderedMembers = members.Where(f => f.GetCustomAttribute <OrderAttribute>() == null).
                                    OrderBy(f => f.MetadataToken);
            #endregion

            // Sort in declaretion order.
            members = orderedMembers.Concat(disorderedMembers).ToArray();

            // Perform all descriptor map.
            foreach (MemberInfo member in members)
            {
                var memberMeta = new MembersHandler.MemberMeta(member);

                #region Validation
                // Skip if the member is not field or property.
                if (!memberMeta.IsValue)
                {
                    continue;
                }

                // Skip if member excluded from instpector.
                if (member.GetCustomAttribute <HideInInspectorAttribute>() != null)
                {
                    continue;
                }
                #endregion

                // Instantiating a field from member.
                var field = InstantiateMember(ref activeLayer, memberMeta, globalOptions);

                // Skip in case if not instantiated.
                if (field == null)
                {
                    continue;
                }

                // Applying to the layout.
                activeLayer?.ApplyControl(field as FrameworkElement);
            }

            // Marking as loaded.
            IsLoaded = true;

            // Calling the local handler.
            OnLoaded();

            // Inform subscribers.
            Loaded?.Invoke(this);
        }
        /// <summary>
        /// Binding UIDescriptor content to the layer's root.
        /// </summary>
        /// <param name="layer">Target layer.</param>
        public async Task BindToAsync(LayoutLayer layer)
        {
            // Storing layer.
            RootLayer   = layer;
            activeLayer = layer;

            #region Getting descripting data
            // Getting relevant type.
            var selfType = this.GetType();

            // Getting options applied to every memeber.
            var globalOptions = Attribute.GetCustomAttributes(selfType, typeof(Attribute)).Where
                                    (f => f.GetType().GetInterface(typeof(IGUILayoutOption).FullName) != null);

            // Get all memebers.
            var members = selfType.GetMembers();

            // Sorting by order.
            var orderedMembers = members.Where(f => f.GetCustomAttribute <OrderAttribute>() != null).
                                 OrderBy(f => f.GetCustomAttribute <OrderAttribute>().Order);

            // Sorting disordered members by metadata.
            var disorderedMembers = members.Where(f => f.GetCustomAttribute <OrderAttribute>() == null).
                                    OrderBy(f => f.MetadataToken);
            #endregion

            // Sort in declaretion order.
            members = orderedMembers.Concat(disorderedMembers).ToArray();

            // Perform all descriptor map.
            foreach (MemberInfo member in members)
            {
                var memberMeta = new MembersHandler.MemberMeta(member);

                #region Validation
                // Skip if the member is not field or property.
                if (!memberMeta.IsValue)
                {
                    continue;
                }

                // Skip if member excluded from instpector.
                if (member.GetCustomAttribute <HideInInspectorAttribute>() != null)
                {
                    continue;
                }
                #endregion

                #region Virtualization
                // Suspending virtualization
                if (IsVirtualized &&
                    virtualizedPackCounter == VirtualizedItemsPack)
                {
                    // Droping the counter.
                    virtualizedPackCounter = 0;

                    // Waiting till loading.
                    while (!lastVirtualizedElement.IsLoaded)
                    {
                        await Task.Delay(5);
                    }

                    Panel rootPanel = (Panel)RootLayer.root;

                    // Checking if the last element still in the visible bounds.
                    bool isVisible = LayoutHandler.IsUserVisible(lastVirtualizedElement, Window.GetWindow(lastVirtualizedElement));

                    // Suspending if the last not visible till view update.
                    if (!isVisible)
                    {
                        // Marker that using for blocking the thread.
                        bool unlocked = false;

                        var window = Window.GetWindow(lastVirtualizedElement);

                        // Waiting till the root will change a size/
                        window.Activated      += Activated;
                        rootPanel.SizeChanged += SizeChanged;

                        void Activated(object sender, EventArgs e)
                        {
                            VirtValHandler();
                        };

                        void SizeChanged(object sender, SizeChangedEventArgs e)
                        {
                            VirtValHandler();
                        };

                        void VirtValHandler()
                        {
                            // Checking if the last element is already visible.
                            isVisible = LayoutHandler.IsUserVisible(lastVirtualizedElement, window);
                            if (isVisible)
                            {
                                // Unsubscribing from event.
                                window.Activated      -= Activated;
                                rootPanel.SizeChanged -= SizeChanged;

                                // Unblocking the thread.
                                unlocked = true;
                            }
                        }

                        // Unblocking instantiation of next group of elements.
                        while (!unlocked)
                        {
                            await Task.Delay(5);
                        }
                    }
                }

                #endregion

                if (!(RegistredFields[member] is IGUIField virtualizedElement))
                {
                    // Instantiating a member.
                    var field = InstantiateMember(ref activeLayer, memberMeta, globalOptions);

                    // Skip in case if not instantiated.
                    if (field == null)
                    {
                        continue;
                    }

                    // Storing in virtualization meta.
                    var meta = new VirtualizedItemMeta(
                        field,
                        ref activeLayer,
                        member);

                    // Settup into virtualization system.
                    lastVirtualizedElement = meta.Element ?? lastVirtualizedElement;
                    //VirtualizedElements.Add(meta);

                    // Applying to the layout.
                    activeLayer?.ApplyControl(field as FrameworkElement);
                }