我为跟踪Microsoft文章Customizing View Cell的UWP列表视图中使用的视图单元创建了一个本机ViewCellRenderer。本文介绍了如何为可以使用本机控件显示的视图单元创建可绑定属性。我想要的是能够创建一个可绑定的事件处理程序,以便我可以使用本机UWP代码绑定此事件,例如Textblock的Tapped事件。
示例 - 在Textblock'Name'的tapped事件中,我想导航到另一个页面。导航逻辑写在我正在使用具有自定义视图单元的列表视图的页面的视图模型中。如何在自定义视图单元格中创建可绑定事件处理程序,以便我可以将其与本机代码的Tapped事件绑定。
我在Xamarin找到了similar case。我们的团队成员回答说,根据您的要求,关键点是使用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
方法中调用导航动作。