[英]Popup in windows phone 8
我想显示一个带有media元素作为一个控件的弹出窗口。 当用户点击按钮时,我必须显示此弹出窗口。 当用户点击设备的后退按钮时,应该关闭弹出窗口。
请帮我在windows phone 8应用程序中如何做到这一点。
使用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;
}
你必须创建一个Popup
控件并且必须将你的媒体元素设置为它的Child
Property.And来处理Back Key按下使用OnBackKeyPress overriden事件。
请看下面的样本。
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;
}
}
}
当你按回键时,你应该检查弹出窗口是否打开? 如果弹出窗口打开则阻止后退键操作。 所以像这样重写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.