Hello,
VisualState
does not contain any properties related to orientation, you could refer to Visual states for more detailed information.
Therefore, the VisualStateManager
will not actually run.
For the needs of monitoring the screen direction and dynamically modifying the layout, you can apply the feature that the size of the page elements will be recalculated after the layout is rotated and respond to the update through the OnSizeAllocated
method.
Please refer to the following code:
public partial class MainPage : ContentPage
{
int count = 0;
public MainPage()
{
InitializeComponent();
}
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
// Get Metrics
var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
var orientation = mainDisplayInfo.Orientation;
if (orientation == DisplayOrientation.Portrait)
{
stack.Orientation = StackOrientation.Vertical;
}
else if (orientation == DisplayOrientation.Landscape)
{
stack.Orientation = StackOrientation.Horizontal;
}
}
}
// here is the sample layout
<StackLayout x:Name="stack">
<Label Text="Label 1" />
<Label Text="Label 2" />
</StackLayout>
Best Regards,
Alec Liu.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.