现在,我正在开发照片应用程序(windows phone 8.1 runtime),但我遇到的问题是在照片缩放时没有限制图像拖动区域。
下面是代码:
<Canvas Name="zoomgrid" Visibility="Collapsed">
<Image x:Name="zoomimages"
Stretch="Fill"
Width="480"
Height="800"
ManipulationDelta="img_intro_ManipulationDelta"
RenderTransformOrigin="0.5,0.5"
ManipulationMode="All">
<Image.RenderTransform>
<CompositeTransform/>
</Image.RenderTransform>
</Image>
</Canvas>
double mincale = 0.5;
double maxscale = 10.0;
private void Image_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
Image elemt = sender as Image;
CompositeTransform transform = elemt.RenderTransform as CompositeTransform;
transform.ScaleX *= e.Delta.Scale;
transform.ScaleY *= e.Delta.Scale;
transform.TranslateX += e.Delta.Translation.X;
transform.TranslateY += e.Delta.Translation.Y;
if (transform.ScaleX < mincale) transform.ScaleX = mincale;
if (transform.ScaleY < mincale) transform.ScaleY = mincale;
if (transform.ScaleX > maxscale) transform.ScaleX = maxscale;
if (transform.ScaleY > maxscale) transform.ScaleY = maxscale;
//To limit the images dragging but did not success.
double scalewidth = Zoomimages.ActualWidth * ct.ScaleX;
double scleheight = Zoomimages.ActualHeight * ct.ScaleY;
double xdiff = Math.Max(0, (scalewidth - this.content.ActualWidth) / 2);
double ydiff = Math.Max(0, (scleheight - this.content.ActualHeight) / 2);
if (Math.Abs(ct.TranslateX) > xdiff)
ct.TranslateX = xdiff * Math.Sign(e.Delta.Translation.X);
if (Math.Abs(ct.TranslateY) > ydiff)
ct.TranslateY = ydiff * Math.Sign(e.Delta.Translation.Y);
}