【发布时间】:2017-12-01 10:38:20
【问题描述】:
我创建了一个自定义控件。 它由一个放置 Button 的 Grid 组成:
using Xamarin.Forms;
namespace MyApp
{
public class clsButton : ContentView
{
private Grid _grid;
private Button _button;
public clsButton()
{
_grid = new Grid
{
Margin = new Thickness(0),
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
};
_grid.BindingContext = this;
_button = new Button()
{
};
_button.Clicked += async (sender, e) =>
{
//I tried different things here, but none gave me the right results. I need to "bubble" this click to the outside
return;
};
_grid.Children.Add(_button, 0, 0);
this.Content = _grid;
}
}
}
我在 ContentPage 中创建了一些自定义控件,如下所示:
_MyButton = new clsImageButton()
{
};
var nTapGestureRecognizer = new TapGestureRecognizer();
nTapGestureRecognizer.Tapped += OnButtonClicked;
_MyButton.GestureRecognizers.Add(nTapGestureRecognizer);
这是同一 ContentPage 中的空白:
async void OnButtonClicked(object sender,EventArgs e)
{
//I don't managed to get here
}
这不起作用。 永远不会调用“OnButtonClicked”。
我想我必须从自定义控件中引发一个事件。 我尝试了一些方法,但都没有成功。
我该如何正确地做到这一点?
【问题讨论】:
标签: xamarin xamarin.forms