通過在自定義聲明一個RoutedEventHandler解決它控制CS文件(myCustomPivotItem.cs)。
public event RoutedEventHandler Click;
然後在onApplyTemplate我得到
Rectangle rect = this.GetTemplateChild("rectObject") as Rectangle;
rect.Tap += new EventHandler<GestureEventArgs>(RectView_Tap);
我然後在相同的CS文件中聲明RectView_Tap訪問矩形對象(myCustomPivotItem.cs)
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!");
}
和它的作品如預期! :)希望它可以幫助任何需要它的人。
謝謝你的按鈕,我嘗試將它應用到一個矩形的Tap事件,這是一個EventHandler類型,它不起作用。我甚至爲它添加了一個依賴項屬性,但我得到一個異常。有任何想法嗎? –
2012-01-13 05:34:33
@Cool Dude - 你應該可以使用相同的方法,但你的事件也必須定義爲EventHandler類型:'公共事件EventHandler 點擊{add {...} remove {。 ..}}' –
2012-01-13 05:52:53
現在我得到一個空指針異常,因爲在調用onApplyTemplate()之前,公共事件屬性中引用了矩形對象。 – 2012-01-13 07:21:30