zoukankan      html  css  js  c++  java
  • Why does my ListView scroll to the top when navigating backwards?

    I’ve seen a few people asking this question. They have a page which contains a ListView and when an item is selected it navigates to another page. When they navigate backwards the ListView is back up to the top again. This behavior is due to the NavigationCacheMode of the page. By default the page will not cache rendered content when navigating “forward”. So when you navigate back to the page it re-renders the content. When displaying content like a ListView this will cause it to show the top content.

    navigation

    As with most things, there are a few solutions to this problem. The most common solution is to set the NaivationCacheMode to Enabled or Required.

    public ListPage()
    {
        this.InitializeComponent();
     
        this.NavigationCacheMode = NavigationCacheMode.Required;
    }

    These options do the following:

    MemberValueDescription
    Disabled 0

    The page is never cached and a new instance of the page is created on each visit.

    Required 1

    The page is cached and the cached instance is reused for every visit regardless of the cache size for the frame.

    Enabled 2

    The page is cached, but the cached instance is discarded when the size of the cache for the frame is exceeded.

    With this property set for the page the page content will not re-render because the rendered state has been cached!

    It does get a little annoying to set the property for every page. I like to use a base page class that contains all my navigation stuff and cache mode as well. This makes it much easier to do the basic stuff.

    public class AppPage : Page
    {
        public AppPage()
        {
            NavigationCacheMode = NavigationCacheMode.Enabled;
     
            // other stuff for navigation
        }
    }
     
    public partial class ListPage : AppPage
    {
        ...
    }

    Unfortunately this is not a dependency property so you cannot create a base style that sets this property.

    A second option is to use the ScrollIntoView method of the ListView. When your page loads, simply scroll your current item into view. This does have the drawback of not being at the exact same spot as before so I do recommend using the NavigationCacheMode.

    查看原文

  • 相关阅读:
    #256 (Div. 2)A. Rewards
    1113 矩阵快速幂
    1108 距离之和最小V2
    1287 加农炮
    1191 消灭兔子
    1051 最大子矩阵
    1086 背包
    1105 第K大的数
    2016 CCPC 网络赛 B 高斯消元 C 树形dp(待补) G 状压dp+容斥(待补) H 计算几何
    Educational Codeforces Round 18 C dp,思维 D lowbit,思维
  • 原文地址:https://www.cnblogs.com/qixue/p/4863705.html
Copyright © 2011-2022 走看看