Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式滚动始终不起作用_C#_Multithreading_Uwp_Uwp Xaml - Fatal编程技术网

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

C# 以编程方式滚动始终不起作用,c#,multithreading,uwp,uwp-xaml,C#,Multithreading,Uwp,Uwp Xaml,XAML 你好,世界 xaml.cs中的代码 <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:Scro

XAML


你好,世界
xaml.cs中的代码

<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>
公共静态无效ScrollToElement(此ScrollViewer ScrollViewer,UIElement元素,
bool isVerticalScrolling=true,bool smoothScrolling=true,float?zoomFactor=null)
{
var transform=element.TransformToVisual((UIElement)scrollViewer.Content);
变量位置=transform.TransformPoint(新点(0,0));
如果(垂直滚动)
{
scrollViewer.ChangeView(null,position.Y,zoomFactor,!smoothScrolling);
}
其他的
{
scrollViewer.ChangeView(位置.X,null,zoomFactor,!smoothScrolling);
}
}
具有不同内容类型的名称空间滚动视图
{
公共类项目
{
公共字符串ItemName;
公共字符串消息;
公共静态int itemNo=0;
公共项目()
{
Message=(itemNo.ToString()+“你好!!”;
ItemName=“Item”+(itemNo++);
Debug.WriteLine(ItemName);
}
}
公共密封部分类主页面:第页
{
公共ObservableCollection obsList=新ObservableCollection();
公共主页()
{
对于(int i=0;i{
var TEST3=MyListView.ContainerFromIndex(4);
ScrollToElement((UIElement)TEST3);
MainScrollViewer.UpdateLayout();
});
}
}
}
您好,在这段代码中,ScrollViewer中有一个图像、一个文本框和一个列表视图,我正在尝试滚动到listView(MyListView)中的一个项目,这段代码可以工作,但是,有时它会滚动到指定的项目,有时它不会,我知道它可能与线程有关,但是,我希望该操作总是在页面中的每个元素加载后发生

我为什么想要这个:

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

页面在listview之前有几个元素,但是我需要相对于listview元素滚动


如何确保滚动始终发生。

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

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

  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();          
            });
        }
    }
}
<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>
MyListView.ScrollIntoView(obsList[4]);