我有一个自定义视图,在其中有2个按钮 - 一个在左边,一个在右边。
我想要一个可绑定的属性来指示每个按钮将调用哪个函数,但是我想要调用的函数在原始内容页面中,而不是自定义控件中。
我知道你不能将void作为可绑定属性,所以我不确定如何实现这一点。
这是我的自定义控件xaml:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="EngineerApp.HeaderBar" HeightRequest="50">
<BoxView x:Name="banner" BackgroundColor="#edeff2"
RelativeLayout.WidthConstraint="{ ConstraintExpression
Type=RelativeToParent,
Property=Width,
Factor=1 }"
RelativeLayout.HeightConstraint="{ ConstraintExpression
Type=RelativeToParent,
Property=Height,
Factor=1 }">
</BoxView>
<Button
Text="Left"
x:Name="btnLeft"
Style="{StaticResource headerBarButton}"
RelativeLayout.WidthConstraint="{ ConstraintExpression
Type=RelativeToView,
ElementName=banner,
Property=Width,
Factor=0.5 }"
RelativeLayout.HeightConstraint="{ ConstraintExpression
Type=RelativeToView,
ElementName=banner,
Property=Height,
Factor=1 }"
>
</Button>
<Button
Text="Right"
Style="{StaticResource headerBarButton}"
RelativeLayout.XConstraint="{ ConstraintExpression
Type=RelativeToView,
ElementName=banner,
Property=Width,
Factor=0.5,
Constant=1
}"
RelativeLayout.WidthConstraint="{ ConstraintExpression
Type=RelativeToView,
ElementName=banner,
Property=Width,
Factor=0.5 }"
RelativeLayout.HeightConstraint="{ ConstraintExpression
Type=RelativeToView,
ElementName=banner,
Property=Height,
Factor=1 }"
>
</Button>
</RelativeLayout>
以下是后端代码:
using System;
using System.Collections.Generic;
using System.Windows.Input;
using Xamarin.Forms;
namespace EngineerApp
{
public partial class HeaderBar : RelativeLayout
{
public HeaderBar()
{
InitializeComponent();
}
}
}
最后,我像往常一样包含自定义的新内容。
<local:HeaderBar></local:HeaderBar>
我该如何将2个按钮的单击事件绑定到我的内容页面中的一个方法?