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?

+3


source to share


1 answer


As it turns out, my problem was coming from a pretty silly thing. Mine was DatabaseService.GetList();

not completely asynchronous and that was what caused the problem, so the above implementation works very well as long as you ensure that the database calls are asynchronous.



+1


source







All Articles