如何将Tapped事件传递到XAML模板?

时间:2018-07-15 11:12:37

标签: xamarin xamarin.forms

我编写了经常在应用程序中使用的代码,因此我创建了此模板。在用户的大量帮助下,到目前为止,我得到的代码是:

<?xml version="1.0" encoding="utf-8" ?>
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          xmlns:local="clr-namespace:Japanese;assembly=Japanese"
          x:Class="Japanese.SwitchViewCellTemplate"
          x:Name="this">
   <Grid VerticalOptions="CenterAndExpand" Padding="20,0" >
      <local:StyledLabel Text="{Binding Text, Source={x:Reference this}}" HorizontalOptions="StartAndExpand" />
      <local:StyledLabel IsVisible="{Binding IsVisible, Source={x:Reference this}}" TextColor="Gray" HorizontalOptions="End" Text="✓" />
   </Grid>
</ViewCell>

这是背后的代码:

using System;
using System.Collections.Generic;
using Xamarin.Forms;

namespace Japanese
{
    public partial class SwitchViewCellTemplate : ViewCell
    {
        public event EventHandler SelectAction;

        public SwitchViewCellTemplate()
        {
            InitializeComponent();
        }

        public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(SwitchViewCellTemplate));
        public static readonly BindableProperty IsVisibleProperty = BindableProperty.Create(nameof(IsVisible), typeof(bool), typeof(SwitchViewCellTemplate));

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }
        public bool IsVisible
        {
            get { return (bool)GetValue(IsVisibleProperty); }
            set { SetValue(IsVisibleProperty, value); }
        }

        protected override void OnTapped()
        {
            base.OnTapped();
            this.SelectAction?.Invoke(this, new EventArgs());
        }
    }
}

以及我的使用方式:

<template:SwitchViewCellTemplate Text="{Binding [1].Name}" 
    IsVisible="{Binding [1].IsSelected}" 
    SelectAction="selectValue" />

这是用于处理切换的方法:

    void Handle_SelectAction(object sender, System.EventArgs e)
    {
        var viewCell = sender as ViewCell;
        if (viewCell == null)
            return;
    }

在使用模板的页面后面的CS代码中,selectValue是函数。

代码给出错误:

 The type initializer for 'Japanese.SwitchViewCellTemplate' threw an exception.

任何人都可以就引起该错误的错误内容给我建议。

1 个答案:

答案 0 :(得分:2)

要解决,可以将事件添加到模板中。

XAML模板

<?xml version="1.0" encoding="utf-8" ?>
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          xmlns:local="clr-namespace:Japanese;assembly=Japanese"
          x:Class="Japanese.SwitchViewCellTemplate"
          x:Name="this">
   <Grid VerticalOptions="CenterAndExpand" Padding="20,0" >
      <local:StyledLabel Text="{Binding Text, Source={x:Reference this}}" HorizontalOptions="StartAndExpand" />
      <local:StyledLabel IsVisible="{Binding IsVisible, Source={x:Reference this}}" TextColor="Gray" HorizontalOptions="End" Text="✓" />
   </Grid>
</ViewCell>

在模板的代码隐藏中添加事件:

public partial class SwitchViewCellTemplate : ViewCell
{
    public event EventHandler SelectAction;

    public SwitchViewCellTemplate()
    {
        InitializeComponent();
    }

    public static readonly BindableProperty TextProperty = 
        BindableProperty.Create(
            nameof(Text), 
            typeof(string), 
            typeof(SwitchViewCellTemplate),
            default(string));

    public static readonly BindableProperty IsVisibleProperty = 
        BindableProperty.Create(
            nameof(IsVisible), 
            typeof(bool), 
            typeof(SwitchViewCellTemplate),
            default(bool));

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
    public bool IsVisible
    {
        get { return (bool)GetValue(IsVisibleProperty); }
        set { SetValue(IsVisibleProperty, value); }
    }

    protected override void OnTapped()
    {
        base.OnTapped();
        this.SelectAction?.Invoke(this, new EventArgs());
    }
}

页面XAML

<template:SwitchViewCellTemplate Text="{Binding [1].Name}" 
    IsVisible="{Binding [1].IsSelected}" 
    SelectAction="Handle_SelectAction" />

隐藏页面代码

    void Handle_SelectAction(object sender, System.EventArgs e)
    {

    }