繁体   English   中英

如果点击其他任何位置,Windows Phone将关闭弹出窗口

[英]Windows Phone close popup if tap anywhere else

我有一个当前正在开发的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>

我发现在声明Popup控件时设置IsLightDismissEnabled="True"有所帮助。 MSDN说明

您可以尝试处理UIElement的LostFocus事件。 在弹出窗口中调用OnLostFocus处理程序时,请关闭弹出窗口(关闭自身)。

经过大量失败的实验并浏览了整个网络,我找到了Coding4Fun工具包,并能够使用其MessagePrompt弹出工作窗口。 该工具包可从http://coding4fun.codeplex.com/获得。

而不是在所有情况下都无法使用“失去焦点”(例如,如果您在弹出窗口中具有文本框或其他可聚焦控件)。 我建议通过在根视觉中检测点击来消除弹出窗口:

            Application.Current.RootVisual.Tap += (s, e) =>
            {
                popup.IsOpen = false;
            };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM