c# - 画笔方向与手机方向不匹配

标签 c# windows-phone-7 xaml

我正在尝试将画笔方向与手机方向相匹配,但我在实现此解决方案时遇到了问题。我的 xaml 页面设置为 PortraitOrLandscape,无论手机的方向如何,我都希望视频画笔正面朝上。在onOrentationChanged事件中加入方向改变if语句之前,出现了如下情况

手机:横向左侧,Videobrush:右侧朝上

手机:人像,Videobrush,顺时针旋转-90

手机:横向右,Videobrush,顺时针旋转-180

XAML

<Rectangle x:Name="videoRectangle" Margin="0,0,0,0">
            <Rectangle.Fill>
                <VideoBrush x:Name="viewfinderBrush" AlignmentX="Left" AlignmentY="Top" Stretch="UniformToFill">
                    <VideoBrush.RelativeTransform>
                        <CompositeTransform x:Name="viewfinderTransform" 
                                            CenterX="0.5" CenterY="0.5"/>
                    </VideoBrush.RelativeTransform>
                </VideoBrush>                    
            </Rectangle.Fill>
        </Rectangle>

XAML.CS

protected override void OnOrientationChanged(OrientationChangedEventArgs e)
    {
        base.OnOrientationChanged(e);

        if (e.Orientation == PageOrientation.LandscapeLeft)
        {                //do nothing
                         //The videobrush orientation is currently right side up
        }
        if (e.Orientation == PageOrientation.Portrait)
        {
            //the videobrush is currently rotated 90 degrees counter clockwise
            this.viewfinderTransform.Rotation = this.camera.Orientation + 90.0;
        }
        if (e.Orientation == PageOrientation.LandscapeRight)
        {
            //the videobrush is currently rotated 180 degrees counter clockwise
            this.viewfinderTransform.Rotation = this.camera.Orientation + 180;
        }
    }

添加 if 语句后,画笔方向变得更加疯狂。我究竟做错了什么?无论手机的方向如何,我只是想让画笔正面朝上。

最佳答案

我正在使用一个简单的开关/外壳来正确旋转画笔:

protected override void OnOrientationChanged(OrientationChangedEventArgs e)
{
  base.OnOrientationChanged(e);
  switch (e.Orientation)
  {
    case PageOrientation.Landscape:
    case PageOrientation.LandscapeLeft:
      videoBrushTransform.Rotation = 0;
      break;
    case PageOrientation.LandscapeRight:
      videoBrushTransform.Rotation = 180;
      break;
    case PageOrientation.Portrait:
    case PageOrientation.PortraitUp:
      videoBrushTransform.Rotation = 90;
      break;
    case PageOrientation.PortraitDown:
      videoBrushTransform.Rotation = 270;
      break;
  }
}

对我来说效果很好。

关于c# - 画笔方向与手机方向不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10220558/

相关文章:

c# - C# 中的三层应用程序 - 放置数据和业务模型的位置

c# - 浮点运算太可靠

windows-phone-7 - Azure 通知中心和 WP8 间歇通知

windows-phone-7 - 更改 UI 以响应 Storyboard.Completed 事件。我们需要 BeginInvoke 吗?

c# - WP7 应用程序中的 "Navigation is not allowed when the task is not in the foreground"

c# - 页眉和页脚布局

C# Nullable Explicit cast required

c# - 使用 AutoMapper 将数据从 SuperClass 复制到 SubClass

c# - 为什么 XAML(WPF) 不在我的 XML 命名空间定义中搜索控件?

xaml - 如何将控件高度数据绑定(bind)到另一个控件的高度?