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
Application
class. - Check the assignment to the
MainPage
property. To use stack-based navigation, this should be set to aNavigationPage
. The constructor of theNavigationPage
takes 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
ItemTapped
event on theListView
. You can do this in XAML or in code behind if the element has a name. - The
ItemTapped
event passes anItemTappedEventArgs
which has the actual object from the collection assigned to theItemsSource
property, however it needs to be cast. Recall that in this application, each data item is represented aFlag
object. Retrieve that object out from the event arguments and cast it to aFlag
. -
You have a property on your
FunFlactsViewModel
namedCurrentFlag
of typeFlag
which the details page uses to display the current flag. Set theFunFlactsViewModel.CurrentFlag
property to the flag from the event arguments. You'll need to retrieve the object from theDependencyService
. -
Use the
Page.Navigation
property to navigate to a new instance of the details page. Use thePushAsync
method so that you can use the C#async
andawait
support 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
OnItemTapped
method that is setting theCurrentFlag
property - 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.SelectedItem
property to connect it to theCurrentFlag
property. 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 theListView
and 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.