如何在Xamarin.Forms中切换页面时禁用ListView重新加载项目?

4
我是使用Xamarin.Forms进行UWP开发。 我有两个页面,需要在它们之间切换。
// Change main page
App.Current.MainPage = otherPage; // this is not a new page, already exists 
// Navigate
App.Current.MainPage.Navigation.PushAsync(otherPage); // this is not a new page, already exists
// Change detail page
MasterDetailsPage.Detail = otherPage; // this is not a new page, already exists 

页面,只需在两个页面之间切换。

otherPage是已经完成加载的页面。这是此页面的示例:

  public partial class Page1 : ContentPage
    {
        public Page1 ()
        {
            InitializeComponent ();

            ObservableCollection<StateModel> strList = new ObservableCollection<StateModel>();
            for (int i = 1; i < 100; i++)
            {
                strList.Add(new StateModel() { Price = i, ProductName = "Product  " + i });
            }

           btn.Clicked += (sender, args) =>
           {
              // change to another page which already finish load
              App.Current.MainPage = StaticPage.Page2;
          };

        }

    }

以下是XAML代码:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App1.Page1">

  <Grid>
    <StackLayout>
      <Label  Margin="6,20,0,0" Text="scroll items and click go to other page and return you will find the listview reset: scroll on the top(I only change the App.Current.MainPage, not create new page)"/>

      <Button x:Name="btn" Text="Go" />

      <ListView x:Name="lvList"  Margin="0,10,0,0" ItemsSource="{Binding }" IsRefreshing="False">
        <ListView.ItemTemplate>
          <DataTemplate>
            <ViewCell>
              <Grid Padding="10" HorizontalOptions="FillAndExpand">
                <Grid.ColumnDefinitions>
                  <ColumnDefinition/>
                  <ColumnDefinition Width="50"/>
                  <ColumnDefinition Width="100"/>
                </Grid.ColumnDefinitions>
                <Label Text="{Binding ProductName}" HorizontalOptions="FillAndExpand" />
                <Label Grid.Column="1" Text="{Binding Price}" Margin="10,0" />
                <Label Text="" Grid.Column="2" HorizontalOptions="EndAndExpand" Margin="0,0,10,0">
                  <Label.Triggers>
                    <DataTrigger TargetType="Label" Binding="{Binding Price}" Value="90">
                      <Setter Property="Text" Value=">>>>>"/>
                    </DataTrigger >
                  </Label.Triggers>
                </Label>
              </Grid>
            </ViewCell>
          </DataTemplate>
        </ListView.ItemTemplate>
      </ListView>
    </StackLayout>
  </Grid>
</ContentPage>

但是当页面切换时,它会重新加载 ListView 的项,这些项逐一添加(就像某种动画)。

例如,每次在切换页面时显示 ListView 时,它会执行以下操作:

ListView.Clear();
ListView.Add();
ListView.Add();
ListView.Add();
// -- continue add 100 items --

那样会使页面加载速度非常慢,对我来说是一个大的性能问题。而且在滚动前会丢失滚动位置,它会滚动到顶部(因为它会清除并重新添加所有项目)。另外,我不知道上次选择哪个项目,它会丢失。
有没有什么方法可以解决这个问题呢?谢谢。
更新:
public static class StaticPage
{
        public static Page1 Page1;
        public static Page2 Page2;


        public static void Init()
        {
            Page1 = new Page1();
            Page2 = new Page2();
        }
}

有些朋友需要这个,所以我加上了它。 我为什么要用它呢?只是为了让你理解,我从未手动重新加载ListView项目,我只创建页面一次。


如果您使用MasterDetailPage,为什么要在某个ListViewItem上点击时更改整个App.MainPage呢?使用MasterDetailPage.Detail = new NavigationPage(new Foo()); 并在项目点击时只需进行Navigation.PushAsync(new BarWhichDependsOnItem())即可。 - Nitro.de
如果您想在点击按钮后隐藏MasterDetailPage菜单,只需使用Navigation.PushModalAsync即可,这样MasterDetail就会被隐藏,直到您再次弹出模态窗口。 - Nitro.de
@TryToSolveItSimple 我们在谈论 Xamarin.Forms - qakmak
@TryToSolveItSimple 我只是尝试将它添加到 UWP 项目的 MainPage 上。但是与我的代码没有任何区别。App.Current.MainPage = somePage - qakmak
你在哪里设置页面的BindingContext?我也觉得有点奇怪,你在ItemSource中只使用{Binding}而没有任何属性。如果你使用了Bindings,为什么要在页面构造函数中填充一个数组呢? - Thomas
显示剩余7条评论
1个回答

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接