【问题标题】:How do I bind a Command with Parameters to a Syncfusion Listview... while using Prism?如何在使用 Prism 时将带参数的命令绑定到 Syncfusion Listview...?
【发布时间】:2019-02-13 04:34:57
【问题描述】:

我正在使用带有 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>
  • 我是否可以在我的 ViewModel 中处理点击事件 (SelectionChanged)?

  • 我应该在我的模板中创建一个按钮,还是这是一个 hack?

【问题讨论】:

    标签: listview xamarin.forms prism syncfusion icommand


    【解决方案1】:

    我们最后检查了报告的查询“使用 PRISM 时 ViewModel 中的事件”。您可以使用 prism 库中提供的 EventToCommandBehavior 类来实现您的要求。

    代码 sn-p: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 命令绑定到 Behaviors 中的 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

    如果您想要在单独单击按钮时执行任何特定操作,您可以在模板中创建按钮。这不是黑客。您也可以根据元素进行自定义。

    【讨论】:

      【解决方案2】:

      我没有使用过同步融合控件,但是从文档中它似乎支持基本控件功能。

      您可以使用 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

      【讨论】:

        猜你喜欢
        • 2015-05-08
        • 2017-02-20
        • 1970-01-01
        • 2021-02-02
        • 2021-12-01
        • 2012-10-31
        • 2011-09-01
        • 2011-03-12
        • 2013-10-07
        相关资源
        最近更新 更多