如何通过单击数据模板列表视图中的项目打开另一个xamarin表单页面?

时间:2016-06-23 14:56:08

标签: c# xaml xamarin xamarin.forms

您好我正在开发一个包含多个xamrian表单页面的应用程序。我的主页面包含数据模板列表视图中的项目我需要帮助的是当用户点击数据模板列表视图中的项目时,它将它们带到不同的xamrian表单页面这里是我的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>

这是我背后的代码:

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;



        }

    }
}

和工具类:

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;
        }
    }
}

我知道有很多例子,但它们只适用于一页我需要多页面,所以任何帮助都会很棒

提前致谢!

1 个答案:

答案 0 :(得分:3)

我通常做的是处理OnItemSelected并将所选项目绑定到新页面。新页面将被推送到导航堆栈。

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);
}

如果工具的页面不同:

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);
}