6

我在滚动查看器中放置了一个图像控件,就像 tnis 一样:

        <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" >
        <Image Source="/Test/1.jpg" Width="320">
            <Image.RenderTransform>
                <CompositeTransform ScaleX="{Binding Path=Value, ElementName=slider}"/>
            </Image.RenderTransform>
        </Image>
    </ScrollViewer>

如代码所示,我添加了一个滑块来控制图像的 Compositetransform,但是当我更改滑块的值时,什么也没发生?

而且我还尝试在图像上附加缩放和平移行为(取决于工具包),不幸的是,我可以上下滚动,但无法放大/缩小图像。Scrollviewer 似乎阻止了捏合操作。

众所周知,Scrollviewer 控件在 WPF 中有一个“ZoomMode”属性,但在 Windows Phone 中已弃用。那么如何在滚动查看器中实现 iamge 缩放,有人可以帮我吗?

4

1 回答 1

6

xml代码:

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <ScrollViewer x:Name="scrl" Height="300" Width="300" BorderBrush="red"
                      BorderThickness="2" VerticalScrollBarVisibility="Disabled">
            <StackPanel>
                <Image x:Name="img" Source="Assets/Mountain.jpg" Height="100" Width="150"
                       RenderTransformOrigin="0.5,0.5">
                    <Image.RenderTransform>
                        <CompositeTransform x:Name="trans"/>
                    </Image.RenderTransform>
                    <toolkit:GestureService.GestureListener>
                        <toolkit:GestureListener PinchStarted="GestureListener_PinchStarted" PinchDelta="GestureListener_PinchDelta" DragDelta="GestureListener_DragDelta"/>
                    </toolkit:GestureService.GestureListener>
                </Image>
            </StackPanel>
        </ScrollViewer>
    </Grid>

cs代码:

    double _initialAngle, _initialScale;
    public MainPage()
    {
        InitializeComponent();
        scrl.ManipulationMode = ManipulationMode.Control;
    }

    private void GestureListener_PinchStarted(object sender, PinchStartedGestureEventArgs e)
    {
        _initialAngle = trans.Rotation;
        _initialScale = trans.ScaleX;
    }

    private void GestureListener_PinchDelta(object sender, PinchGestureEventArgs e)
    {
        trans.Rotation = _initialAngle + e.TotalAngleDelta;
        var curZoom = _initialScale * e.DistanceRatio;
        if (curZoom >= 1 && curZoom <= 3)
        {
            trans.ScaleX = _initialScale * e.DistanceRatio;
            trans.ScaleY = _initialScale * e.DistanceRatio;
        }
    }

    private void GestureListener_DragDelta(object sender, DragDeltaGestureEventArgs e)
    {
        trans.TranslateX += e.HorizontalChange;
        trans.TranslateY += e.VerticalChange;
    }

此代码适用于缩放、旋转和缩放。

于 2014-05-23T13:23:36.967 回答