Windows Phone - change layout on orientation change

痞子三分冷 提交于 2019-12-06 01:27:11

In the page resources define both of the templates

<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Key="DataTemplate1">
        <!--DEFINE TEMPLATE HERE-->
    </DataTemplate>
    <DataTemplate x:Key="DataTemplate2">
        <!--DEFINE TEMPLATE HERE-->
    </DataTemplate>
</phone:PhoneApplicationPage.Resources>

Then define the Pivot like this:

    <phone:Pivot x:Name="PivotPlatform"
                 Title="DEMO"
                 FontSize="13.333"
                 ItemsSource="{Binding PivotItems}">
        <phone:Pivot.HeaderTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Title}" />
            </DataTemplate>
        </phone:Pivot.HeaderTemplate>
    </phone:Pivot>

Handle the orientation changed event

<phone:PhoneApplicationPage ....
                        OrientationChanged="PhoneApplicationPage_OrientationChanged"
                        ....>

by setting the datatemplate programmatically

private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
    if (e.Orientation == PageOrientation.PortraitDown || e.Orientation == PageOrientation.PortraitUp)
    {
        PivotPlatform.ItemTemplate = this.Resources["DataTemplate1"] as DataTemplate;
    }
    else
    {
        PivotPlatform.ItemTemplate = this.Resources["DataTemplate2"] as DataTemplate;
    }
}

That should work!

You can create your new template for Pivot item in Page resources then handle layoutchange event:

void MainPage_OrientationChanged(object sender, OrientationChangedEventArgs e){if (e.Orientation == PageOrientation.LandscapeLeft || e.Orientation == PageOrientation.LandscapeRight)
{
}
else 
{
}}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!