0

我正在为 Windows 手机制作 Live Candle 应用程序..我想在手机方向改变时改变框架的方向..我无法这样做..我正在使用以下代码:-

namespace sdkSimpleMotionCS
{
  public partial class MainPage : PhoneApplicationPage
  {
    Motion motion;

    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        // Check to see if the Motion API is supported on the device.
        if (!Motion.IsSupported)
        {
            MessageBox.Show("the Motion API is not supported on this device.");
            return;
        }

        // If the Motion object is null, initialize it and add a CurrentValueChanged
        // event handler.
        if (motion == null)
        {
            motion = new Motion();
            motion.TimeBetweenUpdates = TimeSpan.FromMilliseconds(20);
            motion.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<MotionReading>>(motion_CurrentValueChanged);
        }

        // Try to start the Motion API.
        try
        {
            motion.Start();
        }
        catch (Exception)
        {
            MessageBox.Show("unable to start the Motion API.");
        }
    }

    void motion_CurrentValueChanged(object sender, SensorReadingEventArgs<MotionReading> e)
    {
        // This event arrives on a background thread. Use BeginInvoke to call
        // CurrentValueChanged on the UI thread.
        Dispatcher.BeginInvoke(() => CurrentValueChanged(e.SensorReading));
    }
    private void CurrentValueChanged(MotionReading e)
    {
        if (motion.IsDataValid)
        {
            // Show the numeric values for attitude
            yawTextBlock.Text = "YAW: " + MathHelper.ToDegrees(e.Attitude.Yaw).ToString("0") + "°";
            pitchTextBlock.Text = "PITCH: " + MathHelper.ToDegrees(e.Attitude.Pitch).ToString("0") + "°";
            rollTextBlock.Text = "ROLL: " + MathHelper.ToDegrees(e.Attitude.Roll).ToString("0") + "°";

            // Set the Angle of the triangle RenderTransforms to the attitude of the device
            ((RotateTransform)yawtriangle.RenderTransform).Angle = MathHelper.ToDegrees(e.Attitude.Yaw);
            ((RotateTransform)pitchtriangle.RenderTransform).Angle = MathHelper.ToDegrees(e.Attitude.Pitch);
            ((RotateTransform)rolltriangle.RenderTransform).Angle = MathHelper.ToDegrees(e.Attitude.Roll);

            // Show the numeric values for acceleration
            xTextBlock.Text = "X: " + e.DeviceAcceleration.X.ToString("0.00");
            yTextBlock.Text = "Y: " + e.DeviceAcceleration.Y.ToString("0.00");
            zTextBlock.Text = "Z: " + e.DeviceAcceleration.Z.ToString("0.00");

            // Show the acceleration values graphically
            xLine.X2 = xLine.X1 + e.DeviceAcceleration.X * 100;
            yLine.Y2 = yLine.Y1 - e.DeviceAcceleration.Y * 100;
            zLine.X2 = zLine.X1 - e.DeviceAcceleration.Z * 50;
            zLine.Y2 = zLine.Y1 + e.DeviceAcceleration.Z * 50;
        }
    }
  }
}

我正在使用运动概念。

4

1 回答 1

0

您需要在 Page 中处理 OrientationChanged事件或更好地使用此类而不是默认的 PhoneApplicationFrame 代码来动画和淡化 Windows Phone 方向更改

于 2012-07-18T09:16:02.727 回答