Windows phone 8 旋转后将图像设置为始终位于中心画布(Windows phone 8)

Windows phone 8 旋转后将图像设置为始终位于中心画布(Windows phone 8),windows-phone-8,Windows Phone 8,有没有办法在WindowsPhone8应用程序中设置图像始终居中画布。 在我的应用程序中,我在画布中有图像。下面是xaml代码 <Canvas Name="myCanvas" Margin="0,0,0,0" Grid.Row="1"> <Image Canvas.ZIndex="1" Name="OrginalImage" </Image> 我的想法是当方向改变时,我设置图像的宽度和高度,然后把它放在中间。但此

有没有办法在WindowsPhone8应用程序中设置图像始终居中画布。 在我的应用程序中,我在画布中有图像。下面是xaml代码

       <Canvas Name="myCanvas" Margin="0,0,0,0" Grid.Row="1">
          <Image Canvas.ZIndex="1" Name="OrginalImage"

        </Image>

我的想法是当方向改变时,我设置图像的宽度和高度,然后把它放在中间。但此代码不会将图像设置在画布的中心?

通常,您应该避免使用
canvas
,除非您有非常具体的原因(例如,可能是绘图程序或游戏)。相反,更喜欢使用类似于网格的东西,它允许您使用相对定位:

<Grid>
  <Image HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>

我终于找到了调整图像大小的解决方案。这是我的密码。希望这能帮助像我这样的人

  void ResizeImage(bool center)
    {
        if (_coercedScale != 0 && _bitmap != null)
        {
            double newWidth = myCanvas.Width = Math.Round(_bitmap.PixelWidth * _coercedScale);
            double newHeight = myCanvas.Height = Math.Round(_bitmap.PixelHeight * _coercedScale);

            xform.ScaleX = xform.ScaleY = _coercedScale;

            viewport.Bounds = new Rect(0, 0, newWidth, newHeight);

            if (center)
            {
                viewport.SetViewportOrigin(
                    new Point(
                        Math.Round((newWidth - viewport.ActualWidth) / 2),
                        Math.Round((newHeight - viewport.ActualHeight) / 2)
                        ));
            }
            else
            {
                Point newImgMid = new Point(newWidth * _relativeMidpoint.X, newHeight * _relativeMidpoint.Y);
                Point origin = new Point(newImgMid.X - _screenMidpoint.X, newImgMid.Y - _screenMidpoint.Y);
                viewport.SetViewportOrigin(origin);
            }
        }
    }

是的,彼得,我的应用程序是绘图程序
  void ResizeImage(bool center)
    {
        if (_coercedScale != 0 && _bitmap != null)
        {
            double newWidth = myCanvas.Width = Math.Round(_bitmap.PixelWidth * _coercedScale);
            double newHeight = myCanvas.Height = Math.Round(_bitmap.PixelHeight * _coercedScale);

            xform.ScaleX = xform.ScaleY = _coercedScale;

            viewport.Bounds = new Rect(0, 0, newWidth, newHeight);

            if (center)
            {
                viewport.SetViewportOrigin(
                    new Point(
                        Math.Round((newWidth - viewport.ActualWidth) / 2),
                        Math.Round((newHeight - viewport.ActualHeight) / 2)
                        ));
            }
            else
            {
                Point newImgMid = new Point(newWidth * _relativeMidpoint.X, newHeight * _relativeMidpoint.Y);
                Point origin = new Point(newImgMid.X - _screenMidpoint.X, newImgMid.Y - _screenMidpoint.Y);
                viewport.SetViewportOrigin(origin);
            }
        }
    }