[英]How to pass event from ViewCell to parent object, where ListView is placed
I want to handle the events from the cell within the parent object. 我想处理来自父对象内单元的事件。 This is the XAML of the parent object: 这是父对象的XAML:
<StackLayout xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App.Views.SomeView"
xmlns:customViews="clr-namespace:App.Views;assembly=App"
VerticalOptions="FillAndExpand">
<ListView x:Name="MyList"
HasUnevenRows="True"
CachingStrategy="RecycleElement">
<ListView.ItemTemplate>
<DataTemplate>
<customViews:CustomListItem/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
As you can see I have a custom cell defined in my data template. 如您所见,我在数据模板中定义了一个自定义单元格。 The cell itselfs looks something like this: 单元格本身看起来像这样:
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App.Views.CustomListItem">
<Image x:Name="infoButton" Aspect="AspectFit" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" />
</ViewCell>
In the code behind file I add a tap recognizer to the image: 在文件后面的代码中,我向图像添加了一个点击识别器:
public partial class CustomListItem : ViewCell
{
private TapGestureRecognizer onIconTapppedRecognizer;
public CustomListItem()
{
InitializeComponent();
// Make it tapable
this.onIconTapppedRecognizer = new TapGestureRecognizer();
this.onIconTapppedRecognizer.Tapped += Icon_Tapped;
this.infoButton.GestureRecognizers.Add(this.onIconTapppedRecognizer);
}
private async void Icon_Tapped(object sender, EventArgs e)
{
await MainPage.Instance.Detail.Navigation.PushAsync(new MyNewPage(this.viewModel));
}
}
Now I want to receive the Icon_Tapped
event on SomeView
. 现在我想收到Icon_Tapped
事件SomeView
。 This approach doesn't work, because the cell is defined in a separate object and not on the same "page". 这种方法行不通,因为单元格是在单独的对象中定义的,而不是在同一“页面”上定义的。 Another approach requires to pass the view model in the code via constructor, but I have defined that in XAML. 另一种方法需要通过构造函数在代码中传递视图模型,但是我已经在XAML中定义了它。 Furthermore, I have a model for each entry in the list view and not one for the whole list view. 此外,对于列表视图中的每个条目,我都有一个模型,而对于整个列表视图,则没有一个模型。
How can I receive the event from the cell? 如何从单元接收事件?
Didn't know that you can define custom events on any object: 不知道您可以在任何对象上定义自定义事件:
SomeView
XAML now looks like this SomeView
XAML现在看起来像这样
<customViews:CustomListItem ButtonClicked="SomeView_ButtonClicked"/>
The according event handler in the code behind file from SomeView
SomeView
文件背后代码中的相应事件处理程序
private async void SomeView_ButtonClicked(object sender, CustomEventArgs e)
{
// do something
}
On my ViewCell
CustomListItem
I have defined a public event handler like this 在我的ViewCell
CustomListItem
我定义了一个像这样的公共事件处理程序
public event EventHandler<CustomEventArgs> ButtonClicked;
And from the event handler from my added gesture recognizer, this event is invoked: 从我添加的手势识别器的事件处理程序中,调用此事件:
private void Icon_Tapped(object sender, EventArgs e)
{
this.ButtonClicked?.Invoke(sender, new CustomEventArgs(this.model));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.