在自定义视图单元格中创建可绑定事件处理程序

Create bindable event handler in custom view cell

我已经按照 Microsoft 文章 Customizing View Cell 为 UWP 的列表视图中使用的视图单元创建了本机 ViewCellRenderer。 本文解释了如何为可以使用本机控件显示的视图单元格创建可绑定属性。我想要的是能够创建一个可绑定的事件处理程序,以便我可以将此事件与本机 UWP 代码绑定,例如 Textblock 的 Tapped 事件。

示例 - 在文本块 'Name' 的点击事件中,我想导航到另一个页面。导航逻辑是在我使用带有自定义视图单元格的列表视图的页面的视图模型中编写的。如何在自定义视图单元格中创建可绑定事件处理程序,以便我可以将其与本机代码的 Tapped 事件绑定。

我发现 similar case in Xamarin. And our team member has replied, For your requirement, the key point is using XamlBehaviors 将 TextBlock 的 Tapped 事件与自定义视图单元格中定义的命令绑定。

Xaml

<Application.Resources>
    <ResourceDictionary>
        <DataTemplate x:Key="ListViewItemTemplate">
            <Grid>                
                <TextBlock Text="{Binding TextName}" >
                <interactivity:Interaction.Behaviors>
                    <core:EventTriggerBehavior EventName="Tapped">
                        <core:InvokeCommandAction  Command="{Binding TappedCommand}"/>
                    </core:EventTriggerBehavior>        
                </interactivity:Interaction.Behaviors>
                </TextBlock>
            </Grid>
        </DataTemplate>
    </ResourceDictionary>
</Application.Resources>

然后您可以在 TappedCommand 方法中调用导航操作。