【问题标题】:How to open another xamarin forms page from clicking a item in a data template list view?如何通过单击数据模板列表视图中的项目打开另一个 xamarin 表单页面?
【发布时间】:2016-06-23 14:56:07
【问题描述】:

您好,我正在开发一个具有多个 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;
        }
    }
}

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

提前致谢!

【问题讨论】:

    标签: c# xaml xamarin xamarin.forms


    【解决方案1】:

    通常我所做的是处理 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);
    }
    

    【讨论】:

    • 感谢您的快速回答,您能否根据我的代码更深入地了解 OnItemSelected 部分?非常感谢:)
    • 这行得通,但是如果我想让一个标签转到一个页面而另一个标签转到一个完全不同的页面怎么办?感谢您迄今为止的所有帮助!
    • 根据您添加到“工具”的新 PageType 属性或其他内容做出决定。您在事件处理程序中拥有整个 Tools 对象。
    • 我仍然无法理解您所说的内容,您能给我举个例子吗?在此先感谢:)
    猜你喜欢
    • 2016-09-05
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多