以编程方式滚动始终不起作用

时间:2018-07-09 11:23:08

标签: c# multithreading uwp uwp-xaml

XAML

<Page
    x:Class="ScrollViewWithDifferentTypeOfContent.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ScrollViewWithDifferentTypeOfContent"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid Height="300">
        <ScrollViewer Name="MainScrollViewer"
                      IsVerticalRailEnabled="True"
                      VerticalScrollMode="Enabled"
                    >
            <StackPanel>
                <Image Source="/Assets/icon0.png" />
                <TextBlock Name="BlockyThing">  HELLO WORLD</TextBlock>
                <ListView Name="MyListView" ItemsSource="{x:Bind obsList}" Loaded="Page_Loaded">
                    <ListView.ItemTemplate>
                        <DataTemplate x:DataType="local:Item">
                            <StackPanel>
                                <TextBlock  Text="{x:Bind Message}"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </StackPanel>
        </ScrollViewer>
    </Grid>
</Page>

xaml.cs中的代码

  public static void ScrollToElement(this ScrollViewer scrollViewer, UIElement element,
        bool isVerticalScrolling = true, bool smoothScrolling = true, float? zoomFactor = null)
    {
        var transform = element.TransformToVisual((UIElement)scrollViewer.Content);
        var position = transform.TransformPoint(new Point(0, 0));

        if (isVerticalScrolling)
        {
            scrollViewer.ChangeView(null, position.Y, zoomFactor, !smoothScrolling);
        }
        else
        {
            scrollViewer.ChangeView(position.X, null, zoomFactor, !smoothScrolling);
        }
    }



namespace ScrollViewWithDifferentTypeOfContent
{
   public class Item
    {
        public string ItemName;
        public string Message;
        public static int itemNo = 0; 

        public Item()
        {
            Message = (itemNo).ToString() +  "  HELLO HELLO HELLO!!";
            ItemName = "Item" + (itemNo++);
            Debug.WriteLine(ItemName);
        }
    }

    public sealed partial class MainPage : Page
    {

        public ObservableCollection<Item> obsList = new ObservableCollection<Item>();

        public MainPage()
        {
            for(int i=0; i<40; i++)
                obsList.Add(new Item());            
            this.InitializeComponent();
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            CoreDispatcher dispatcher = Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher;

            var ignored = dispatcher.RunAsync(CoreDispatcherPriority.High, () => {
            var TEST3 = MyListView.ContainerFromIndex(4);  
            MainScrollViewer.ScrollToElement((UIElement) TEST3);
            MainScrollViewer.UpdateLayout();          
            });
        }
    }
}

嗨,在这段代码中,ScrollViewer中有一个图像,一个文本框和一个列表视图,我试图滚动到listView(MyListView)中的一个项目,此代码有效,但是,有时它会滚动对于指定的项目,有时并非如此,我知道它可能与线程有关,但是我希望该操作始终在页面中的每个元素加载之后进行。

为什么要这个:

我需要通过滚动页面来实现导航,

页面在列表视图之前有几个元素,但是我将需要相对于列表视图元素滚动。

如何确保滚动始终发生。

1 个答案:

答案 0 :(得分:1)

不建议在ScrollViewer中使用ListView,因为这会破坏列表的虚拟化(将呈现所有40个项目)。

您可以使用HeaderTemplate获得相同的视觉效果:

<Grid Height="300">
    <ListView Name="MyListView"
              ItemsSource="{x:Bind obsList}"
              Loaded="Page_Loaded">
        <ListView.HeaderTemplate>
            <DataTemplate>
                <StackPanel>
                    <Image Source="/Assets/icon0.png" />
                    <TextBlock Name="BlockyThing">  HELLO WORLD</TextBlock>
                </StackPanel>
            </DataTemplate>
        </ListView.HeaderTemplate>
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="local:Item">
                <StackPanel>
                    <TextBlock  Text="{x:Bind Message}" />
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

然后,您可以从代码隐藏中调用ScrollIntoView方法,该方法可能更强大:

MyListView.ScrollIntoView(obsList[4]);