Xamarin 带viewCell的导航命令

Xamarin 带viewCell的导航命令,xamarin,xamarin.ios,Xamarin,Xamarin.ios,我有一个自定义的ViewCell。我想知道是否可以添加一个命令,当单击ViewCell时,该命令将导航到另一个页面 以下是我目前的情况: public class MainMenuItem : ViewCell { public MainMenuItem(string text, string icon) { View = new StackLayout() { Spacing = 10, Paddi

我有一个自定义的
ViewCell
。我想知道是否可以添加一个命令,当单击
ViewCell
时,该命令将导航到另一个页面

以下是我目前的情况:

public class MainMenuItem : ViewCell
{
    public MainMenuItem(string text, string icon)
    {
        View = new StackLayout()
        {
            Spacing = 10,
            Padding = 10,
            Orientation = StackOrientation.Horizontal,
            VerticalOptions = LayoutOptions.Center,
            Children = {
                new Image() {
                    Source = ImageSource.FromFile(icon),
                    HorizontalOptions = LayoutOptions.Start,
                    HeightRequest = 30,
                    WidthRequest = 30,
                },
                new Label() {
                    Text = text,
                    HorizontalOptions = LayoutOptions.Start,
                    FontSize = 18,
                },
                 new Image() {
                    Source = ImageSource.FromFile("listitem_next.png"),
                    HeightRequest = 12,
                    HorizontalOptions = LayoutOptions.EndAndExpand
                }
            }
        };

        View = View;
    }
}
上面是我的视图单元。现在,我在
表格视图的表格部分中呈现这些内容。下面是代码:

TableView tvProfile = new TableView
{
    HasUnevenRows = true,
    Intent = TableIntent.Form,
    Root = new TableRoot {,
            new TableSection ("Emergency Contacts")
            {
                new MainMenuItem("Contacts", "icon_phone.png")
            },
            new TableSection ("Check in Timers")
            {
                new MainMenuItem("Timers", "icon_clock.png")
            },
            new TableSection ("Medical Information")
            {
                new MainMenuItem("Medcial Info", "icon_medicalkit.png")
            }
        }
};
我想要的是,当用户选择一个项目(查看单元格
)时,我想让用户导航到相应的页面

如何使用命令执行此操作?如果可能的话。我不熟悉使用命令,所以无论我在网络上得到什么,我都会不知所措


非常感谢您在这方面提供的任何帮助。

这里是一个快速实现。将
命令
命令参数
添加到
构造函数
,并添加调用此
命令的
手势识别器

public class MainMenuItem : ViewCell
{
    public MainMenuItem(string text, string icon, ICommand command,  Func<Page> commandParameterFunc)
    {
        View = new StackLayout()
        {
            ...
        };

        View.GestureRecognizers.Add(new TapGestureRecognizer { Command = command, CommandParameter = commandParameterFunc });
    }
}
公共类MainMenuItem:ViewCell { 公共MainMenuItem(字符串文本、字符串图标、ICommand命令、Func命令参数Func) { 视图=新StackLayout() { ... }; View.GestureRecognizers.Add(新的TapGestureRecognizer{Command=Command,CommandParameter=commandParameterFunc}); } }
然后执行以下更改-创建一个命令并向每个单元格添加2个参数。在这里定义命令、单击时发生的情况以及命令的参数,这是一个页面(但您不应该在这里创建它并检查错误)

更新:我更改为传递函数而不是对象,因此页面是在单击时创建的。还是有点脏;)

公共类MyPage:ContentPage
{
专用只读ICommand _navigateCommand;
公共MyPage()
{
_navigateCommand=new命令(commandParameter=>Navigation.PushModalAsync((commandParameter作为Func)());
TableView tvProfile=新的TableView
{
HasRows=true,
Intent=TableIntent.Form,
Root=新表根
{
新表节(“紧急联系人”)
{
新的主菜单项(“联系人”、“图标”\u phone.png)、\u导航命令,()=>新联系人页面()
},
新表节(“签入计时器”)
{
新的MainMenuItem(“计时器”、“图标”\u clock.png)、\u navigateCommand,()=>newtimerspage())
},
新表节(“医疗信息”)
{
新的主菜单项(“医疗信息”、“icon\u medicalkit.png”、“导航命令”(=>new MedicalInfo())
}
}
};
内容=tvProfile;
}
}

非常感谢您。我会设法把它清理一下。但再次感谢你把我推向任何方向。
public class MyPage : ContentPage
{
    private readonly ICommand _navigateCommand;

    public MyPage()
    {
        _navigateCommand = new Command(commandParamter => Navigation.PushModalAsync((commandParamter as Func<Page>)())));

        TableView tvProfile = new TableView
        {
            HasUnevenRows = true,
            Intent = TableIntent.Form,
            Root = new TableRoot 
            {
                new TableSection ("Emergency Contacts")
                {
                    new MainMenuItem("Contacts", "icon_phone.png", _navigateCommand, () => new ContactsPage())
                },
                new TableSection ("Check in Timers")
                {
                    new MainMenuItem("Timers", "icon_clock.png", _navigateCommand, () => new TimersPage())
                },
                new TableSection ("Medical Information")
                {
                    new MainMenuItem("Medcial Info", "icon_medicalkit.png", _navigateCommand, () => new MedicalInfoPage())
                }
            }
        };
        Content = tvProfile;
    }
}