情况如下: 我有一个UIElement,每个角落有4个椭圆 我想能够用鼠标/触摸拖动这些并使元素调整大小
(就像在添加形状时在Word中一样,...)
这是我目前的代码:
private void ResizeElement_PointerPressed(object sender, PointerRoutedEventArgs e) {
var fe = AssociatedObject as FrameworkElement;
ElementStartResizing?.Invoke(fe, new EventArgs());
ResizeEllipse = (Ellipse) sender;
ResizeEllipse.ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY;
ResizeEllipse.ManipulationDelta += ResizeElement_PointerMoved;
ResizeEllipse.PointerReleased += ResizeElement_PointerReleased;
pointerId = (int) e.Pointer.PointerId;
prevPoint = e.GetCurrentPoint(parent).Position;
switch (ResizeEllipse.Name) {
case "TopLeft":
centerPoint = new Point(fe.RenderSize.Width, fe.RenderSize.Height);
break;
case "TopRight":
centerPoint = new Point(0, fe.RenderSize.Height);
break;
case "BottomLeft":
centerPoint = new Point(fe.RenderSize.Width, 0);
break;
case "BottomRight":
centerPoint = new Point(0, 0);
break;
}
}
private void ResizeElement_PointerMoved(object sender, ManipulationDeltaRoutedEventArgs args) {
var fe = AssociatedObject as FrameworkElement;
var pos = args.Delta.Translation;
ResizeElement(centerPoint.X, centerPoint.Y, pos.X, pos.Y);
ElementResizing?.Invoke(fe, new EventArgs());
}
private void ResizeElement (double centerX, double centerY, double scaleX, double scaleY) {
var item = AssociatedObject as FrameworkElement;
var visual = ElementCompositionPreview.GetElementVisual(item);
var _compositor = ElementCompositionPreview.GetElementVisual(item).Compositor;
visual.CenterPoint = new Vector3((float) centerX, (float) centerY, 0);
var scaleOut = _compositor.CreateVector2KeyFrameAnimation();
scaleOut.InsertKeyFrame(0f, new Vector2(visual.Size.X + (float) scaleX, visual.Size.Y + (float) scaleY));
visual.StartAnimation("Size", scaleOut);
}
问题在于构图动画"尺寸"出于某种原因根本不起作用。 (在我的所有项目上,只有" Scale"" Opacity"工作)
修改
其他信息: 这是一个类,而不是Control(没有Xaml)
public class ResizeBehavior : DependencyObject, IBehavior {
public DependencyObject AssociatedObject { get; set; }
public void Attach (DependencyObject associatedObject) {
if ((associatedObject != AssociatedObject) && !Windows.ApplicationModel.DesignMode.DesignModeEnabled) {
AssociatedObject = associatedObject;
if (AssociatedObject is FrameworkElement fe) {
fe.Loaded += Fe_Loaded;
}
}
}
[...]
}
答案 0 :(得分:0)
它可能不是最优雅的方式,但我通过结合ManipulationDelta和改变高度和宽度找到了一个解决方案:
private void ResizeElement (double scaleX, double scaleY) {
var fe = AssociatedObject as FrameworkElement;
fe.Width = fe.ActualWidth + scaleX;
fe.Height = fe.ActualHeight + scaleY;
}
我删除了组合API方法并使用了普通的方法