【问题标题】:Popup in windows phone 8windows phone 8中的弹出窗口
【发布时间】:2018-01-05 18:35:56
【问题描述】:

我想显示一个带有媒体元素的弹出窗口作为一个控件。 当用户单击按钮时,我必须显示此弹出窗口。当用户点击设备的后退按钮时,弹出窗口应该关闭。

请帮助我如何在 windows phone 8 应用程序中执行此操作。

【问题讨论】:

    标签: c# xaml windows-phone-8 alert popupwindow


    【解决方案1】:

    带有 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>
    

    应用程序栏

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

    背后的代码

    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;
    }
    

    【讨论】:

      【解决方案2】:

      您必须创建一个Popup 控件并且必须将您的媒体元素设置为Child 属性。并且要处理返回键按下使用 OnBackKeyPress 覆盖事件。

      请参见下面的示例。

          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;
                  }
              }
          }
      

      【讨论】:

        【解决方案3】:

        当你按下返回键时,你应该检查弹出窗口是否打开?如果弹出窗口打开,则阻止后退键操作。所以像这样覆盖 OnBackKeyPress()

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

        【讨论】:

          猜你喜欢
          • 2015-02-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多