0

美好的一天,我有一个弹出用户控件,用户可以在其中与手势交互。我想要的是,当用户点击图像项时,会调用一个方法并关闭弹出窗口。但是我遇到的问题是,当点击用户控件中的图像项时,会触发用户控件中的所有点击事件方法。我尝试为每个设置不同的手势列表,但它似乎不起作用。这是我的代码:

myPopUp = new Popup();
            myPopUp.Height = 400;
            myPopUp.Width = 400;
            //myPopUp.VerticalOffset = 100;
            myPopUp.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            myPopUp.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            layout_menu layouts = new layout_menu();
            myPopUp.Child = layouts;
            myPopUp.IsOpen = true;

            GestureListener gesturelistener = GestureService.GetGestureListener(layouts);
            gesturelistener.Tap += new EventHandler<GestureEventArgs>(onFirstImageTap);

            GestureListener gesturelistener2 = GestureService.GetGestureListener(layouts);
            gesturelistener2.Tap += new EventHandler<GestureEventArgs>(onSecondImageTap);



        }

        private void onFirstImageTap(object sender, GestureEventArgs e)
        {
            //LayoutPanel = 1;
            MessageBox.Show("First image tapped");
            myPopUp.IsOpen = false;
        }

        private void onSecondImageTap(object sender, GestureEventArgs e)
        {
            MessageBox.Show("Second image tapped");
            myPopUp.IsOpen = false;
        }

Layout_menu XAML:

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}" Margin="0,0,10,0">
        <Grid x:Name="first_image_layout" HorizontalAlignment="Left" Height="150" Margin="13,10,0,0" VerticalAlignment="Top" Width="143">
            <Image HorizontalAlignment="Left" Height="99" Margin="18,33,0,0" VerticalAlignment="Top" Width="107" Source="/Assets/Images/Navigation_Pane.png">
                <toolkit:GestureService.GestureListener>
                    <toolkit:GestureListener Tap="onFirstImageTap"></toolkit:GestureListener>
                </toolkit:GestureService.GestureListener>
            </Image>
        </Grid>
        <Grid x:Name="second_image_layout" HorizontalAlignment="Left" Height="156" Margin="13,161,0,0" VerticalAlignment="Top" Width="143">
            <Image HorizontalAlignment="Left" Height="99" Margin="18,33,0,0" VerticalAlignment="Top" Width="107" Source="/Assets/Images/Navigation_Pane.png">
                <toolkit:GestureService.GestureListener>
                    <toolkit:GestureListener Tap="onSecondImageTap"></toolkit:GestureListener>
                </toolkit:GestureService.GestureListener>
            </Image>
        </Grid>

当其中一个图像被点击时,如何关闭此弹出窗口?解决方案受到高度赞赏。提前致谢。

4

1 回答 1

0

有很多方法可以做到这一点,但我认为最好的方法是在你的用户控件中声明自定义事件。

像您一样保留在 UserControl 的 XAML 中声明的 GestureListener,但在代码隐藏中添加两个自定义事件:

public event EventHandler FirstImageTapped;
public event EventHandler SecondImageTapped;

然后,仍然在 UserControl 中,使用onFirstImageTaponSecondImageTap方法来触发您的事件:

private void onFirstImageTap(object sender, GestureEventArgs e)
{
    var eventHandler = this.FirstImageTapped;

    if (eventHandler != null)
    {
        eventHandler(this, EventArgs.Empty);
    }
}

private void onSecondImageTap(object sender, GestureEventArgs e)
{
    var eventHandler = this.SecondImageTapped;

    if (eventHandler != null)
    {
        eventHandler(this, EventArgs.Empty);
    }
}

最后,在父页面中,创建弹出窗口时,订阅您的自定义事件:

layout_menu layouts = new layout_menu();
layouts.FirstImageTapped += this.OnFirstImageTapped;
layouts.SecondImageTapped += this.OnSecondImageTapped;

在事件处理程序中,关闭弹出窗口:

private void OnFirstImageTapped(object sender, EventArgs e)
{
    MessageBox.Show("First image tapped");
    myPopUp.IsOpen = false;
}

private void OnSecondImageTapped(object sender, EventArgs e)
{
    MessageBox.Show("Second image tapped");
    myPopUp.IsOpen = false;
}
于 2013-11-12T09:17:53.680 回答