Jak mogę dołączyć zdarzenia DragStarted
DragDelta
do siatki w windows 8/WinRT
. Zrobiłem to samo w Windows Phone z metodą GestureService.GetGestureListener()
. Próbowałem zastąpić kod zdarzeniami ManipulationStarted & ManipulationDelta w systemie Windows 8. Jednak wynik nie jest taki sam. W telefonie z systemem Windows dla pojedynczego przeciągnięcia wchodzi zdarzenia DragDelta 2 lub więcej razy. Ale z drugiej strony w Windows 8, w zdarzeniu ManupulationDelta uruchamia się tylko raz dla podobnej operacji przeciągania.Implementowanie zdarzeń DragStarted DragDelta w Windows 8/WinRT
Odpowiedz
Tak, myślę, że wiem, czego chcesz.
powiedzmy masz jakieś XAML tak:
<Grid Margin="50">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Rectangle Fill="Blue" x:Name="MyRect" />
</Grid>
chcesz przenieść ten prostokąt wokół Grid
przeciągając go.
Wystarczy użyć tego kodu:
public MainPage()
{
this.InitializeComponent();
MyRect.ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY;
MyRect.ManipulationDelta += Rectangle_ManipulationDelta;
MyRect.ManipulationCompleted += Rectangle_ManipulationCompleted;
}
private void Rectangle_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
var _Rectangle = sender as Windows.UI.Xaml.Shapes.Rectangle;
var _Transform = (_Rectangle.RenderTransform = (_Rectangle.RenderTransform as TranslateTransform) ?? new TranslateTransform()) as TranslateTransform;
_Transform.X += e.Delta.Translation.X;
_Transform.Y += e.Delta.Translation.Y;
}
private void Rectangle_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
var _Rectangle = sender as Windows.UI.Xaml.Shapes.Rectangle;
_Rectangle.RenderTransform = null;
var _Column = System.Convert.ToInt16(_Rectangle.GetValue(Grid.ColumnProperty));
if (_Column <= 0 && e.Cumulative.Translation.X > _Rectangle.RenderSize.Width * .5)
_Rectangle.SetValue(Grid.ColumnProperty, 1);
else if (_Column == 1 && e.Cumulative.Translation.X < _Rectangle.RenderSize.Width * -.5)
_Rectangle.SetValue(Grid.ColumnProperty, 0);
var _Row = System.Convert.ToInt16(_Rectangle.GetValue(Grid.RowProperty));
if (_Row <= 0 && e.Cumulative.Translation.Y > _Rectangle.RenderSize.Height * .5)
_Rectangle.SetValue(Grid.RowProperty, 1);
else if (_Row == 1 && e.Cumulative.Translation.Y < _Rectangle.RenderSize.Height * -.5)
_Rectangle.SetValue(Grid.RowProperty, 0);
}
dla tego:
nadzieję, że będę blisko! Powodzenia!
Thx za opublikowanie tego niesamowitego kodu. Pomogło mi to z moim zadaniem! Jednak nie bardzo rozumiem, jak to działa i dlaczego używacie dwóch trybów manipulacji (x i y) w konstruktorze strony głównej? Czy mógłbyś wyjaśnić swój kod krok po kroku? Dziękuję bardzo! Naprawdę staram się zrozumieć, co się dzieje. :) –
Piękny kawałek przeciągnij i upuść poza GridView. Zabawne jest to, że wciąż się porusza, gdy przesuniesz i zwolnisz, przynajmniej gdy ustawisz ManipulationMode = "All": D –
Czy ustawiłeś 'IsManipulationEnabled = true' na swojej siatce? –
Nie znalazłem żadnej takiej własności do siatki. Ustawiam właściwość ManipulationMode = "All" –
Przepraszam - myliłem się z siatką WPF - "Windows.UI.Xaml.Controls.Grid' ma manipulację włączoną przez" ManipulationMode ", jak powiedziałeś. –