如何从主窗体中捕获用户控件中特定按钮上的点击事件 [英] How to capture tap event on specific button in user control from main form

查看:128
本文介绍了如何从主窗体中捕获用户控件中特定按钮上的点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows store应用程序,该应用程序具有一个主窗体和多个用户控件.在每个用户控件中,我都有一个图像,当点击该图像时,该图像应触发该用户控件外部的情节提要动画设置.

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.

用于淡入用户控件IN的情节提要很好,但很明显,当在用户控件中单击关闭按钮时,我需要淡出OUT动画才能触发.

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);
        }
    }
}

主页代码如下:

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();
    }
}

推荐答案

该行:

this.AddHandler(UIElement.TappedEvent, new TappedEventHandler(Page02_CloseButtonTapped), true);

将MainPage的轻击事件与Page02_CloseButtonTapped方法相关联.因此,任何点击事件都会触发该方法.

is associating the tapped event of the MainPage to Page02_CloseButtonTapped method. So any tap event on it will fire the method.

我假设您打算将其附加到Page02控件的CloseButtonTapped事件上?

I'm assuming you meant to attach it to the CloseButtonTapped event on the Page02 control instead?

这篇关于如何从主窗体中捕获用户控件中特定按钮上的点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆