Load items asynchronously in ListView after navigating to Windows Phone 8.1 page
I have a Windows Phone 8.1 app in which I have a button that moves to PageTwo.xaml
. This one PageTwo.xaml.cs
in the code behind has something like this:
string type = "";
protected override void OnNavigatedTo(NavigationEventArgs e) {
type = e.Parameter.ToString();
}
private void Page_Loaded(object sender, RoutedEventArgs e) {
PageTwoListViewModel pageTwoListViewModel = ViewModelLocator.PageTwoListStatic;
this.DataContext = pageTwoListViewModel;
}
The reason I am setting DataContext
in the Page_Loaded event is because the project is ClassLibrary and I don't have the file App.xaml
, but that shouldn't affect anything related to my problem.
>
Then in mine PageTwoViewModel
I have the following:
public RelayCommand PageLoadedCommand { get; private set; }
public PageTwoListViewModel() {
this.PageLoadedCommand = new RelayCommand(PageLoaded);
}
private void PageLoaded() {
LoadList();
}
private async void LoadList() {
ObservableCollection<MyListModel> _list = await DatabaseService.GetList();
MyViewList = _list;
}
Code that causes PageLoadedCommand to run:
<Page (...)>
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Loaded">
<core:InvokeCommandAction Command="{Binding PageLoadedCommand}">
</core:InvokeCommandAction>
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
</Page>
The problem is that events OnNavigatedTo
and both Page_Loaded
are executed before the page is visible, so if I have a large list to populate, only after everything is done will it jump to PageTwo.xaml
, freezing the application.
I want to go to PageTwo.xaml
, and when there, start the loading animation and populate my ListView asynchronously. How can i do this?
source to share