我是使用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项目,我只创建页面一次。
Xamarin.Forms
。 - qakmakApp.Current.MainPage = somePage
- qakmak