缩放转换文本块

本文关键字:文本 转换 缩放 | 更新日期: 2023-09-27 17:56:38

我正在画布上添加一个文本块,用户可以随处携带,但现在我试图弄清楚如何使文本块能够放大或旋转捏住它,这是我到目前为止的代码:

//create the textblock 
TextBlock txt = new TextBlock() { Text = UserString, FontSize = 56 };
//Adding gesture listener here
GestureListener TextGestureListener = GestureService.GetGestureListener(txt);
txt.MouseEnter += txt_MouseEnter;
TextGestureListener.DragStarted += new EventHandler<DragStartedGestureEventArgs>(GestureListener_DragStarted);
TextGestureListener.DragDelta += new EventHandler<DragDeltaGestureEventArgs>(GestureListener_DragDelta);
TextGestureListener.DragCompleted += new EventHandler<DragCompletedGestureEventArgs>(GestureListener_DragCompleted);
TextGestureListener.PinchDelta += new EventHandler<PinchGestureEventArgs>(GestureListener_PinchDelta);
WholePicture.Children.Add(txt);
//pinch to zoom or at least try!
void GestureListener_PinchDelta(object sender, PinchGestureEventArgs e)
{
    TextBlock txt = (TextBlock)sender;
    Scale = (ScaleTransform)txt.RenderTransform;
    Scale.ScaleX = e.DistanceRatio;
    Scale.ScaleY = e.DistanceRatio;
}

现在,如果我尝试放大文本块,它只会因此错误而崩溃

       $exception   {System.InvalidCastException: Unable to cast object of type 'System.Windows.Media.TranslateTransform' to type 'System.Windows.Media.ScaleTransform'.
       at CrazyFill.Pages.CrazyFillCore.GestureListener_PinchDelta(Object sender, PinchGestureEventArgs e)
       at Microsoft.Phone.Controls.SafeRaise.Raise[T](EventHandler`1 eventToRaise, Object sender, GetEventArgs`1 getEventArgs)
       at Microsoft.Phone.Controls.GestureListener.RaiseGestureEvent[T](Func`2 eventGetter, Func`1 argsGetter, Boolean releaseMouseCapture)
       at Microsoft.Phone.Controls.GestureListener.ProcessTouchPanelEvents()
       at Microsoft.Phone.Controls.GestureListener.TouchDelta()
       at Microsoft.Phone.Controls.GestureListener.OnTouchFrameReported(Object sender, TouchFrameEventArgs e)
       at System.Windows.Input.Touch.OnTouch(Object sender, TouchFrameEventArgs e)
       at MS.Internal.JoltHelper.RaiseEvent(IntPtr target, UInt32 eventId, IntPtr coreEventArgs, UInt32 eventArgsTypeIndex)}    System.Exception {System.InvalidCastException}

缩放转换文本块

我认为您有一个错误,因为您用新转换覆盖了文本块的渲染转换。翻译的旧版本将以这种方式丢失。你可以使用CompositeTransform来做到这一点,并在上面设置scaletransform。

在这种情况下,我将使用数据绑定,而不仅仅是从代码隐藏进行操作。

    <TextBox Text="This will move" Height="80" Width="200" x:Name="ToMoveTextBox" RenderTransformOrigin="0.5,0.5">
        <TextBox.RenderTransform>
            <CompositeTransform ScaleX="{Binding ScaleXY}" ScaleY="{Binding ScaleXY}" 
                                TranslateX="{Binding TranslateX}" TranslateY="{Binding TranslateY}"/>
        </TextBox.RenderTransform>
        <toolkit:GestureService.GestureListener>
            <toolkit:GestureListener DragDelta="GestureListener_DragDelta" PinchDelta="GestureListener_PinchDelta"/>
        </toolkit:GestureService.GestureListener>
    </TextBox>

在我的示例中,我使用代码隐藏来计算值,但以其他方式,您也可以使用 CallMethodAction 从 ViewModel 中执行此操作(这是一个 UI 操作,因此您可以选择。

    private void GestureListener_DragDelta(object sender, DragDeltaGestureEventArgs e)
    {
        TranslateX += e.HorizontalChange;
        TranslateY += e.VerticalChange;
        e.Handled = true;
    }
    private void GestureListener_PinchDelta(object sender, PinchGestureEventArgs e)
    {
        ScaleXY = e.DistanceRatio;
        e.Handled = true;
    }

我希望它能帮助你。

GestureListener_PinchDelta显然是错误的,这是工作

void GestureListener_PinchDelta(object sender, PinchGestureEventArgs e)
{
        TextBlock ui = (TextBlock)sender;
        if (ui != null)
        {
            if (!(ui.RenderTransform is ScaleTransform))
                ui.RenderTransform = new ScaleTransform();
            ScaleTransform t = ui.RenderTransform as ScaleTransform;
            t.ScaleX = e.DistanceRatio;
            t.ScaleY = e.DistanceRatio;
            e.Handled = true;
        }
}

这只是一个小错误,那就是当我启动手势时,文本块忘记了用户放置它的位置并返回它创建的位置(页面顶部),为解决方案工作,当发现我会让你知道