【问题标题】:How to disable ListView reload items when change page on Xamarin.Forms?如何在 Xamarin.Forms 上更改页面时禁用 ListView 重新加载项目?
【发布时间】:2016-10-22 13:19:54
【问题描述】:

我将 Xamarin.Forms 用于 UWP。

我有 2 页,我需要一些时间来切换它们:

// 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 

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

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());并且在 Item 点击时只需创建 Navigation.PushAsync(new BarWhichDependsOnItem());
  • @Nitro.de 嗨,朋友。看来你不明白。我从没说过我只改整个App.MainPage,如果你看清楚了,你会发现还有MasterDetails.Detail=
  • 如果你想在点击按钮后隐藏 MasterDetailPage-Menu,只需使用 Navigation.PushModalAsync 以便隐藏 MasterDetail,直到你再次 popModal
  • 我看到了,但App.Current.MainPage = StaticPage.Page2; 是我不明白的
  • @Nitro.de。我不能使用导航。因为那是不同的细节。我不能在这些页面之间使用导航更改。关于StaticPage我加代码

标签: c# .net xamarin xamarin.forms uwp


【解决方案1】:

原来这是一个来自 Xamarin.Forms 的 错误

查看详情:

ScrollViewer will reset the scroll position when navigate to exists page or back to the last page

UWP phone Navigate to exist page still very slow.

已经 1 年多了,即使有人确实发送了 pull request 来修复它,Xamarin 团队也没有合并代码。也许 Xamarin 团队在几年前就已经知道 Windows Phone 会死掉。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-29
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多