Exercise 2: Select a row (XAM280)
This exercise will continue building the Fun Flags ListView application. You'll add support to navigate to the provided details page when a flag is tapped in the ListView
To complete the exercise, you will need Visual Studio for Windows or macOS with the Xamarin development tools installed. You will also need either an emulator/simulator or a device to run the exercise on. Please see the setup page if you need help installing the Xamarin development environment.
Open the starter solution
This exercise is a continuation of the previous exercise. You can use your existing solution or begin from the prior Exercise 1 > Completed solution in your copy of the cloned or downloaded course materials.
Verify that we have a root NavigationPage
In this step, you'll verify that a NavigationPage is available to perform stack navigation. You'll use it to display the detail page.
- Open App.xaml.cs which holds your
Applicationclass. - Check the assignment to the
MainPageproperty. To use stack-based navigation, this should be set to aNavigationPage. The constructor of theNavigationPagetakes the first page to display (the "root").
Handle the selection of an item in the ListView
Next, you'll handle the selection for a single flag and provide navigation to a second (details) screen.
- Wire up to the
ItemTappedevent on theListView. You can do this in XAML or in code behind if the element has a name. - The
ItemTappedevent passes anItemTappedEventArgswhich has the actual object from the collection assigned to theItemsSourceproperty, however it needs to be cast. Recall that in this application, each data item is represented aFlagobject. Retrieve that object out from the event arguments and cast it to aFlag. -
You have a property on your
FunFlactsViewModelnamedCurrentFlagof typeFlagwhich the details page uses to display the current flag. Set theFunFlactsViewModel.CurrentFlagproperty to the flag from the event arguments. You'll need to retrieve the object from theDependencyService. -
Use the
Page.Navigationproperty to navigate to a new instance of the details page. Use thePushAsyncmethod so that you can use the C#asyncandawaitsupport to ensure you see exceptions. - Run the application and try tapping on an item to see it navigate to the details page. Use the arrow keys on the detail page to move to a different flag and then press the back button in the header. Notice that the original flag is still selected in the
ListView.
async void OnItemTapped(object sender, ItemTappedEventArgs e)
{
DependencyService.Get<FunFlactsViewModel>().CurrentFlag = (Flag)e.Item;
await this.Navigation.PushAsync(new FlagDetailsPage());
}
Bind to SelectedItem
The ListView has another property you can use to track and manage selection: SelectedItem. This property is also bindable; you can use it to connect to any property of the BindingContext, such as the CurrentFlag property! In fact, if you make the binding two-way, you can get it to update the state of the ListView automatically.
- Delete the code in the
OnItemTappedmethod that is setting theCurrentFlagproperty - you won't need it anymore; all you need now is the code to navigate to the second page. - Add a binding in the XAML for the
ListView.SelectedItemproperty to connect it to theCurrentFlagproperty. Make sure it's marked as a two-way binding, and then run the app again and change the current flag on the details page using the up/down buttons. Then back up to theListViewand note that it tracks the current selection!
<ListView ItemsSource="{Binding Flags}"
SelectedItem="{Binding CurrentFlag, Mode=TwoWay}"
ItemTapped="OnItemTapped" />
Exercise summary
In this exercise, you added support to detect selection within the ListView and navigate to a second screen.
You can view the completed solution in the Exercise 2 > Completed folder of your copy of the cloned or downloaded course materials.