如何从基类中捕获点击的项目(位于模板中)的事件? [英] How can i capture the event of a tapped item (located in a template) from the base class?
问题描述
我有一个基本网格
<Grid Grid.Row="1" Grid.Column="1" x:Name="GridName">
<StackLayout Orientation="Vertical">
<art:GridOptionsView ItemsSource="{Binding Items}" >
<art:GridOptionsView.ItemTemplate>
<DataTemplate>
<uikit:DashboardItemTemplate />
</DataTemplate>
</art:GridOptionsView.ItemTemplate>
</art:GridOptionsView>
</StackLayout>
</Grid>
其中使用以下DashboardItemTemplate
which uses the following DashboardItemTemplate
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
BackgroundColor="White">
<ContentView.Content>
<Grid Padding="0">
<StackLayout VerticalOptions="Center" HorizontalOptions="Center" Orientation="Vertical" Spacing="10">
<Grid>
<Label Text="" Style="{StaticResource FontIcon}" HorizontalTextAlignment="Center" Opacity="1" FontSize="130" TextColor="{Binding BackgroundColor}" VerticalOptions="Center" HorizontalOptions="Center" IsVisible="{Binding Source={x:Reference Root}, Path=ShowiconColoredCircleBackground}" />
<Label Text="{Binding Icon}" Style="{StaticResource FontIcon}" Opacity="1" TextColor="White" VerticalOptions="Center" HorizontalOptions="Center" />
</Grid>
<Label Text="{Binding Name}" TextColor="{Binding Source={x:Reference Root}, Path=TextColor}" FontSize="14" HorizontalTextAlignment="Center">
</Label>
</StackLayout>
</Grid>
</ContentView.Content>
<ContentView.GestureRecognizers>
<TapGestureRecognizer Tapped="OnWidgetTapped" />
</ContentView.GestureRecognizers>
</ContentView>
如何在基础xaml类上捕获OnWidgetTapped事件?
How can i capture the "OnWidgetTapped" event on my base xaml class?
推荐答案
我通常使用模板中的自定义绑定属性 ParentBindingContext
I do this usually with a custom bindable property ParentBindingContext
in my template:
public class MyTemplate : ContentPage
{
public static BindableProperty ParentBindingContextProperty = BindableProperty.Create(nameof(ParentBindingContext),
typeof(object), typeof(BasePageTemplate));
public object ParentBindingContext
{
get { return GetValue(ParentBindingContextProperty); }
set { SetValue(ParentBindingContextProperty, value); }
}
}
然后在您的页面(其中包含模板)只需设置 ParentBindingContext
:
And then in your page (which contains the template) just set the ParentBindingContext
:
<DataTemplate>
<template:MyTemplate ParentBindingContext="{Binding BindingContext, Source={x:Reference Name=MyPageName}}" />
</DataTemplate>
您可以访问完整的 BindingContext
在您的模板中的页面。以下命令示例显示了模板如何绑定到命令 MyCommand
,它位于 BindingContext
中页面:
With that you can access the full BindingContext
of your page in your template. The following example of a command shows how the template can bind to a command MyCommand
, which is in the BindingContext
of the page:
Command="{Binding ParentBindingContext.MyCommand, Source={x:Reference Name=MyTemplatePageName}}"
但这预先假定您的页面有一个 BindingContext
像ViewModel)。此ViewModel包含整个页面的全局命令。这些命令(或只是方法)然后可以被模板访问,因为他们知道页面的 BindingContext
。
But this presupposes that your page has a BindingContext
behind (like a ViewModel). This ViewModel then contains the "global" commands for the whole page. These commands (or just methods) can then be accessed by the template, because they know about the BindingContext
of the page.
这篇关于如何从基类中捕获点击的项目(位于模板中)的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!