Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Updating RelativeLayout constraints on View?

$
0
0

The idea is the following:

  • I have a custom view that will only work on a RelativeLayout.
  • When the Parent IS a RelativeLayout I want to set the constraint automatically so the user doesn't have to.
  • For setting the constraint, I intended to delete the previous view and readd it.

    When trying this, I get this error: Cannot change observablecollection during a collectionchanged event., at this point I don't know how else I can update the Constraints. Any Ideas?

Code happens on OnParentSet of my Custom view, the error part happens in Set the RelativeLayout region:

protected override void OnParentSet()
        {
            base.OnParentSet();

            if ((Parent is RelativeLayout))
            {

                #region Set the layout.

                var mContent = new StackLayout();

                mContent.Orientation =
                    (ScreenOrigin == ScreenPosition.Left || ScreenOrigin == ScreenPosition.Right) ?
                    StackOrientation.Horizontal :
                    StackOrientation.Vertical;

                // Content.
                BindableLayout.SetItemsSource(mContent, ItemsSource);

                if (ItemHeaderTemplate == null)
                {
                    BindableLayout.SetItemTemplate(mContent, ItemTemplate);
                }
                else
                {
                    BindableLayout.SetItemTemplateSelector(mContent, new OptionSliderDataTemplateSelector(ItemTemplate, ItemHeaderTemplate));
                }

                Content = mContent;

                #endregion

                #region Set the RelativeLayout

                var mParent = Parent as RelativeLayout;
                var index = mParent.Children.IndexOf(this);
                mParent.Children.RemoveAt(index);
                mParent.Children.Add(
                    this,
                    Constraint.RelativeToParent((parent) => { return (.5 * parent.Width) - 100; }),
                    Constraint.RelativeToParent((parent) => { return (.5 * parent.Height) - 100; }),
                    Constraint.Constant(50),
                    Constraint.Constant(50));

                #endregion
            }
            else
            {
#if DEBUG
                throw new ArgumentException($"[{GetType().Name}]: Parent MUST be a RelativeLayout.", $"{nameof(Parent)}");
#else
                        Debug.WriteLine($"[{GetType().Name}]: Parent MUST be a RelativeLayout.");
#endif
            }
        }

Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>