ScrollIntoView不适用于Touch [英] ScrollIntoView doesn't work with Touch

查看:95
本文介绍了ScrollIntoView不适用于Touch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一张地图,上面有许多PushPins,并且在右下角列出了所有PushPins的列表(使用相同的视图模型)

So I have a map with a number of PushPins on it and a list down the right hand side listing all the PushPins (using the same view models)

当我单击地图上的大头针时,我想将该项目滚动到列表中的视图中.

When I click a pin on the map I want to scroll the item into view on the list.

我目前在Click和OnTouch上使用此代码:

I currently use this code on Click and OnTouch:

private void ScrollPushPin(Pushpin pushpin)
{
      ScrollViewer scrollViewer = GetScrollViewer(MyList) as ScrollViewer;
      scrollViewer.ScrollToBottom();
      var index = this.MyList.Items.IndexOf(pushpin);

      //index is never -1 so I would expect it to work?
      this.MyList.ScrollIntoView(pushpin); 
}

点击时:

void pp_MouseDown(object sender, MouseButtonEventArgs e)
{
    ScrollPushPin(sender as PushPin);
}

触摸时:

void pp_TouchDown(object sender, TouchEventArgs e)
{
    var pushpin = (Pushpin)sender;
    pushpin.CaptureTouch(e.TouchDevice);
}

void pp_TouchUp(object sender, TouchEventArgs e)
{
   var pushpin = (Pushpin)sender;
    if (pushpin != null && e.TouchDevice.Captured == pushpin)
    {
        pushpin.ReleaseTouchCapture(e.TouchDevice);
        ScrollPushPin(pushpin);
    }
}

虽然此代码适用于当我用鼠标单击图钉时Touch事件不会将PushPin滚动到视图中并且看不到原因的原因?

While this code works fine for when I click my pushpin with a mouse the Touch events don't scroll my PushPin into view and I can't see why?

我也尝试过:

I have also tried:

this.MyList.Dispatcher.Invoke((Action)(() => this.MyList.ScrollIntoView(pushpin)));

this.MyList.ScrollIntoView(this.MyList.Items[val]);

推荐答案

所以不要问我为什么这样做,而是在事件的末尾添加e.Handled = true可以解决问题:

So don't ask me why this works but adding e.Handled = true to the end of my events solved the problem:

void pp_TouchDown(object sender, TouchEventArgs e)
{
    var pushpin = (Pushpin)sender;
    pushpin.CaptureTouch(e.TouchDevice);
    e.Handled = true
}

void pp_TouchUp(object sender, TouchEventArgs e)
{
   var pushpin = (Pushpin)sender;
    if (pushpin != null && e.TouchDevice.Captured == pushpin)
    {
        pushpin.ReleaseTouchCapture(e.TouchDevice);
        ScrollPushPin(pushpin);
    }
    e.Handled = true
}

编辑

添加e.Handled = true会导致更多问题,因此我决定编写自己的ScrollIntoView

adding e.Handled = true caused more problems down the line so I decided to write my own ScrollIntoView

var val = this.MyList.Items.IndexOf(myObj);
if (val != -1)
{
    ScrollViewer scrollViewer = GetScrollViewer(MyList) as ScrollViewer;
    var itemHeight = scrollViewer.ExtentHeight / this.MyList.Items.Count;
    scrollViewer.ScrollToVerticalOffset(itemHeight * val);
}

//where
public static DependencyObject GetScrollViewer(DependencyObject o)
{
    if (o is ScrollViewer)
    { return o; }

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(o); i++)
    {
        var child = VisualTreeHelper.GetChild(o, i);

        var result = GetScrollViewer(child);
        if (result == null)
        {
            continue;
        }
        else
        {
            return result;
        }
    }

    return null;
}

这篇关于ScrollIntoView不适用于Touch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆