简体   繁体   English

在Windows Phone 8中弹出窗口

[英]Popup in windows phone 8

I want to show a popup with media element as one control. 我想显示一个带有media元素作为一个控件的弹出窗口。 When user clicks on the button, then I have to show this popup. 当用户点击按钮时,我必须显示此弹出窗口。 And when user clicks on the back button of the device then the popup should be closed. 当用户点击设备的后退按钮时,应该关闭弹出窗口。

Please help me how to do this in windows phone 8 application. 请帮我在windows phone 8应用程序中如何做到这一点。

Popup with MediaElement (view is name of PhoneApplicationPage ) 使用MediaElement弹出窗口(视图是PhoneApplicationPage名称)

<Popup
    x:Name="popup">

    <Grid
        Background="{StaticResource PhoneChromeBrush}"
        Height="{Binding Path=ActualHeight, ElementName=view}"
        Width="{Binding Path=ActualWidth, ElementName=view}">

        <MediaElement />

    </Grid>

</Popup>

Application Bar 应用栏

<phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar>

        <shell:ApplicationBarIconButton
            Click="ShowPopup"
            IconUri="/Icons/show.png"
            Text="show" />

    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

Code behind 代码背后

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
    if (this.popup.IsOpen)
    {
        this.popup.IsOpen = false; 
        e.Cancel = true;
    }

    base.OnBackKeyPress(e);
}


private void ShowPopup(object sender, EventArgs e)
{
    this.popup.IsOpen = true;
}

You have to create a Popup control & have to set your media element as it's Child Property.And to handle Back Key press use OnBackKeyPress overriden event. 你必须创建一个Popup控件并且必须将你的媒体元素设置为它的Child Property.And来处理Back Key按下使用OnBackKeyPress overriden事件。

Please see below sample. 请看下面的样本。

    private Popup _popup;

    private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        Grid g=new Grid {Height = 400,Width = 480,Background =new SolidColorBrush(Colors.Green)};
        Rectangle r = new Rectangle
            {
                Height = 50,
                Width=50,
                Fill = new SolidColorBrush(Colors.Red),
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center
            };
        g.Children.Add(r);
        _popup = new Popup()
           {
               Height = 400,
               Width = 480,
               IsOpen = true,
               Child = g
           };          
    }

    protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
    {
        if (_popup != null)
        {
            if (_popup.IsOpen)
            {
                e.Cancel = true;
                _popup.IsOpen = false;
            }
        }
    }

When you press back key, You should check is popup open? 当你按回键时,你应该检查弹出窗口是否打开? If popup open then prevent the back key action. 如果弹出窗口打开则阻止后退键操作。 So override OnBackKeyPress() like this: 所以像这样重写OnBackKeyPress()

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
    if (this.popup.IsOpen)
    {
        this.popup.IsOpen = false; 
        e.Cancel = true;
    }

    base.OnBackKeyPress(e);
}

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

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