问题
I have a windows store app that has a main form and several user controls. In each user control, I have an image, that, when tapped, should trigger a storyboard animation setup outside of the user control.
The storyboard to fade the user control IN, works fine, but obviously I need the fade OUT animation to fire when the close button is clicked within the user control.
Here's the code I added to the user control itself to fire when the close button is clicked. The problem is that it's firing when you click anywhere on the usercontrol, not just the close button.
public sealed partial class Page02 : UserControl {
public delegate void CloseButtonTappedHandler(object sender, TappedRoutedEventArgs e);
public event CloseButtonTappedHandler CloseButtonTapped;
public Page02() {
this.InitializeComponent();
this.imgCloseButton.Tapped += new TappedEventHandler(this.imgCloseButton_Tapped);
}
private void imgCloseButton_Tapped(object sender, TappedRoutedEventArgs e) {
if (CloseButtonTapped != null) {
CloseButtonTapped(sender, e);
}
}
}
The main page code looks like this:
public sealed partial class MainPage : Page {
public MainPage() {
InitializeComponent();
this.AddHandler(UIElement.TappedEvent, new TappedEventHandler(Page02_CloseButtonTapped), true);
}
private void textBlock1_Tapped(object sender, TappedRoutedEventArgs e) {
MenuBar01.Visibility = Visibility.Visible;
HideUserControl1.Begin();
ShowUserControl2.Begin();
}
private void Page02_CloseButtonTapped(object sender, TappedRoutedEventArgs e) {
HideUserControl2.Begin();
}
}
回答1:
The line:
this.AddHandler(UIElement.TappedEvent, new TappedEventHandler(Page02_CloseButtonTapped), true);
is associating the tapped event of the MainPage to Page02_CloseButtonTapped method. So any tap event on it will fire the method.
I'm assuming you meant to attach it to the CloseButtonTapped event on the Page02 control instead?
来源:https://stackoverflow.com/questions/36455961/how-to-capture-tap-event-on-specific-button-in-user-control-from-main-form