C# 如何在codebehind中访问CommandParameter?

C# 如何在codebehind中访问CommandParameter?,c#,xaml,xamarin,xamarin.forms,C#,Xaml,Xamarin,Xamarin.forms,我正在使用ListView显示从API获得的“分配”的标题。单击作业后,我想转到此作业的详细信息页面。这里我想显示所选作业的描述和id。我几乎把这一切都弄明白了,但我被卡住了 我的XAML页面如下所示: <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microso

我正在使用ListView显示从API获得的“分配”的标题。单击作业后,我想转到此作业的详细信息页面。这里我想显示所选作业的描述和id。我几乎把这一切都弄明白了,但我被卡住了

我的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="App.Views.AssignmentListPage"
         Title="Opdrachten">
<ListView x:Name="AssignmentsListView" Margin="20">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Padding="20,0,0,0" HorizontalOptions="StartAndExpand" Orientation="Horizontal">
                    <Label Text="{Binding Title}" VerticalTextAlignment="Center">
                        <Label.GestureRecognizers>
                            <TapGestureRecognizer Tapped="OnLabelClicked" CommandParameter="{Binding .}"/>
                        </Label.GestureRecognizers>
                    </Label>
                    <!--<Label Text="{Binding Title}" VerticalTextAlignment="Center">
                        <Label.GestureRecognizers>
                            <TapGestureRecognizer Command="viewDetails" CommandParameter="{Binding .}"/>
                        </Label.GestureRecognizers>
                    </Label>-->
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
public partial class AssignmentListPage : ContentPage
{
    public AssignmentListPage ()
    {
        InitializeComponent ();
    }

    protected override async void OnAppearing()
    {
        base.OnAppearing();

        AssignmentsListView.ItemsSource = await App.AssignmentManager.GetAssignmentsAsync();

    }

    async void OnLabelClicked(object s, EventArgs e)
    {

        var a = e.ToString();

        var assignment = new Assignment
        {
            Id = "1234",
            Title = "Cross-Platform Application",
            Description = "What a beautiful description"
        };

        var assignmentDetailsPage = new AssignmentDetailsPage
        {
            BindingContext = assignment
        };

        await Navigation.PushAsync(assignmentDetailsPage);
    }


}
如您所见,我已经在
OnLabelClicked
函数中硬编码了id、标题和描述。我想使用
e.Parameter
获取这些数据,但这不是我可以选择的。我只能从
到字符串
等于
获取类型
中进行选择。但当我运行应用程序并检查
e
时,它确实包含“参数”。见图

我尝试过在XAML文件中使用
命令
而不是
点击
,但没有成功。我不知道该怎么做


我对这一切都不熟悉,我整天都在研究这个问题。我有一点隧道视力。我希望你们能给我指出正确的方向。

ListView
内置了
itemstapped
ItemSelected
事件,这些事件将执行您想要的操作

<ListView x:Name="AssignmentsListView" Margin="20" ItemSelected="OnItemSelect">

public void OnItemSelect(sender s, SelectedItemChangedEventArgs a) 
{
   // a.SelectedItem will be the selected Item, you need to cast it
   var item = (MyClass)a.SelectedItem;

   ...
}

public void OnItemSelect(发件人s,SelectedItemChangedEventArgs a)
{
//a.SelectedItem将是所选项目,您需要强制转换它
var item=(MyClass)a.SelectedItem;
...
}

列表视图
具有内置的
itemstapped
ItemSelected
事件,这些事件将执行您想要的操作

<ListView x:Name="AssignmentsListView" Margin="20" ItemSelected="OnItemSelect">

public void OnItemSelect(sender s, SelectedItemChangedEventArgs a) 
{
   // a.SelectedItem will be the selected Item, you need to cast it
   var item = (MyClass)a.SelectedItem;

   ...
}

public void OnItemSelect(发件人s,SelectedItemChangedEventArgs a)
{
//a.SelectedItem将是所选项目,您需要强制转换它
var item=(MyClass)a.SelectedItem;
...
}

您正在混合两种不同的用户交互模式,这两种模式本不打算混合。您可以按照@Jason的建议使用事件模式,这相当于许多UI框架(包括WinForms)中使用的事件驱动模式。它在基于XAML的框架(WPF、Xamarin等)中也得到了完全支持,但也值得了解命令模式

它是为更具声明性的使用模型而构建的,是应用程序开发的一部分。其思想是使用数据绑定在前端视图和支持它们的对象(在本例中称为视图模型)之间创建更松散的连接

在事件模式中,您将连接通过.NET委托调用的事件处理程序,而命令是从
ICommand
派生的类,这些类型的对象既用于执行所需的逻辑,也用于指示状态(当前是否可以调用命令)

在这种情况下,您需要将
ICommand
对象(通常是视图模型的可绑定属性)绑定到
tappesturerecognizer的
Command
属性以及要传递给
CommandParameter
属性的值。调用时,
CommandParameter
将传递给
Command
对象的
Execute
方法

有关更完整的代码示例,请参阅链接文章(特别是第一篇关于Xamarin命令的文章)


然而,如果您愿意,您确实可以回到久经考验的、真正的基于事件的交互模型。这两种模式各有优缺点。

您正在混合两种不同的用户交互模式,而这两种模式并不打算混合使用。您可以按照@Jason的建议使用事件模式,这相当于许多UI框架(包括WinForms)中使用的事件驱动模式。它在基于XAML的框架(WPF、Xamarin等)中也得到了完全支持,但也值得了解命令模式

它是为更具声明性的使用模型而构建的,是应用程序开发的一部分。其思想是使用数据绑定在前端视图和支持它们的对象(在本例中称为视图模型)之间创建更松散的连接

在事件模式中,您将连接通过.NET委托调用的事件处理程序,而命令是从
ICommand
派生的类,这些类型的对象既用于执行所需的逻辑,也用于指示状态(当前是否可以调用命令)

在这种情况下,您需要将
ICommand
对象(通常是视图模型的可绑定属性)绑定到
tappesturerecognizer的
Command
属性以及要传递给
CommandParameter
属性的值。调用时,
CommandParameter
将传递给
Command
对象的
Execute
方法

有关更完整的代码示例,请参阅链接文章(特别是第一篇关于Xamarin命令的文章)


然而,如果您愿意,您确实可以回到久经考验的、真正的基于事件的交互模型。每个都有优点和缺点。

非常感谢,伙计,你今天真的帮了我大忙!我不需要把东西绑在标签上之类的东西
ItemSelected
工作起来很有魅力!非常感谢,伙计,你今天真的帮了我大忙!我不需要把东西绑在标签上之类的东西
ItemSelected
工作起来很有魅力!谢谢正如我所说,我试着使用
ICommand
,但我被卡住了。我一定会读这些文章,因为我对这个主题感兴趣。谢谢!正如我所说,我试着使用
ICommand
,但我被卡住了。我一定会读那些文章,因为我对这个主题感兴趣。