美好的一天,我有一个Popup用户控件,用户可以在其中与手势进行交互。我想要的是当用户点击图像项时,调用一个方法并关闭Popup。但我遇到的问题是,当点击usercontrol中的图像项时,会触发usercontrol中的所有tap事件方法。我尝试为每一个设置不同的gesturelistnener但它似乎没有工作。这是我的代码:
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>
如果点击其中一个图像,我该如何关闭此弹出窗口?解决方案非常受欢迎。提前谢谢。
答案 0 :(得分:0)
有很多方法可以做到这一点,但我认为最好是在用户控件中声明自定义事件。
保持在UserControl的XAML中声明的GestureListener,就像你一样,但在代码隐藏中添加两个自定义事件:
public event EventHandler FirstImageTapped;
public event EventHandler SecondImageTapped;
然后,仍然在UserControl中,使用onFirstImageTap
和onSecondImageTap
方法触发您的活动:
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;
}