通过在自定义控件 cs 文件 (myCustomPivotItem.cs) 中声明 RoutedEventHandler 来解决此问题。
public event RoutedEventHandler Click;
然后在 onApplyTemplate 我可以访问矩形对象
Rectangle rect = this.GetTemplateChild("rectObject") as Rectangle;
rect.Tap += new EventHandler<GestureEventArgs>(RectView_Tap);
然后我在同一个 cs 文件 (myCustomPivotItem.cs) 中声明了 RectView_Tap
private void RectView_Tap(object sender, GestureEventArgs e)
{
if (Click != null)
Click(this, new RoutedEventArgs());
}
在我的 MainPage.xaml 中,我像这样声明了自定义控件
<controls:PivotItem x:Name="pivotitem2">
<view:myCustomePivotItem x:Name="custompivotItem2" Click="myHandler"/>
</controls:PivotItem>
在 MainPage.cs 中我声明了 myHandler...
void myHandler(object sender, RoutedEventArgs e)
{
//delegate operation
MessageBox.Show("Clicked!");
}
它按预期工作! :) 希望它能帮助任何需要它的人。