2014-04-15 39 views
0

我有一個用戶控件,並在用戶控件本身的點擊事件。並且我正在爲該用戶控件在父頁面的電話應用程序頁面中保存事件。我想要通過保持事件提高點擊事件如何實現此目的? ParentPage是有:如何處理WindowsPhone8中的事件觸發事件?

<DataTemplate x:Key="template" > 
    <chatbubble:ChatBubbleControl x:Name="ChatBubble" Hold="ChatBubbleControl_Hold_1" /> 
</DataTemplate> 

用戶控件有..

<UserControl.Resources> 
    .... 
    <Grid x:Name="LayoutRoot" Background="Transparent" Width="455" Tap="chatBubble_Tap" > 
    ..... 
    </Grid> 

我想從ChatBubbleControl_Hold_1

回答

0

提高chatBubble_Tap事件時,可以嘗試提高您r這樣的事件:

// make your eventhandler in Parent Page public and static so it will be available thru all the App 
public static void chatBubble_Tap(object sender, System.Windows.Input.GestureEventArgs e) 
{ 
    // your code 
} 

// then in your UserControl you should be able to call it like this: 
private void ChatBubbleControl_Hold_1(object sender, System.Windows.Input.GestureEventArgs e) 
{ 
    YourPage.chatBubble_Tap(sender, e); 
} 

這種情況取決於你在Tap事件中有什麼(並非所有事情都可以在靜態方法中)。 。

您還可以通過你的頁面的處理程序,以你的用戶控件,然後從處理程序調用你的點擊事件(在這種情況下,點擊事件可以是public(非靜態)簡單的情況下,可以是這樣的:

// your Control in MainPage (or other Page) 
<local:myControl x:Name="yourControl" VerticalAlignment="Center" Grid.Row="1"/> 

// initializing control and event to be invoked: 
public MainPage() 
{ 
    InitializeComponent(); 
    yourControl.pageHandler = this; 
} 

public void second_Click(object sender, RoutedEventArgs e) 
{ 
    // something here 
} 

// and the control code: 
public partial class myControl : UserControl 
{ 
    public Page pageHandler; 

    public myControl() 
    { 
     InitializeComponent(); 
     myButton.Hold +=myButton_Hold; 
    } 

    private void myButton_Hold(object sender, System.Windows.Input.GestureEventArgs e) 
    { 
     if (pageHandler is MainPage) (pageHandler as MainPage).second_Click(sender, e); 
    } 
} 

第三個選擇是傳遞一個Action您的控制(在這種情況下,事件可以是私有的):

// code in MainPage (or your Page) 
public MainPage() 
{ 
    InitializeComponent(); 
    yourControl.myAction = second_Click; // setting an action of Control 
} 

private void second_Click(object sender, RoutedEventArgs e) 
{ 
    // something here 
} 

// and the Control class 
public partial class myControl : UserControl 
{ 
    public Action<object, System.Windows.Input.GestureEventArgs> myAction; 

    public myControl() 
    { 
     InitializeComponent(); 
     myButton.Hold +=myButton_Hold; 
    } 

    private void myButton_Hold(object sender, System.Windows.Input.GestureEventArgs e) 
    { 
     if (myAction != null) myAction.Invoke(sender, e); 
    } 
} 
相關問題