Windows Phone 7 ScrollViewer.HorizontalOffset not updating

本文关键字:not updating HorizontalOffset ScrollViewer Phone Windows | 更新日期: 2023-09-27 18:04:17

我创建了一个快速示例,将图像放置在ScrollViewer中,启动DispatcherTimer,然后打印出ScrollViewer。HorizontalOffset每200毫秒。从这个例子中,我注意到一些奇怪的行为——如果我抓取图像并滚动一小部分,比如60像素左右,HorizontalOffset的值永远不会改变。是否有一个原因,ScrollViewer没有正确报告其位置?

编辑:我还尝试在ScrollViewer中抓取ScrollBar(名为"HorizontalScrollBar")并检查其值属性,但我得到了相同的结果。

EDIT2:似乎这个错误只发生在Mango构建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;
        }
    }

Windows Phone 7 ScrollViewer.HorizontalOffset not updating

不频繁的滚动事件更新是芒果性能改进的一部分:

http://blogs.msdn.com/b/slmperf/archive/2011/06/02/listbox-scrollviewer-performance-improvement-for-mango-and-how-it-impacts-your-existing-application.aspx

修复是改变ScrollViewer的操纵模式如下:

<ListBox ItemsSource="{Binding Items}" ScrollViewer.ManipulationMode ="Control" Height="652" Canvas.Top="80">

我有使用scrollviewer和horizontalOffset的经验,您可以将开发人员工具更新到beta2以使其工作(在我的情况下,scrollviewer是beta中的已知错误)。如果还是不行,试试我的代码:

    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;