我在画布上添加了一个文本块,用户可以将其带到任何地方,但现在我一直试图找出如何使文本块能够放大或旋转捏住它的方法,
到目前为止,这是我的代码:
//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}
最佳答案
我认为您有一个错误,因为您用新的转换覆盖了文本块的rendertransform。较旧的translatex-y将会丢失。您可以对此使用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;
}
希望对您有所帮助。