Windows Phone 7 - ScrollViewer的值改变了

本文关键字:改变 ScrollViewer Phone Windows | 更新日期: 2023-09-27 17:54:52

我一直在寻找解决方案,但无法得到正确的解决方案。我有一个宽度为960的网格,其中有ScrollViewer。现在我想知道的值(水平偏移),我的滚动,而滚动。我找到的所有解决方案都是针对wpf/silverlight的,它对我不起作用。

编辑

好的,这里是示例代码,xaml:

<ScrollViewer Name="Scroll" LayoutUpdated="ScrollViewer_LayoutUpdated" IsEnabled="True" Width="480" ScrollViewer.HorizontalScrollBarVisibility="Auto">
    <Grid x:Name="ContentPanel" Background="Red" Margin="12,0,12,0" Width="960">
        <Rectangle Name="GreenRectangle" Fill="Green" Width="240" Height="240"></Rectangle>
    </Grid>
</ScrollViewer>
c#

private void ScrollViewer_LayoutUpdated(object sender, EventArgs e)
{
    GreenRectangle.Width = Scroll.HorizontalOffset;
    GreenRectangle.Height = Scroll.HorizontalOffset;
}

但问题是它并不是一直在改变大小。也许我的英语不好,你听不懂我说的话。这是一个电影的例子,我左右滑动,大小总是一样的。当我停止滑动时,它会改变大小。

https://www.dropbox.com/s/eh28oavxpsy19bw/20130122_1601_56.avi

Windows Phone 7 - ScrollViewer的值改变了

可以使用scrollviewer依赖属性,它有一个HorizontalOffset和一个VerticalOffset。诀窍是将事件绑定到滚动查看器,但它可以在加载事件处理程序中完成。如果你把一个宽网格在你的滚动查看器,你可以得到偏移!

在您的xaml文件中(这里是MainPage示例):

        <ScrollViewer Loaded="ScrollViewer_Loaded_1">
        <Grid x:Name="ContentPanel" Grid.Row="1" Width="1000" Margin="12,0,12,0">
            <StackPanel>
                ...

在你的代码后面的文件(MainPage.cs这里):

        public static readonly DependencyProperty ScrollViewVerticalOffsetProperty =
        DependencyProperty.Register(
                                    "ScrollViewVerticalOffset",
                                    typeof(double),
                                    typeof(MainPage),
                                    new PropertyMetadata(new PropertyChangedCallback(OnScrollViewVerticalOffsetChanged))
                                    );
        public static readonly DependencyProperty ScrollViewHorizontalOffsetProperty =
        DependencyProperty.Register(
                                    "ScrollViewHorizontalOffset",
                                    typeof(double),
                                    typeof(MainPage),
                                    new PropertyMetadata(new PropertyChangedCallback(OnScollViewHorizontalOffsetChanged))
                                    );
    private ScrollViewer _listScrollViewer;
    private void ScrollViewer_Loaded_1(object sender, RoutedEventArgs e)
    {
        _listScrollViewer = sender as ScrollViewer;
        Binding binding1 = new Binding();
        binding1.Source = _listScrollViewer;
        binding1.Path = new PropertyPath("VerticalOffset");
        binding1.Mode = BindingMode.OneWay;
        this.SetBinding(ScrollViewVerticalOffsetProperty, binding1);
        Binding binding2 = new Binding();
        binding2.Source = _listScrollViewer;
        binding2.Path = new PropertyPath("HorizontalOffset");
        binding2.Mode = BindingMode.OneWay;
        this.SetBinding(ScrollViewHorizontalOffsetProperty, binding2);
    }
    public double ScrollViewVerticalOffset
    {
        get { return (double)this.GetValue(ScrollViewVerticalOffsetProperty); }
        set { this.SetValue(ScrollViewVerticalOffsetProperty, value); }
    }
    public double ScrollViewHorizontalOffset
    {
        get { return (double)this.GetValue(ScrollViewHorizontalOffsetProperty); }
        set { this.SetValue(ScrollViewHorizontalOffsetProperty, value); }
    }
    private static void OnScrollViewVerticalOffsetChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        MainPage page = obj as MainPage;
        ScrollViewer viewer = page._listScrollViewer;
        // ... do something here
    }
    private static void OnScollViewHorizontalOffsetChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        MainPage page = obj as MainPage;
        ScrollViewer viewer = page._listScrollViewer;
        // ... do something here
    }

下面是我使用的XAML代码

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" LayoutUpdated='ContentPanel_LayoutUpdated'>
        <ScrollViewer x:Name='scroller' VerticalAlignment='Stretch' VerticalScrollBarVisibility='Visible' >
            <StackPanel
                x:Name='listItems'></StackPanel>
        </ScrollViewer>
    </Grid>
这是 背后的c#代码
private void ContentPanel_LayoutUpdated(object sender, EventArgs e)
    {
        var offset = scroller.VerticalOffset;
    }

每当滚动滚动时,网格(容器网格)的布局就会发生变化,因此会触发布局更新事件…请尝试通过在事件中放置断点并查找偏移值来调试。

将属性ManipulationMode="Control"添加到ScrollViewer中。这是必要的,因为否则UI线程将不会收到足够的ScrollViewer滚动值来获得流体动画的通知-正常模式是Windows Phone的性能优化,您需要绕过它!