在Xamarin Forms中拖放网格布局

时间:2018-09-27 04:58:15

标签: xamarin xamarin.forms

我在XAML页面中设置了以下网格布局。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<nav class="navbar navbar-mainnav">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Brand</a>
    </div>

    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">One more separated link</a></li>
          </ul>
        </li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>

我想在每个框视图上添加拖放功能。我的想法是,我应该能够将每个框视图拖放到具有相同列跨度的另一个框视图上。有人可以建议我如何做吗?请帮忙。

1 个答案:

答案 0 :(得分:1)

不幸的是,Xamarin.Forms中还没有内置的拖放功能。您仍然可以自己使用PanGestureRecognizer来实现它。

您可以向视图中添加手势识别器,例如Label,如下所示:

<Label>
   <Label.GestureRecognizers>
      <PanGestureRecognizer PanUpdated="PanGestureRecognizer_OnPanUpdated" />
   </Label.GestureRecognizers>
</Label>

现在,在PanUpdated事件处理程序中,您可以使用TranslationXTranslationY来移动视图。

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)
    }
}

要真正实现放置功能,您将必须手动测试对象当前位于何处以及是否允许放置。