Windows Phone 7 ScrollViewer.Horizo​​ntalOffset 未更新

发布于 2024-11-27 09:54:13 字数 2783 浏览 1 评论 0 原文

我创建了一个快速示例,将图像放入 ScrollViewer 中,启动 DispatcherTimer,然后每 200 毫秒打印出 ScrollViewer.Horizo​​ntalOffset。从示例中我注意到一些奇怪的行为 - 如果我抓取图像并滚动少量(例如 60 像素左右),Horizo​​ntalOffset 值永远不会改变。 ScrollViewer 未正确报告其位置是否有原因?

编辑:我还尝试抓取 ScrollViewer 中的 ScrollBar (名为“Horizo​​ntalScrollBar”)并检查其 Value 属性,但得到相同的结果。

EDIT2:看来这个错误只发生在 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;
        }
    }

I created a quick example that places in image in a ScrollViewer, starts a DispatcherTimer and then prints out the ScrollViewer.HorizontalOffset every 200 ms. From the example I noticed some strange behavior - if I grab the image and scroll around by small amounts, say 60 pixels or so, the HorizontalOffset value never changes. Is there a reason that the ScrollViewer is not reporting its position correctly?

EDIT: I also tried grabbing the ScrollBar (named "HorizontalScrollBar") in the ScrollViewer and checking its Value property but I get the same results.

EDIT2: It appears this bug only happens on Mango build 7712 (even if the app is built for 7.0). I'll close this out and hope that its fixed in the final build.

Sample code. On my machine I can drag the image for large extents without getting an update. I seem to only get an update ever 120 or so increments of value. I would like to get the update at least every 10-20 pixels.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我也只是我 2024-12-04 09:54:13

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

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 的 ManipulationMode,如下所示:

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

The infrequent scroll event updating is part of the performance improvements in Mango:

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

The fix is to change the ScrollViewer's ManipulationMode as follows:

<ListBox ItemsSource="{Binding Items}" ScrollViewer.ManipulationMode ="Control" Height="652" Canvas.Top="80">
孤寂小茶 2024-12-04 09:54:13

我有使用scrollviewer和horizo​​ntalOffset的经验,你可以将你的开发工具更新到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;

I have experienced with scrollviewer and horizontalOffset, you may update your developer tool to beta2 to have it working (it was in my case, scrollviewer is a known bug in beta). If it's still no luck, try my code (work for me):

    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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文