簡體   English   中英

Xamarin.Forms中的自定義EventHandler在xaml中不起作用

[英]Custom EventHandler in Xamarin.Forms doesn't work in xaml

Xamarin.Forms ,在PCL中,我具有自定義控件,帶有自定義事件:

public class TestGrid : Grid
{
    public EventHandler OnTapped;

    public TestGrid()
    {
        var tgr = new TapGestureRecognizer { NumberOfTapsRequired = 1 };
        tgr.Tapped += Tgr_Tapped;
        this.GestureRecognizers.Add(tgr);
    }

    private void Tgr_Tapped(object sender, EventArgs e)
    {
        OnTapped?.Invoke(sender, e);
    }
}

在我的起始頁中,我有:

public partial class StartPage : ContentPage
{
    public StartPage()
    {
        InitializeComponent();
        BindingContext = new StartPageViewModel();
        MainGrid.OnTapped += OnGridTapped;
    }

    private void OnGridTapped(object sender, EventArgs e)
    {
        Debug.WriteLine("Tapped!");
    }
}

我的XAML看起來像這樣,並且可以正常工作:

<v:TestGrid x:Name="MainGrid" />

但!

當我刪除MainGrid.OnTapped += OnGridTapped; 並在xaml中添加事件,在這里:

<v:TestGrid x:Name="MainGrid" OnTapped="OnGridTapped">

我不工作 它說.. OnTapped事件未找到:

未處理的異常:

Xamarin.Forms.Xaml.XamlParseException:位置6:33。 找不到名稱為OnTapped的屬性

我的問題是: 為什么? 有什么不同?

OnTapped必須是一個事件:

public event EventHandler OnTapped;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM