我在我的Windows Phone面临的一个问题8.1应用,在那里,如果我加入的ListView的的ItemSource元素,列表视图重置其滚动位置。
为了解释,如果我滚动到列表视图的底部,并插入到绑定OC顶部的元件,列表视图跳转到自动顶部。
我希望它保持原样。 如何解决这一问题?
码
后面的代码
public sealed partial class MainPage : Page
{
public ObservableCollection<String> Collection = new ObservableCollection<string>();
private bool incall;
private int offset;
int _noofelements;
ScrollViewer _scrollViewer;
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
listview.ItemsSource = Collection;
addNumber(0);
}
public static ScrollViewer GetScrollViewer(DependencyObject depObj)
{
if (depObj is ScrollViewer) return depObj as ScrollViewer;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
var child = VisualTreeHelper.GetChild(depObj, i);
var result = GetScrollViewer(child);
if (result != null) return result;
}
return null;
}
private void OnViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
ScrollViewer view = (ScrollViewer)sender;
if ((view.VerticalOffset > 0.9 * view.ScrollableHeight) & !incall)
{
incall = true;
addNumber(++offset);
}
}
private void addNumber(int offset)
{
int start = offset * 20;
for (int i = start; i < start + 20; i++)
{
if (i % 20 == 0)
Collection.Insert(0, (_noofelements++).ToString());
else
Collection.Add((_noofelements++).ToString());
}
incall = false;
}
private void listview_Loaded(object sender, RoutedEventArgs e)
{
_scrollViewer = GetScrollViewer(listview);
if (_scrollViewer != null)
{
_scrollViewer.ViewChanged += OnViewChanged;
}
}
}
UI
<Grid>
<ListView x:Name="listview" Loaded="listview_Loaded">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel ItemsUpdatingScrollMode="KeepItemsInView"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Margin="25" FontSize="32" Text="{Binding}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>