在Xamarin表单中拖放网格布局
在我的XAML页面中设置了以下网格布局在Xamarin表单中拖放网格布局,xamarin,xamarin.forms,Xamarin,Xamarin.forms,在我的XAML页面中设置了以下网格布局 <Grid> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Grid.ColumnDefi
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<BoxView BackgroundColor="Red" Grid.Row="0" Grid.Column="0">
<BoxView.GestureRecognizers>
<PanGestureRecognizer PanUpdated="PanGestureRecognizer_OnPanUpdated"/>
</BoxView.GestureRecognizers>
</BoxView>
<BoxView BackgroundColor="Green" Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2" />
<BoxView BackgroundColor="Red" Grid.Row="0" Grid.Column="3" />
<BoxView BackgroundColor="Blue" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" />
<BoxView BackgroundColor="Purple" Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2"/>
<BoxView BackgroundColor="Aqua" Grid.Row="2" Grid.Column="0" />
<BoxView BackgroundColor="Fuchsia" Grid.Row="2" Grid.Column="1" />
<BoxView BackgroundColor="GreenYellow" Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="2">
</BoxView>
</Grid>
我想在每个框视图上添加拖放功能。我的想法是,我应该能够拖动每个长方体视图,并将其放到另一个具有相同列跨度的长方体视图上。有人能告诉我怎么做吗?请提供帮助。不幸的是,Xamarin.Forms中还没有内置的拖放功能。您仍然可以使用
pangestureerecognizer
自己实现它
您可以将手势识别器添加到视图中,例如标签
,如下所示:
<Label>
<Label.GestureRecognizers>
<PanGestureRecognizer PanUpdated="PanGestureRecognizer_OnPanUpdated" />
</Label.GestureRecognizers>
</Label>
要真正实现拖放功能,您必须手动测试对象当前所在的位置,以及是否允许在该位置拖放。是否适用于方框视图?如何添加PanGesture?如果我想交换这两个
。。我该怎么做?您可以将pangestrerecognizer
添加到BoxView
中,方法与标签所示的方法相同-只需使用BoxView.gestreecognizers
。是。它会移动长方体视图。但我想把它放到另一个方块视图上。它将替换我放置它的长方体视图。我如何才能做到这一点?您必须在我标记为//handle drop的位置手动处理此问题。您必须检查当前位置是否在另一个长方体视图上方(通过比较坐标),如果是,您将重置平移,然后手动将元素移动到该位置(通过正确设置网格.Column
和网格.Row
值),我已将PanGesture应用于特定的长方体视图(红色的)。但整个网格正在移动。我怎样才能防止这种情况?我希望红色框视图叠加在水族框视图上。你能给个建议吗?
private double _startTranslationX, _startTranslationY
private void PanGestureRecognizer_OnPanUpdated(object sender,
PanUpdatedEventArgs e)
{
var box = (BoxView) sender;
if (e.StatusType == GestureStatus.Started)
{
_startTranslationX = box.TranslationX;
_startTranslationY = box.TranslationY;
}
else if (e.StatusType == GestureStatus.Running)
{
box.TranslationX = _startTranslationX + e.TotalX;
box.TranslationY = _startTranslationY + e.TotalY;
}
else if (e.StatusType == GestureStatus.Completed)
{
box.TranslationX = _startTranslationX + e.TotalX;
box.TranslationY = _startTranslationY + e.TotalY;
//handle drop here (depending on your requirements)
}
}