我正在为UWP使用Xamarin.Forms。如何在Xamarin.Forms上更改页面时禁用ListView重新加载项目?
我有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页之间切换。
该页面已完成加载。这是该页面的例子:
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为什么你想改变整个App.MainPage,如果有人点击一个ListViewItem?使用MasterDetailPage.Detail = new NavigationPage(new Foo());并在项目点击只是做一个Navigation.PushAsync(新的BarWhichDependsOnItem()); –
@ Nitro.de嗨,朋友。看起来你不明白。我从来没有说过我只更改整个'App.MainPage',如果你看得清楚,你会发现还有'MasterDetails.Detail =' – qakmak
如果你想隐藏MasterDetailPage-菜单按钮被轻敲后使用导航.PushModalAsync,所以MasterDetail被隐藏,直到你再次popModal –