PhilipMR
PhilipMR

Reputation: 49

Why aren't my labels showing up?

I've created a control that consists of a set of labels that listen to touch events (to select one of them; the 'SelectList'). When I embed this control into one of my pages, everything works fine.

However, I'm creating a control that serves as a container for any other View/control and uses an alpha mask to hide parts of it. When I put my SelectList in this container, everything appears exactly as it should except the labels are all invisible! As it turns out their width and height are never set (both remain at -1).

The way it works right now is the SelectList is a RelativeLayout containing a StackLayout with labels. The container is a View with a property handle to the content, then the custom renderer masks it after converting it to a native UIView using a little extension method snippet I found online:

    public static UIView ConvertToNative(this View view, CGRect size)
    {
        var renderer = Platform.CreateRenderer(view);

        renderer.NativeView.Frame = size;
        renderer.Element.Layout(size.ToRectangle());
        renderer.NativeView.SetNeedsLayout();

        return renderer.NativeView;
    }

No matter what I do the labels don't get a size assigned to them (I've tried Layout/ForceLayout on SizeChanged events and in a lot of irrelevant places as well to be sure). Setting the width- and height request manually does make the labels show up, but this is seems like a hack rather than a proper solution.

EDIT: I also think it might be worth mentioning that the size of the StackLayout containing the labels does appear to be normal. Also, using a ListView is not an option in this scenario due to styling limitations.

Upvotes: 0

Views: 101

Answers (1)

Yuri S
Yuri S

Reputation: 5370

Change you PopupView as below:

public class PopupView : ContentView
    {
        //public readonly View Content;

        public PopupView(View content)
        {
            if (content == default(View))
            {
                throw new ArgumentNullException(nameof(content));
            }
            Content = content;
        }
    }

I also shifted your box as it shows on top of labels. It is added after adding labels.

    private void Test3()
    {
        var selectList = GenerateSimpleSelectList();
        var boxView = new BoxView() { Color = Color.Purple };
        selectList.Children.Add(boxView,
                          Constraint.RelativeToParent((p) => { return p.X + 200; }),
                          Constraint.RelativeToParent((p) => { return p.Y + 200; }),
                          Constraint.RelativeToParent((p) => { return p.Width - 200; }),
                          Constraint.RelativeToParent((p) => { return p.Height - 200; }));
        var popupView = new PopupView(selectList);
        this.Content = popupView;
    }

enter image description here

Upvotes: 1

Related Questions