自定义 ViewCell 包含按钮具有命令并绑定到此命令 [英] Custom ViewCell Contain Button Has Command And Binding To This Command
问题描述
我修复了一些项目中自定义的一些问题其中之一我需要在分离的类和文件中使用自定义 ViewCell
像这样:
I fix customize some problems in some projects
one of them that I need to use custom ViewCell
in Separated class and file
like that:
<?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
是视图,它将命令用它做一些事情
where mArt
is the view that will command make some thing with it
之后,我在我的 xamarin
页面上使用了这个视图单元:
after that I used this view cell in on of my xamarin
pages like that:
<ListView.ItemTemplate>
<DataTemplate>
<Cell:ArticleItemViewCell />
</DataTemplate>
</ListView.ItemTemplate>
当我在我的设备上运行应用程序时,它抛出一个异常,说找不到为mArt"引用的对象所以我需要一些方法来传递 Source={x:Reference Name=mArt}
与相同的结果或进行交互该命令将使其成为
when I run the application on my device, it throw an exception say that cannot find object referenced for 'mArt'
so I need some way to pass Source={x:Reference Name=mArt}
up with the same result or make the interact that command will make it
推荐答案
从你写的内容来看,我假设你有一个使用 ViewCell
的视图,比如
From what you wrote, I assume that you have a view using your ViewCell
something like
<ContentView ...
x:Name="mArt">
<ListView ...>
<ListView.ItemTemplate>
<DataTemplate>
<templates:ArticleItemViewCell ... />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentView>
现在您正试图从您的 ViewCell
中引用该视图 mArt
.不幸的是,事情并非如此.mArt
不是全局变量,而是视图类的成员(如果您对详细信息感兴趣,请查看 .xaml.g.cs
在您的对象文件夹中创建的文件).
And now you are trying to reference that view mArt
from your ViewCell
. Unfortunately that is not how things work. mArt
is not something like a global variable, but a member of your view class (if you are interested in the details, have a look at the .xaml.g.cs
file that is created in your object folder).
ArticleItemViewCell
然而是一个不同的类,你不能简单地访问其他类的字段.ArticleItemViewCell
对 mArt
一无所知.虽然可能可以通过某种方式访问父级,但我建议您不要这样做,因为您往往会忘记这些详细信息,几个月后您会查看自己的视图并想知道在哪里执行与单元格的交互,直到您意识到单元格做了一些可疑的事情.它只会花费你时间.去过也做过.相信我.
ArticleItemViewCell
however is a different class from which you can't simply access fields of some other class. ArticleItemViewCell
does not know anything about mArt
. While it might be possible to access the parent in some way, I'd advise you no to, because you tend to forget these details and some months later you'll look at your view and wonder where the interaction with the cell is implemented, until you realize, that the cell does some fishy things. It will only cost you time. Been there, done that. Believe me.
而是在您的视单元中创建一个 Command
类型的可绑定属性,并从您的包含视图绑定到它
Rather create a bindable property of type Command
in your viewcell, and bind to it from your containing view
在 ArticleItemViewCell.xaml.cs 中
In 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>
从您的角度来看,您可以绑定虚拟机的clickCommand
And from your view you can bind the clickCommand
of your VM
<ContentView ...
x:Name="mArt">
<ListView ...>
<ListView.ItemTemplate>
<DataTemplate>
<templates:ArticleItemViewCell TappedCommand="{Binding Source={x:Reference mArt}, Path=BindingContext.clickCommand}" ... />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentView>
我没有尝试确切的代码,但基本上这应该可以工作.
I did not try the exact code, but basically this oughta work.
请注意:使用 ItemTapped
事件 (查看文档) 与命令行为的事件 (见此处) 更具表现力,并为您省去了额外的命令.
Please note: Consuming the ItemTapped
event (see the docs) with a event to command behavior (see here) is more expressive and spares you the additional command.
这篇关于自定义 ViewCell 包含按钮具有命令并绑定到此命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!