Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.