Xamarin : OnTapped navigation to new page
- Add listview in page
<?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.MainMaster"
Title="My Application">
<ListView x:Name="MainList" ItemTapped="OnTapped">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding Name}"></Label>
<Label Text="{Binding Description}"></Label>
<Label Text="{Binding OrderNumber}"></Label>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
- Write a code to bind listview
public partial class MainMaster : ContentPage
{
public MainMaster()
{
InitializeComponent();
MainList.ItemsSource = new List<ListViewTemplate>
{
new ListViewTemplate { Name="One", Description="One", OrderNumber=1 },
new ListViewTemplate { Name="Two", Description="Two", OrderNumber=2 },
new ListViewTemplate { Name="Three", Description="Three", OrderNumber=3 }, new ListViewTemplate { Name="Four", Description="Four", OrderNumber=4 },
new ListViewTemplate { Name="Five", Description="Five", OrderNumber=5 },
};
}
}
- Add new page, I set name "NewPage"
async void OnTapped(object sender, ItemTappedEventArgs e)
{
var selected = e.Item as ListViewTemplate;
switch (selected.OrderNumber)
{
case 1:
await Navigation.PushAsync(new NewPage());
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
//Set not to selected item : optional
((ListView)sender).SelectedItem = null;
}
- Set page in to run the application
public class App : Application
{
public App()
{
MainPage = new NavigationPage(new MainMaster());
}
}
- Run the application
No comments:
Post a Comment