Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Silverlight几何体缩放问题_Silverlight_Geometry_Zooming - Fatal编程技术网

Silverlight几何体缩放问题

Silverlight几何体缩放问题,silverlight,geometry,zooming,Silverlight,Geometry,Zooming,如何使用几何体和ScaleTransform在Silverlight中实现缩放到特定点?你能建议一些算法,这样我就能做到这一点吗?我的逻辑是错误的。我想我在设置缩放的中心点时遇到了一些麻烦 这是我在xaml文件中的几何体数据 <Canvas x:Name="LayoutRoot" Background="Orchid" MouseWheel="PathDraw_OnMouseWheel"> <Path x:Name="PathDraw" Stroke="Brown" S

如何使用几何体和ScaleTransform在Silverlight中实现缩放到特定点?你能建议一些算法,这样我就能做到这一点吗?我的逻辑是错误的。我想我在设置缩放的中心点时遇到了一些麻烦

这是我在xaml文件中的几何体数据

<Canvas x:Name="LayoutRoot"  Background="Orchid" MouseWheel="PathDraw_OnMouseWheel">
  <Path x:Name="PathDraw" Stroke="Brown"  StrokeThickness="1" Margin="200,200,0,0">
    <Path.Data>
      <RectangleGeometry x:Name="rect"  Rect="80 80 80 80"/>
    </Path.Data>
  </Path>
</Canvas>

下面是缩放逻辑

private static int ZoomSteps = 0;

    private static double centerX = 0;
    private static double centerY = 0;
    private double zoomCoeff = 1.1;

    private void PathDraw_OnMouseWheel(object sender, MouseWheelEventArgs e)
    {
        double delta = e.Delta;
        if (scaleX != 1)
        {
            scaleX = delta >= 0 ? scaleX * zoomCoeff : (scaleX <= 1 ? 1 : scaleX / zoomCoeff);
            scaleY = delta >= 0 ? scaleY * zoomCoeff : (scaleY <= 1 ? 1 : scaleY/zoomCoeff);
        }
        else
        {
            scaleX = scaleY = delta >= 0 ? zoomCoeff : 1;
        }

        scale = new ScaleTransform { ScaleX = scaleX, ScaleY = scaleY,
                                     CenterX = e.GetPosition(PathDraw).X,
                                     CenterY = e.GetPosition(PathDraw).Y

        };
        rect.Transform = scale;
    }
private static int ZoomSteps=0;
专用静态双中心X=0;
专用静态双中心=0;
专用双zoomCoeff=1.1;
MouseWheel上的私有void路径绘制(对象发送器,MouseWheelEventArgs e)
{
双三角洲=东三角洲;
如果(scaleX!=1)
{
scaleX=delta>=0?scaleX*zoomCoeff:(scaleX=0?scaleY*zoomCoeff:(scaleY=0?zoomCoeff:1;
}
scale=new ScaleTransform{ScaleX=ScaleX,ScaleY=ScaleY,
CenterX=e.GetPosition(路径绘制).X,
CenterY=e.GetPosition(路径绘制).Y
};
矩形变换=比例;
}

多亏了

您应该使用两种变换来分离缩放和平移,而不是在路径上留有边距和具有固有偏移的几何体(80,80处的左上角为矩形):

<Canvas x:Name="LayoutRoot"  Background="AliceBlue" MouseWheel="PathDraw_OnMouseWheel">
    <Path x:Name="PathDraw" Stroke="Brown" StrokeThickness="1">
        <Path.Data>
            <RectangleGeometry x:Name="rect" Rect="0 0 80 80">
                <RectangleGeometry.Transform>
                    <TransformGroup>
                        <ScaleTransform x:Name="scaleTransform"/>
                        <TranslateTransform x:Name="translateTransform"
                                            X="100" Y="100"/>
                    </TransformGroup>
                </RectangleGeometry.Transform>
            </RectangleGeometry>
        </Path.Data>
    </Path>
</Canvas>

您现在还可以在MouseMove处理程序中独立操作转换,以便拖动矩形。

我在画布中包含的xaml文件中有一个矩形。如果(scaleX!=1){scaleX=delta>=0?scaleX*zoomCoeff:(scaleX=0?scaleY*zoomCoeff:(scaleY=0?zoomCoeff:1;}scale=new ScaleTransform{ScaleX=ScaleX,scaleY=scaleY,CenterX=e.GetPosition(PathDraw).X,CenterY=e.GetPosition(PathDraw).Y};rect.Transform=scale;但如果不将此代码包含在您的问题中,则很难在注释中阅读它。
private double zoomCoeff = 1.1;

private void PathDraw_OnMouseWheel(object sender, MouseWheelEventArgs e)
{
    var deltaScale = (e.Delta > 0) ? zoomCoeff : (1d / zoomCoeff);
    var position = e.GetPosition((UIElement)sender);
    var dx = position.X - translateTransform.X;
    var dy = position.Y - translateTransform.Y;

    translateTransform.X = position.X - deltaScale * dx;
    translateTransform.Y = position.Y - deltaScale * dy;
    scaleTransform.ScaleX *= deltaScale;
    scaleTransform.ScaleY *= deltaScale;
}