【发布时间】:2018-12-07 04:29:30
【问题描述】:
我有一个简单的 Xamarin.Forms 页面,其中包含 BoxViews 的网格布局。我希望能够通过滑动手势同时选择这些框视图。我怎样才能做到这一点?
我想建立一种瓦片地图,以便更容易选择框视图。我想轻松实现这一目标。只有滑动不是很好,因为只会选择“一个”框视图。
这是我目前所拥有的:
XAML
<ContentPage.Content>
<Grid x:Name="pageGrid" RowSpacing="1" ColumnSpacing="1" VerticalOptions="Center" HorizontalOptions="Center">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
</Grid>
</ContentPage.Content>
后面的代码:
public Page()
{
InitializeComponent();
int columnIndex = 0;
for (int rowIndex = 0; rowIndex <= 8; rowIndex++)
{
BoxView boxview = new BoxView { BackgroundColor = Color.White };
SwipeGestureRecognizer swipeGestureRecognizer = new SwipeGestureRecognizer();
swipeGestureRecognizer.Swiped += (sender, args) =>
{
if (boxview.BackgroundColor == Color.White)
{
boxview.BackgroundColor = Color.Gray;
}
else if (boxview.BackgroundColor == Color.Gray)
{
boxview.BackgroundColor = Color.White;
}
};
swipeGestureRecognizer.Threshold = 1;
swipeGestureRecognizer.Direction = SwipeDirection.Left | SwipeDirection.Right;
TapGestureRecognizer tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += (sender, args) =>
{
if (boxview.BackgroundColor == Color.White)
{
boxview.BackgroundColor = Color.Gray;
}
else if (boxview.BackgroundColor == Color.Gray)
{
boxview.BackgroundColor = Color.White;
}
};
boxview.GestureRecognizers.Add(swipeGestureRecognizer);
boxview.GestureRecognizers.Add(tapGestureRecognizer);
if (rowIndex == 4 && columnIndex == 3)
{
boxview.BackgroundColor = Color.Red;
}
pageGrid.Children.Add(boxview, columnIndex, rowIndex);
if (rowIndex != 8) continue;
if (columnIndex == 6) return;
columnIndex += 1;
rowIndex = -1;
}
}
现在每次滑动操作,只会选择一个 boxview!
【问题讨论】:
标签: xamarin.forms grid multi-select swipe-gesture