【问题标题】:wp7 - set page orientation or alternativewp7 - 设置页面方向或替代
【发布时间】:2012-05-25 18:13:05
【问题描述】:

我已尝试但似乎无法在后面的代码中设置 wp7 应用程序的页面方向。我假设它无法完成,但我想我会在这里检查。

可以吗?

如果不是这里是我的问题,也许有人可以提供帮助。我创建了一个设置为 Landscape 的应用程序,并将 PageOrientation 设置为 LandscapeLeft。由于手机在移动,它有时会明显旋转到 LandscapeRight,我也不想要它。所以我这样做了

protected override void OnOrientationChanged(OrientationChangedEventArgs e)
{
     if(e.Orientation == PageOrientation.LandscapeLeft)
          base.OnOrientationChanged(e); 
}

问题解决了,但是,我向我的朋友展示了该应用程序,他们玩了一会。由于电话按钮和他们握住电话的方式,他们中的一些人更喜欢它是相反的方式(LandscapeRight)。

为了满足我的应用程序的用户,我想放置一个设置,他们可以根据他们想要 LandscapeLeft 或 LandscapeRight 来更改。由于我无法更改代码中的方向,我怎么能做到这一点?

我确实尝试过弄乱屏幕旋转,我有点工作了,即应用程序总是设置为 LandscapeLeft,如果他们想要 LandscapeRight,那么只需旋转变换 180*。但主要问题是 MessageBox 会出现颠倒。

【问题讨论】:

    标签: c# silverlight windows-phone-7


    【解决方案1】:

    您可以在后面的代码中设置页面方向

     this.Orientation = PageOrientation.Landscape;
    

    查看属性http://msdn.microsoft.com/en-us/library/microsoft.phone.controls.phoneapplicationpage.orientation(v=vs.92).aspx

    例如: http://www.kunal-chowdhury.com/2011/10/know-about-wp7-page-orientation-and.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+kunal2383+%28Kunal%27s+Blog%29

    但是,这仅限于设置 Landscape 或 Portrait - 它似乎忽略了 PortraitUp 和 PortraitDown 以及 LandscapeLeft 和 LandscapeRight。


    似乎您能做的最好的事情是强制手机进入横向,然后使用旋转变换 - 例如对于全屏页面(没有系统托盘或应用栏)然后这可以翻转页面之间左右风景:

        private bool t;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            SupportedOrientations = SupportedPageOrientation.Landscape;
            Orientation = PageOrientation.Landscape;
    
            if (t)
            {
                t = false;
                this.RenderTransform = new RotateTransform() {Angle = 180, CenterX = 400, CenterY = 240};
            }
            else
            {
                t = true;
                this.RenderTransform = null;
            }
        }
    

    那是 Xaml:

    <phone:PhoneApplicationPage 
        x:Class="PhoneApp1.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
        xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="480"
        FontFamily="{StaticResource PhoneFontFamilyNormal}"
        FontSize="{StaticResource PhoneFontSizeNormal}"
        Foreground="{StaticResource PhoneForegroundBrush}"
        SupportedOrientations="PortraitOrLandscape"  Orientation="Landscape"
        shell:SystemTray.IsVisible="False">
    
        <Grid x:Name="LayoutRoot" Background="Transparent">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
    
            <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
                <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
                <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    
                <Button Click="Button_Click" Content="one"/>
            </StackPanel>
    
            <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid>
        </Grid>
    
    </phone:PhoneApplicationPage>
    

    【讨论】:

    • 你不能设置 this.Orientation。好吧,您可以并且不会出错,但它什么也不做。
    • 同意-刚刚开始玩-发布了新答案-感谢您今天教我一些东西:)
    • 这与我所拥有的相似。我认为它(你的)实际上会更好地工作:-) 但是 MessageBox 呢?他们可能会出现颠倒?或者也许我应该自己制作随屏幕旋转的消息?
    • 如果它是一个正在运行的应用程序,那么你无论如何都不想显示消息框 :) 在 RunSat 中我只显示自定义覆盖(toasts)
    • 嗯,我有一个用于返回按钮的消息框,以确保用户实际按下它。我的一位用户在运行期间一直错误地推动它(不要问)哈哈。假设我可以自己做:-)
    【解决方案2】:

    只需将SupportedOrientations property 设置为横向。这样,无论您的用户持有 LandscapeLeft 还是 LandscapeRight,它都能正常工作。 There is no way to programmatically set one or the other.

    【讨论】:

    • 它并没有真正由用户将手机握在手中。手机(对于大多数用户而言)作为其正在运行的应用程序位于臂带中。问题是当你移动你的手臂时屏幕会旋转,我发现自己必须旋转我的手臂才能让屏幕旋转回来。
    猜你喜欢
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    • 2019-07-23
    • 1970-01-01
    • 2015-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多