Scrollviewer ChangeView功能无法滚动到底部

时间:2015-08-05 12:32:00

标签: xaml listview windows-runtime windows-phone-8.1 scrollviewer

我在windows phone 8.1 rt app中使用了scrollviewer里面的listview。 我需要滚动到列表底部,但更改视图功能不符合要求。

为同一场景创建了一个示例:

MainPage.xaml中

<Page
x:Class="InfiList.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:InfiList"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:customlv="using:InfiList"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.Resources>
    <Style x:Key="ListViewStyle1" TargetType="ListView">
        <Setter Property="IsTabStop" Value="False"/>
        <Setter Property="TabNavigation" Value="Once"/>
        <Setter Property="IsSwipeEnabled" Value="True"/>
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="VerticalContentAlignment" Value="Top"/>
        <Setter Property="ItemContainerTransitions">
            <Setter.Value>
                <TransitionCollection>
                    <AddDeleteThemeTransition/>
                    <ReorderThemeTransition/>
                </TransitionCollection>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <ItemsStackPanel Orientation="Vertical"/>
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListView">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                        <ItemsPresenter FooterTransitions="{TemplateBinding FooterTransitions}" FooterTemplate="{TemplateBinding FooterTemplate}" Footer="{TemplateBinding Footer}" HeaderTemplate="{TemplateBinding HeaderTemplate}" Header="{TemplateBinding Header}" HeaderTransitions="{TemplateBinding HeaderTransitions}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Page.Resources>

<Grid x:Name="root">
    <ScrollViewer x:Name="MyScrollViewer" ViewChanged="OnViewChanged" IsVerticalScrollChainingEnabled="True">
        <ListView x:Name="listview" ItemsSource="{Binding Collection}" Style="{StaticResource ListViewStyle1}" >
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                    <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                </Style>
            </ListView.ItemContainerStyle>

            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <ItemsStackPanel ItemsUpdatingScrollMode="KeepItemsInView"/>
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid Background="LightGray" HorizontalAlignment="Left" Margin="25">
                        <TextBlock Margin="25 25 25 50" FontSize="32" Text="{Binding}" TextWrapping="WrapWholeWords" MaxWidth="200"/>
                    </Grid>

                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ScrollViewer>
</Grid>
<Page.BottomAppBar>
    <CommandBar>
        <AppBarButton Click="AppBarButton_Click">

        </AppBarButton>
    </CommandBar>
</Page.BottomAppBar>

MainPage.xaml.cs中

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at    http://go.microsoft.com/fwlink/?LinkId=391641

namespace InfiList
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{

    public ObservableCollection<String> Collection { get; set; }

    private bool incall;
    private int offset;
    int _noofelements;

    public MainPage()
    {
        this.InitializeComponent();
        Collection = new ObservableCollection<string>();
        listview.DataContext = this;


        this.NavigationCacheMode = NavigationCacheMode.Required;

        addNumber(0);
        listview.Loaded += listview_Loaded;
    }

    void listview_Loaded(object sender, RoutedEventArgs e)
    {
        MyScrollViewer.ChangeView(null, int.MaxValue, null, true);

    }

    private void OnViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
    {
        ScrollViewer view = (ScrollViewer)sender;
        Debug.WriteLine("Vertica Pffset: " + view.VerticalOffset);

        if ((view.VerticalOffset < 0.1 * view.ScrollableHeight) & !incall)
        {
            incall = true;
            addNumber(++offset);
        }
    }

    private void addNumber(int offset)
    {
        int scrollcount = 30;
        int start = offset * scrollcount;
        for (int i = start; i < start + scrollcount; i++)
        {
            string s= (_noofelements++).ToString() ;
            if (i % 2 == 0)
                s += "msdkd kmsdksdk kdsmkd skmcds ckdsmckds ckdsmcksd kcmdskcdsc kdmmcsckdsc" + Environment.NewLine + "sdjndsjnds"
                    + "msdkd kmsdksdk kdsmkd skmcds ckdsmckds ckdsmcksd kcmdskcdsc kdmmcsckdsc" + Environment.NewLine + "sdjndsjnds"
                   + "dssdsddsds";

            Collection.Insert(0,s);
        }

        incall = false;
    }

    /// <summary>
    /// Scrolls to bottom
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void AppBarButton_Click(object sender, RoutedEventArgs e)
    {
        MyScrollViewer.ChangeView(null, int.MaxValue, null, true);
        //MyScrollViewer.ScrollToVerticalOffset(int.MaxValue);
        //For scrolling till top
        //MyScrollViewer.ScrollToVerticalOffset(0);
    }
}
}

有没有办法实现所需的功能?

3 个答案:

答案 0 :(得分:3)

使用scrollviewer的ExtentHeight
scrollViewer.ChangeView(null,scrollViewer.ExtentHeight,null);

示例:localStorage

答案 1 :(得分:0)

当我使用带有更改视图的滚动查看器时,我遇到了类似的问题。它只是从未真正到达底部。出乎意料的是,我发现了一个名为 UpdateLayout() 的函数。我在更改视图功能之前使用了这个功能,它终于奏效了。

答案 2 :(得分:-1)

尝试使用ListView.ScrollIntoView, 例如:

listView.ScrollIntoView(listView.Items.Last(),ScrollIntoViewAlignment.Leading);