使用Prism时如何将带有参数的命令绑定到Syncfusion Listview ...?

时间:2018-09-07 20:07:35

标签: listview xamarin.forms prism syncfusion icommand

我正在将Syncfusion Listview与Xamarin表单一起使用,并希望使用模型中的ICommand界面。

阅读help file for this control时,似乎可以指示我在控件本身上设置一个事件,然后在View中处理响应。

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?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

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

代码段:C#:用于在ViewModel中定义事件的命令。

group_by()

#EventArgs转换器在执行命令时返回ItemSelectionChangedEventArgs

library(tidyverse)
TestX %>% 
    gather("GP", "value", -Group,-Code, -Date) %>% 
    filter(!is.na(value)) %>% # gather the 3 columns into 1, and then compute
    arrange(Date,Group, Code) %>% 
    group_by(Date) %>% # group before compute the results, you can also group by Code
    mutate(Factor = sum(value)/3)

# A tibble: 12 x 6
# Groups:   Date [2]
  Group Date       Code  GP    value Factor
  <chr> <date>     <chr> <chr> <dbl>  <dbl>
 1 SH    1995-07-30 C3    SHRet     5   5.33
 2 SH    1995-07-30 C4    SHRet     5   5.33
 3 SL    1995-07-30 C5    SLRet     0   5.33
 4 SL    1995-07-30 C6    SLRet     1   5.33
 5 SM    1995-07-30 C1    SMRet     2   5.33
 6 SM    1995-07-30 C2    SMRet     3   5.33
 7 SH    1995-08-30 c10   SHRet     4   7
 8 SH    1995-08-30 C9    SHRet     3   7
 9 SL    1995-08-30 12    SLRet     3   7
10 SL    1995-08-30 c11   SLRet     2   7
11 SM    1995-08-30 C7    SMRet     4   7
12 SM    1995-08-30 C8    SMRet     5   7

代码段:XAML:EventToCommandBehavior将ViewModel命令绑定到“行为”中的SfListView SelectionChanged事件。

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)
    {

    }
}

我们已附上样本供您参考。您可以从以下位置下载该文件

链接:http://www.syncfusion.com/downloads/support/directtrac/general/ze/ListViewPrismMust1198452680

如果您希望任何单独单击按钮时应执行的特定操作,则可以在模板中创建按钮。不是黑客。您还可以根据元素进行自定义。