问题
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>
where mArt is the view that will command make some thing with it
after that I used this view cell in on of my xamarin pages like that:
<ListView.ItemTemplate>
<DataTemplate>
<Cell:ArticleItemViewCell />
</DataTemplate>
</ListView.ItemTemplate>
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
回答1:
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>
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 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.
Rather create a bindable property of type Command in your viewcell, and bind to it from your containing view
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);
}
And now you can bind them from your 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>
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.
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.
来源:https://stackoverflow.com/questions/54982308/custom-viewcell-contain-button-has-command-and-binding-to-this-command