Bug 57838 - SetBoundsConstraint cannot be used to preconfigure a view for adding to a RelativeLayout
Summary: SetBoundsConstraint cannot be used to preconfigure a view for adding to a Rel...
Status: CONFIRMED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.4
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-06-28 15:13 UTC by Mike Norman
Modified: 2017-07-12 22:51 UTC (History)
2 users (show)

Tags: ac relativelayout boundscontstraint
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report for Bug 57838 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
CONFIRMED

Description Mike Norman 2017-06-28 15:13:54 UTC
Expected:
The user should be able to call RelativeLayout.SetBoundsConstraint(someView), and then call someRelLayout.Add(someView) to add a preconfigured view to a RelativeLayout.

Actual:
The following code does not compile:
public partial class App : Application
{
    public App()
    {
        InitializeComponent();

        ContentPage cp = new ContentPage();
        RelativeLayout rl = new RelativeLayout();
        cp.Content = rl;

        Label downHereLabel = new Label { Text = "Down here!" , BackgroundColor = Color.Blue};


        BoundsConstraint bc = BoundsConstraint.FromExpression(() => new Rectangle(cp.Width / 2, cp.Height / 2, cp.Width / 2, cp.Height / 2), null);
        RelativeLayout.SetBoundsConstraint(downHereLabel, bc);
        rl.Children.Add(downHereLabel); // NOTE: this line will not compile

        MainPage = cp;
    }

    protected override void OnStart()
    {
        // Handle when your app starts
    }

    protected override void OnSleep()
    {
        // Handle when your app sleeps
    }

    protected override void OnResume()
    {
        // Handle when your app resumes
    }
}

It appears that the Add() method overloads in IRelativeList<T> are ambiguous, and clobber the bare Add(TRestrict item) overload that seems to have been intended to have been inherited from the (internal) ObserveableWrapper<TTrack, TRestrict> class. In its absence, the compiler cannot choose between the Add() overload that has four default Constraint arguments and the one that has four default Expression<Func<double>> arguments. Whatever the reason, though, Add(someView) does not resolve.

As a workaround, the user can disambiguate by passing "(null as Constraint)", for example, but then should delay calling SetBoundsConstraint() until after the Add() call. However, this results in useless code being run that gets clobbered with the eventual SetBoundsConstraint() call.

Similar Add(someView) calls seem to work on other Layout subclasses, so perhaps guidance could be sought from the code in those classes.

My guess is that this is an untested regression from e6d5186c8 on 2017-03-07, when various types were given the internal access modifier. This would also explain things if, by chance, this scenario more or less works for XAML.
Comment 1 Jimmy [MSFT] 2017-06-29 16:59:53 UTC
I can confirm that I am seeing a build error when I try to add a View to the RelativeLayout as shown above. I get the error:

> The call is ambiguous between the following methods or properties: 'RelativeLayout.IRelativeList<T>.Add(T, Expression<Func<double>>, Expression<Func<double>>, Expression<Func<double>>, Expression<Func<double>>)' and 'RelativeLayout.IRelativeList<T>.Add(T, Constraint, Constraint, Constraint, Constraint)'

The commit you mentioned is included in 2.3.5 so I tested with several older versions to verify but the error still occurs. 


### Version Tests
2.3.4.247   BAD
2.3.3.193   BAD
2.3.2.127   BAD