저는 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 페이지 사이를 전환합니다.
페이지가 이미로드가 완료되었습니다. 이것은 XAML입니다
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;
};
}
}
:이이 페이지에 대한 예입니다
<?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 사용 (새 Foo()); 항목 탭에서 Navigation.PushAsync (새 BarWhichDependsOnItem());을 탭합니다. –
@ Nitro.de 안녕하세요. 네가 이해하지 못하는 것 같아. 나는'App.MainPage' 전체를 단지 변경한다고 말한 적은 없습니다. 명확하게 보면,'MasterDetails.Detail =' – qakmak
도 있습니다. 버튼을 누른 후에 MasterDetailPage-Menu를 숨기려면 Navigation을 사용하십시오. .PushModalAsync 그래서 MasterMetal 다시 때까지 MasterDetail 숨기기 –