我正在使用带有Xamarin表单的Syncfusion Listview,并希望使用位于我的模型中的ICommand
接口。
在阅读help file for this control时,它似乎指示我在控件本身上设置一个事件,然后在我的视图中处理响应。
Screen.xaml
<syncfusion:SfListView x:Name="listView" Grid.Row="1" ItemsSource="{Binding TrustAnchors}"
SelectionChanged="Handle_SelectionChanged" ItemSize="100" >
<syncfusion:SfListView.ItemTemplate>
<DataTemplate>
<Grid Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="0.4*" />
<RowDefinition Height="0.6*" />
</Grid.RowDefinitions>
<Label Text="{Binding Name}" FontAttributes="Bold" TextColor="Teal" FontSize="21" />
<Label Grid.Row="1" Text="{Binding Description}" TextColor="Teal" FontSize="15"/>
</Grid>
</DataTemplate>
</syncfusion:SfListView.ItemTemplate>
</syncfusion:SfListView>
我们在最后检查了报告的查询“使用PRISM时ViewModel中的事件”。您可以使用prism库中提供的EventToCommandBehavior类来实现您的要求。
代码段:C#:ViewModel中的事件定义命令。
public class MainPageViewModel : BindableBase, INavigationAware
{
private Command<ItemSelectionChangedEventArgs> selectionChangedCommand;
public Command<ItemSelectionChangedEventArgs> SelectionChanged
{
get { return selectionChangedCommand; }
set { selectionChangedCommand = value; }
}
public MainPageViewModel()
{
SelectionChanged = new Command<ItemSelectionChangedEventArgs>(OnSelectionChanged);
}
private void OnSelectionChanged(ItemSelectionChangedEventArgs eventArgs)
{
}
}
#EventArgs转换器在执行命令时返回ItemSelectionChangedEventArgs
public class EventArgs : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
object eventArgs = null;
if (value is Syncfusion.ListView.XForms.ItemSelectionChangedEventArgs)
eventArgs = value as Syncfusion.ListView.XForms.ItemSelectionChangedEventArgs;
return eventArgs;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
代码片段:XAML:EventToCommandBehavior将ViewModel命令绑定到“行为”中的SfListView SelectionChanged事件。
xmlns:behvaior="clr-namespace:Prism.Behaviors;assembly=Prism.Forms"
<ContentPage.Resources>
<ResourceDictionary>
<local:EventArgs x:Key="eventArgs" />
</ResourceDictionary>
</ContentPage.Resources>
<listView:SfListView x:Name="listView" ItemSize="70"
ItemsSource="{Binding ContactsInfo}">
<listView:SfListView.Behaviors>
<behvaior:EventToCommandBehavior EventName="SelectionChanged"
EventArgsConverter="{StaticResource eventArgs}"
Command="{Binding SelectionChanged}"/>
</listView:SfListView.Behaviors>
</listView:SfListView.ItemTemplate>
</listView:SfListView>
我们附上了样本供您参考。您可以从以下位置下载相同的内容
链接:http://www.syncfusion.com/downloads/support/directtrac/general/ze/ListViewPrismMust1198452680
如果您想要单独单击按钮时应该执行的任何特定操作,则可以在模板中创建按钮。这不是黑客行为。您也可以根据元素进行自定义。
我没有使用syncfusion控件,但是从文档中看来它似乎支持基本控制功能。
您可以使用EventToCommandBehavior - https://prismlibrary.github.io/docs/xamarin-forms/EventToCommandBehavior.html
从链接复制:
<ListView>
<b:EventToCommandBehavior EventName="ItemTapped"
Command="{Binding ItemTappedCommand}"
EventArgsParameterPath="Item" />
</ListView>
您将拥有该页面的以下命名空间。
xmlns:b="clr-namespace:Prism.Behaviors;assembly=Prism.Forms"
另外,为什么不使用DelegateCommand