簡體   English   中英

如何通過單擊數據模板列表視圖中的項目來打開另一個xamarin表單頁面?

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

您好,我正在開發一個具有多個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;
        }
    }
}

我知道那里有很多示例,但它們僅用於一頁,我需要用於多個頁面,因此任何幫助都將是驚人的

提前致謝!

通常,我要做的是處理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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM