Windows Phone 7 滚动视图控件的HorizontalOffset属性没有更新

3
我创建了一个快速示例,将图像放入ScrollViewer中,启动DispatcherTimer,并每200毫秒打印出ScrollViewer.HorizontalOffset。从示例中我注意到一些奇怪的行为-如果我通过小幅度滚动60个像素左右来拖动图像,则HorizontalOffset值永远不会更改。 ScrollViewer是否无法正确报告其位置的原因是什么?
编辑:我还尝试了在ScrollViewer中抓取ScrollBar(命名为“HorizontalScrollBar”)并检查其Value属性,但结果相同。
编辑2:看来这个错误只发生在Mango build 7712上(即使应用程序构建为7.0)。我将关闭此功能,并希望在最终版本中修复此问题。
示例代码。在我的机器上,即使进行大范围的图像拖动,也没有更新。我似乎只在120个或更多值的增量中得到更新。我希望至少每10-20像素获得更新。
<Grid x:Name="LayoutRoot" Background="Transparent">
        <ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" x:Name="Scroll">
            <Image Source="Jellyfish.jpg" Stretch="None"/>
        </ScrollViewer>
    </Grid>

MainPage.xaml.cs:

// Constructor
        public MainPage()
        {
            InitializeComponent();

            this.Loaded += (s, e) =>
                {
                    var scrollBar = Scroll.FindVisualChild("HorizontalScrollBar") as ScrollBar;
                    scrollBar.ValueChanged += (s1, e1) => Debug.WriteLine(DateTime.Now + " " + scrollBar.Value);
                };
        }

ExtensionMethods.cs:

public static class ExtensionMethods
    {
        public static FrameworkElement FindVisualChild(this FrameworkElement root, string name)
        {
            FrameworkElement temp = root.FindName(name) as FrameworkElement;
            if (temp != null)
                return temp;

            foreach (FrameworkElement element in root.GetVisualDescendents())
            {
                temp = element.FindName(name) as FrameworkElement;
                if (temp != null)
                    return temp;
            }

            return null;
        }

        public static IEnumerable<FrameworkElement> GetVisualDescendents(this FrameworkElement root)
        {
            Queue<IEnumerable<FrameworkElement>> toDo = new Queue<IEnumerable<FrameworkElement>>();

            toDo.Enqueue(root.GetVisualChildren());
            while (toDo.Count > 0)
            {
                IEnumerable<FrameworkElement> children = toDo.Dequeue();
                foreach (FrameworkElement child in children)
                {
                    yield return child;
                    toDo.Enqueue(child.GetVisualChildren());
                }
            }
        }

        public static IEnumerable<FrameworkElement> GetVisualChildren(this FrameworkElement root)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++)
                yield return VisualTreeHelper.GetChild(root, i) as FrameworkElement;
        }
    }

2
能展示一下你是如何获取偏移量的(代码)吗?我刚刚建立了一个快速示例,看起来它运行良好。 - Den
我刚刚测试了你的代码,对我来说它运行得很好。延迟可能是由于你获取快照的方式引起的。能否也分享一下这个部分呢? - Den
嗨Dennis - 快照代码是上面的“Debug.WriteLine()”。当您运行该示例时,您是否意味着它几乎在每个像素处更新?我只会在80-120个像素处获得更新。 - James Cadd
我在一个独立的应用程序中测试了你的代码,它运行得非常好。你的应用程序中还有其他处理吗? - Den
2个回答

3

2

我有使用过scrollviewer和horizontalOffset,你可能需要将开发工具更新至beta2版本以使其正常运行(在我的情况下,scrollviewer是beta版中已知的一个bug)。如果仍然无法成功,请尝试使用我的代码(对我有效):

    public MainPage()
    {
        InitializeComponent();
        if (someVariable == 0)
        {
            myPopup = new Popup() { IsOpen = true, Child = new AnimatedSplashScreen() };
            backroungWorker = new BackgroundWorker();
            RunBackgroundWorker();

            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(10);

            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();

            someVariable = 1;
        }


    }

    #region timer
    void timer_Tick(object sender, EventArgs e)
    {

        if (imagesScrollview.HorizontalOffset == (listBox.ActualWidth - 483))
            imagesScrollview.ScrollToHorizontalOffset(10);
       imagesScrollview.ScrollToHorizontalOffset(imagesScrollview.HorizontalOffset +1);
        current = imagesScrollview.HorizontalOffset + 1;

啊,谢谢指出第一个测试版中的错误。我会看看第二个测试版是否解决了这个问题。 - James Cadd
不幸的是,这似乎不是解决方案。最新的测试版仍存在问题。 - James Cadd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接