Skip to content

Instantly share code, notes, and snippets.

@oddbear
Created November 25, 2016 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oddbear/e3eda01e0f19182e03fa3e665a79a101 to your computer and use it in GitHub Desktop.
Save oddbear/e3eda01e0f19182e03fa3e665a79a101 to your computer and use it in GitHub Desktop.
A Xamarin.Forms (2.3.2) hack, that short circuit the layout system.
public class PresenterTest : ContentPresenter
{
BoxView _testView;
public PresenterTest()
{
_testView = new BoxView {
BackgroundColor = Color.Red,
WidthRequest = 30,
HorizontalOptions = LayoutOptions.Fill,
VerticalOptions = LayoutOptions.Fill
};
var children = (ObservableCollection<Element>)((ILayoutController)this).Children;
children.Add(_testView);
var rand = new Random();
Device.StartTimer(TimeSpan.FromMilliseconds(100), () =>
{
_testView.WidthRequest = rand.Next(50, 80);
if (relayouts > 20)
{
System.Diagnostics.Debug.WriteLine(relayouts);
return false;
}
if (timer++ < 30)
{
System.Diagnostics.Debug.WriteLine(relayouts);
return false;
}
return true;
});
}
int timer = 0;
int relayouts = 0;
protected override void LayoutChildren(double x, double y, double width, double height)
{
relayouts++;
LayoutChildIntoBoundingRegion(_testView, new Rectangle(width / 2 - _testView.WidthRequest / 2, 50, _testView.WidthRequest, _testView.WidthRequest));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment