我有一个我目前正在开发的 Windows Phone 应用程序,我的共享按钮使用以下代码打开一个带有多个选项的小弹出窗口:
private void share_Click(object sender, System.EventArgs e)
{
Popup share= new Popup();
share.Child = new sharePopup();
share.IsOpen = true;
share.VerticalOffset = 30;
share.HorizontalOffset = 0;
}
现在,这个弹出窗口有一个“关闭”按钮,但如果我不点击它,而是点击前一个仍然可见的页面上的另一个按钮,即使移动到新页面,弹出窗口也会保持原位。我必须单击“关闭”才能使弹出窗口消失。
如果我点击弹出窗口本身之外的任何位置,我正在寻找一种关闭弹出窗口的方法。是否有预定义的方法来执行此操作?如果没有,我可以采取什么方法?
提前致谢
编辑:这是弹出窗口的 c# 代码
public partial class sharePopup : UserControl
{
public sharePopup()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Popup mensaje = this.Parent as Popup;
mensaje.IsOpen = false;
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
this.Focus();
}
private void UserControl_LostFocus(object sender, RoutedEventArgs e)
{
Popup mensaje = this.Parent as Popup;
mensaje.IsOpen = false;
}
}
}
弹出窗口的 XAML 仅包含大小、颜色和按钮定义:
<UserControl x:Class="MSPinTrainingApp.sharePopup"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480"
Width="480" Height="200" >
<Grid x:Name="LayoutRoot" LostFocus="LayoutRoot_LostFocus">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.Background>
<SolidColorBrush Color="{StaticResource PhoneChromeColor}"/>
</Grid.Background>
<TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Margin="3" x:Name="txtMensaje" Text="Compartir:" LostFocus="txtMensaje_LostFocus" />
<Image Margin="70,60,335,62" Source="appbar.email.hardedge.png" Stretch="Fill" Tap="Image_Tap_1"/>
<Image Margin="200,60,200,62" Source="appbar.facebook.png" Stretch="Fill" Tap="Image_Tap_2"/>
<Image Margin="335,60,70,62" Source="appbar.twitter.bird.png" Stretch="Fill" Tap="Image_Tap_3"/>
<Image Margin="430,-12,11,134" Source="/appbar.close.png" Width="50" Tap="Image_Tap_4"/>
</Grid>