Reputation: 2071
I would like to change size of a control in Xamarin.Forms with some event in code behind of a control.
Especially, I would like to change size of a StackLayout when Orientation of device changes.
For now, I have done it like this (but it does not work):
private void SettingsPage_OnSizeChanged(object sender, EventArgs e)
{
var isPortrait = UIHelpers.IsPortrait(this);
if (isPortrait)
RemoteServerSL.WidthRequest = RemoteServerSL.ParentView.Width;
else
{
RemoteServerSL.WidthRequest = RemoteServerSL.ParentView.Width/2;
}
this.UpdateChildrenLayout();
this.ForceLayout();
RemoteServerSL.ForceLayout();
}
The purpose is to change Width of my StackLayout to parentWidth if I'm in landscape mode.
How can I handle that?
BTW.I'm working on Android for now.
Upvotes: 1
Views: 2868
Reputation: 7454
You should use OnSizeAllocated method override to detect orientation change;
double previousWidth;
double previousHeight;
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
if (previousWidth != width || previousHeight != height)
{
previousWidth = width;
previousHeight = height;
if (width > height)
{
// landscape mode
}
else
{
// portrait mode
}
}
}
Upvotes: 2