【发布时间】:2013-02-22 19:41:52
【问题描述】:
我有一个正在开发的 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>
【问题讨论】:
-
我们需要查看 XAML,但看起来您正在文本框上设置 LostFocus 事件。您是否在显示弹出窗口时使文本框获得焦点?如果不是,则不会在该控件上调用 LostFocus。
-
你是对的,那是我的错误......很快就会发布 XAML。
-
好的。我之前的评论的其余部分没有得到答复。您是否将重点放在 txtMensaje 上?有关更多信息,请参阅此内容:stackoverflow.com/questions/124649
-
不,我不是,我删除了那个 txtMensaje_LostFocus。使用您发布的其他问题,我添加了一个带有 this.Focus() 的 UserControl_Loaded(...) 方法,以及带有用于关闭弹出窗口的代码的 UserControl_LostFocus(...) ,但仍然没有。
-
您在调试器中看到了什么? Alos,我会尝试继续使用文本框的想法并明确地将焦点设置在它上面。 LostFocus 应该被调用。
标签: c# popup windows-phone