【问题标题】:Custom ViewCell Contain Button Has Command And Binding To This Command自定义 ViewCell 包含按钮具有命令并绑定到该命令
【发布时间】:2019-03-04 20:28:41
【问题描述】:

我修复了一些项目中的一些自定义问题 其中一个我需要在分离的类和文件中使用自定义ViewCell 像这样:

<?xml version="1.0" encoding="UTF-8"?>
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          x:Class="HBRS.Controls.ViewCellTemplates.ArticleItemViewCell">
    <Image>
            <Image.GestureRecognizers>
                <TapGestureRecognizer   
                    Command="{Binding BindingContext.clickCommand, Source={x:Reference Name=mArt}}"
                    CommandParameter="{Binding .}" />
            </Image.GestureRecognizers>
    </Image>
</ViewCell>

mArt 是一个视图,它将命令用它做一些事情

之后,我在我的xamarin 页面中使用了这个视图单元格:

<ListView.ItemTemplate>
    <DataTemplate>
        <Cell:ArticleItemViewCell />
    </DataTemplate>
</ListView.ItemTemplate>

当我在我的设备上运行应用程序时,它抛出一个异常说 找不到为“mArt”引用的对象 所以我需要一些方法以相同的结果传递Source={x:Reference Name=mArt} 或进行交互,该命令将使其实现

【问题讨论】:

    标签: xamarin xamarin.android


    【解决方案1】:

    根据您所写的内容,我假设您使用 ViewCell 之类的东西有一个视图

    <ContentView ...
        x:Name="mArt">
        <ListView ...>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <templates:ArticleItemViewCell ... />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentView>
    

    现在您正尝试从您的ViewCell 引用该视图mArt。不幸的是,事情并非如此。 mArt 不是全局变量,而是视图类的成员(如果您对细节感兴趣,请查看在您的对象文件夹中创建的 .xaml.g.cs 文件)。

    ArticleItemViewCell 然而是一个不同的类,你不能简单地访问其他类的字段。 ArticleItemViewCellmArt 一无所知。虽然可能可以通过某种方式访问​​父级,但我建议你不要这样做,因为你往往会忘记这些细节,几个月后你会查看你的视图并想知道在哪里与细胞的交互被实施,直到你意识到细胞做了一些可疑的事情。它只会花费你时间。去过也做过。相信我。

    而是在你的 viewcell 中创建一个 Command 类型的可绑定属性,并从你的包含视图绑定到它

    在 ArticleItemViewCell.xaml.cs 中

    public static readonly BindableProperty TappedCommandProperty = BindableProperty.Create(nameof(TappedCommand), typeof(Command), typeof(ArticleItemViewCell)); 
    
    public Command TappedCommand
    {
        get => (Command)GetValue(TappedCommandProperty);
        set => SetValue(TappedCommandProperty, value);
    }
    

    现在您可以通过ArticleItemViewCell 绑定它们

    <ViewCell xmlns="http://xamarin.com/schemas/2014/forms" 
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              x:Class="HBRS.Controls.ViewCellTemplates.ArticleItemViewCell"
              x:Name="Cell">
        <Image>
                <Image.GestureRecognizers>
                    <TapGestureRecognizer   
                        Command="{Binding TappedCommand, Source={x:Reference Cell}}"
                        CommandParameter="{Binding .}" />
                </Image.GestureRecognizers>
        </Image>
    </ViewCell>
    

    从您的角度来看,您可以绑定 VM 的 clickCommand

    <ContentView ...
        x:Name="mArt">
        <ListView ...>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <templates:ArticleItemViewCell TappedCommand="{Binding Source={x:Reference mArt}, Path=BindingContext.clickCommand}" ... />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentView>
    

    我没有尝试确切的代码,但基本上这应该可以工作。

    请注意:ItemTapped 事件(see the docs) 与事件到命令行为(see here) 一起使用更具表现力,并且省去了额外的命令。

    【讨论】:

    猜你喜欢
    • 2018-08-04
    • 1970-01-01
    • 2013-10-09
    • 2013-09-16
    • 2019-05-11
    • 1970-01-01
    • 2016-11-22
    • 2016-06-25
    • 1970-01-01
    相关资源
    最近更新 更多