加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows-8 – 在Windows 8 / WinRT中实现DragStarted DragDelta

发布时间:2020-12-13 20:13:39 所属栏目:Windows 来源:网络整理
导读:如何将DragStarted DragDelta事件附加到 Windows 8 / WinRT中的网格中.我在Windows手机中用GestureService.GetGestureListener()方法做了同样的事情.我试图用ManipulationStarted在Windows 8中的ManipulationDelta事件但结果不一样.在windows手机中单次拖动,
如何将DragStarted DragDelta事件附加到 Windows 8 / WinRT中的网格中.我在Windows手机中用GestureService.GetGestureListener()方法做了同样的事情.我试图用ManipulationStarted&在Windows 8中的ManipulationDelta事件但结果不一样.在windows手机中单次拖动,它会进入DragDelta事件2次以上.但是另一方面在Windows 8中,在ManupulationDelta事件中,它只会对类似的拖动操作触发一次.
是的,我想我知道你想要什么.

假设你有一些这样的XAML:

<Grid Margin="50">
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Rectangle Fill="Blue" x:Name="MyRect" />
</Grid>

你想通过拖动它来移动这个矩形.

只需使用这段代码:

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

为了这:

希望我很亲近祝你好运!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读