简体   繁体   中英

How to open another xamarin forms page from clicking a item in a data template list view?

Hello I am working on a app that has multiple xamrian forms pages. my main page consists of items in a data template list view what I need help with is when the user clicks a item in the data template list view it takes them to a different xamrian forms page here's my Xaml:

<?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="SchoolTools.SchoolToolsHome">
     <ListView x:Name="listView" HasUnevenRows="true">
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
          <Frame Padding="0,0,0,8" BackgroundColor="#d2d5d7">
            <Frame.Content>
              <Frame Padding="15,15,15,15"   OutlineColor="Gray" BackgroundColor="White">
                <Frame.Content>
                  <StackLayout Padding="20,0,0,0"  Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
                    <Label Text="{Binding Name}"
                           FontFamily="OpenSans-Light"
                           FontSize="24"/>
                  </StackLayout>
                </Frame.Content>
              </Frame>
            </Frame.Content>
          </Frame>
        </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>
</ContentPage>

Here's my code behind:

using System;
using System.Collections.Generic;

using Xamarin.Forms;

namespace AppName
{
    public partial class AppName : ContentPage
    {
        public AppName()
        {
            InitializeComponent();

            var name = new List<Tools>
            {
                     new Tools("netitem","Internet"),
                     new Tools("emailitem","E-Mail"),
                     new Tools("mathitem","Math"),
                     new Tools("sciitem","Science"),
                     new Tools("writeitem","Handwriting"),
                     new Tools("carditem","FlashCards"),
                     new Tools("bookitem","Books"),
        };

            listView.ItemsSource = name;



            Content = listView;



        }

    }
}

and the tools class:

using System;
namespace AppName
{
    public class Tools
    {
        public string Name { get; private set; }
        public string item { get; private set; }


        public Tools(string item, string name)
        {
            this.item = item;

            Name = name;



        }


        public override string ToString()
        {
            return Name;
        }
    }
}

I Know there are many examples out there but they are only for one page I need it for multiple pages so any help would be amazing

Thanks in advance!

Typically what I do is I handle OnItemSelected and bind the selected item to a new page. The new page is pushed onto the navigation stack.

void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
{
    var tools = e.SelectedItem as Tools;

    if (tools == null)
    {
        return;
    }

    var toolsView = new ToolsView();
    toolsView.BindingContext = tools;
    Navigation.PushAsync(toolsView);
}

If the page is different for the tools:

void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
{
    var tools = e.SelectedItem as Tools;

    if (tools == null)
    {
        return;
    }

    ContentPage page = null;

    switch (tools.Name)
    {
        case "Handwriting":
            page = new HandwritingView();
            break;
        case "Books":
            page = new BooksView();
            break;
        default:
            page = new ToolsView();
            break;
    }

    page.BindingContext = tools;
    Navigation.PushAsync(page);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM