В моем приложении пользователь может выбрать Image
и перетащить его на Grid
, чтобы играть с ним. Я делаю это, обрабатывая событие PointerEntered
Grid
. Здесь я обнаруживаю, что пользователь выбрал изображение, и если пользователь удерживает кнопку мыши.
Теперь я хочу поместить Image
в сетку и передать указатель (все еще нажатый) на мой Image
, поэтому Image
использует свои собственные ManipulationStarted
, ManipulationDelta
и ManipulationCompleted
события. Это должно позволить пользователю перетаскивать изображение одним плавным движением из списка изображений в Grid
, вместо того, чтобы отпускать и нажимать на элемент.
Я попытался высвободить указатель из sender
в PointerEntered
и захватить его с помощью CapturePointer
, но это не работает, хотя CapturePointer
возвращает true
.
Вот код, который я использую для события PointerEntered
:
private void DrawingArea_OnPointerEntered(object sender, PointerRoutedEventArgs e)
{
// If we enter the grid while dragging and we have an image that was dragged
if (e.Pointer.IsInContact && CurrentDraggedImage != null)
{
DrawingArea.Children.Add(CurrentDraggedImage);
// Move it to the location we're currently at
var transform = (CurrentDraggedImage.RenderTransform as CompositeTransform);
transform.TranslateX += e.GetCurrentPoint(DrawingArea).RawPosition.X - DrawingArea.ActualWidth / 2;
transform.TranslateY += e.GetCurrentPoint(DrawingArea).RawPosition.Y - DrawingArea.ActualHeight/2;
// This works (I think)
(sender as UIElement).ReleasePointerCaptures();
// This doesn't work (or it isn't what I need), but returns true
CurrentDraggedImage.CapturePointer(e.Pointer);
// Get ready for a new image
CurrentDraggedImage = null;
}
}
В этом ответе содержится мой код манипуляции: