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

问题描述 投票:1回答:1

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

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

listview xamarin.forms xamarin.uwp
1个回答
1
投票

我在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方法中调用导航动作。

© www.soinside.com 2019 - 2024. All rights reserved.