UWP Popup 弹出提示框
一:需求
做一个类似于安卓的弹出消息框,如图。当用户点击下载或者选择时,能够从底部弹出一个提示框,用于提示用户。

二:Popup 类
不需要我们自己额外去写一个弹窗类,微软自己有一个Popup 弹窗类。当弹窗打开时,会自动放在当前应用页面的最顶层。
//获取或设置要在弹出项中承载的内容。
public UIElement Child { get; set; }
Popup类里有一个Child属性,用来存弹窗中的内容。
child的类型是UIElement。
UIElement 是具有可视外观并可以处理基本输入的大多数对象的基类。
因此child属性可以存grid stackpannel 这些......
//获取或设置弹出项当前是否显示在屏幕上。
//如果当前显示了弹出项,则为 **true**;否则为 **false**。 默认值为 **false**。
public bool IsOpen { get; set; }
Popup类还有一个IsOpen属性,当会true的时候,弹窗是打开的。false则相反。
三:ps。。。
当创建一个popup的对象,并且将它的IsOpen属性设置为true的时候,代表将会有一个弹窗 显示在当前应用的最顶层。
像上面图中的做法,看上去只有一小块是弹窗,其实我的做法是,最顶层的popup的child属性里放的是一个grid,在grid里才是我显示出来的那一小块提示框,因为grid如果没有背景颜色的话,底下一层是会显示的,所以没有什么问题。不会因为盖了一层grid,底下的内容会被盖住。
四:直接上代码
创建一个用户控件
<UserControl
x:Class="One.UC.PopupNotice"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:One.UC"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight=""
d:DesignWidth=""> <UserControl.Resources> <Storyboard x:Name="PopupIn">
<DoubleAnimation From=""
To=""
Duration="00:00:00.5"
Storyboard.TargetName="PopupContainer"
Storyboard.TargetProperty="Opacity"
>
<DoubleAnimation.EasingFunction>
<PowerEase EasingMode="EaseOut"></PowerEase>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
<DoubleAnimation From="-10"
To="-100"
Duration="00:00:00.5"
Storyboard.TargetName="PopupContainer"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.Y)"
>
<DoubleAnimation.EasingFunction>
<PowerEase EasingMode="EaseOut"></PowerEase>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard> <Storyboard x:Name="PopupOut">
<DoubleAnimation From=""
To=""
Duration="00:00:00.5"
Storyboard.TargetName="PopupContainer"
Storyboard.TargetProperty="Opacity"
>
<DoubleAnimation.EasingFunction>
<PowerEase EasingMode="EaseOut"></PowerEase>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
<DoubleAnimation From="-100"
To="-10"
Duration="00:00:00.5"
Storyboard.TargetName="PopupContainer"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.Y)"
>
<DoubleAnimation.EasingFunction>
<PowerEase EasingMode="EaseOut"></PowerEase>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard> </UserControl.Resources> <Grid> <StackPanel Background="#18C3D8"
Padding=""
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
Name="PopupContainer"
Opacity=""> <!--改变Y轴和透明底-->
<StackPanel.RenderTransform>
<TranslateTransform Y="-10"></TranslateTransform>
</StackPanel.RenderTransform> <TextBlock Name="PopupContent"></TextBlock> </StackPanel> </Grid> </UserControl>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation; // The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236 namespace One.UC
{
public sealed partial class PopupNotice : UserControl
{
//存放弹出框中的信息
private string _popupContent; //创建一个popup对象
private Popup _popup = null; public PopupNotice()
{
this.InitializeComponent(); //将当前的长和框 赋值给控件
this.Width = Window.Current.Bounds.Width;
this.Height = Window.Current.Bounds.Height; //将当前的控价赋值给弹窗的Child属性 Child属性是弹窗需要显示的内容 当前的this是一个Grid控件。
_popup = new Popup();
_popup.Child = this; //给当前的grid添加一个loaded事件,当使用了ShowAPopup()的时候,也就是弹窗显示了,这个弹窗的内容就是我们的grid,所以我们需要将动画打开了。
this.Loaded += PopupNoticeLoaded; } /// <summary>
/// 重载
/// </summary>
/// <param name="popupContentString">弹出框中的内容</param>
public PopupNotice(string popupContentString):this()
{
_popupContent = popupContentString;
} /// <summary>
/// 显示一个popup弹窗 当需要显示一个弹窗时,执行此方法
/// </summary>
public void ShowAPopup()
{
_popup.IsOpen = true;
} /// <summary>
/// 弹窗加载好了
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void PopupNoticeLoaded(object sender, RoutedEventArgs e)
{
PopupContent.Text = _popupContent; //打开动画
this.PopupIn.Begin();
//当进入动画执行之后,代表着弹窗已经到指定位置了,再指定位置等一秒 就可以消失回去了
this.PopupIn.Completed += PopupInCompleted;
} /// <summary>
/// 当进入动画完成后 到此
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public async void PopupInCompleted(object sender, object e)
{
//在原地续一秒
await Task.Delay(); //将消失动画打开
this.PopupOut.Begin();
//popout 动画完成后 触发
this.PopupOut.Completed += PopupOutCompleted;
} //弹窗退出动画结束 代表整个过程结束 将弹窗关闭
public void PopupOutCompleted(object sender, object e)
{
_popup.IsOpen = false;
} }
}
在要显示一个弹窗的代码里调用ShowAPopup()
PopupNotice popupNotice = new PopupNotice("正在后台下载......");
popupNotice.ShowAPopup();
最终效果:

--------some words----------
1.Popup 弹出
2.UIElement ui元素
----------the end------------
UWP Popup 弹出提示框的更多相关文章
- android标题栏上面弹出提示框(二) PopupWindow实现,带动画效果
需求:上次用TextView写了一个从标题栏下面弹出的提示框.android标题栏下面弹出提示框(一) TextView实现,带动画效果, 总在找事情做的产品经理又提出了奇葩的需求.之前在通知栏显示 ...
- android标题栏下面弹出提示框(一) TextView实现,带动画效果
产品经理用的是ios手机,于是android就走上了模仿的道路.做这个东西也走了一些弯路,写一篇博客放在这里,以后自己也可用参考,也方便别人学习. 弯路: 1.刚开始本来用PopupWindow去实现 ...
- PHP弹出提示框并跳转到新页面即重定向到新页面
本文为大家介绍下使用PHP弹出提示框并跳转到新页面,也就是大家所认为的重定向,下面的示例大家可以参考下 这两天写一个demo,需要用到提示并跳转,主要页面要求不高,觉得没必要使用AJAX,JS等, ...
- [转] 在Asp.net前台和后台弹出提示框
一.在前台弹出提示框 1.点击"A"标记或者"控件按钮"弹出提示框 <asp:LinkButton ID="lbtnDel" runa ...
- SilverLight 页面后台方法XX.xaml.cs 创建JS,调用JS ,弹出提示框
1.Invoke和InvokeSelf [c-sharp] view plaincopy public partial class CreateJSDemo : UserControl { publi ...
- 基于Jquery 简单实用的弹出提示框
基于Jquery 简单实用的弹出提示框 引言: 原生的 alert 样子看起来很粗暴,网上也有一大堆相关的插件,但是基本上都是大而全,仅仅几句话可以实现的东西,可能要引入好几十k的文件,所以话了点时间 ...
- iOS bug 之 H5 页面没有弹出提示框
描述:在安卓上有提示框,但是在iOS上没有提示框. step 1: 失误,是我没有在正确的位置设置网址. step 2: 修改之后,测试页能弹出提示框,但是正式的页面没有提示框. step 3: 我输 ...
- C#自动关闭弹出提示框
自动关闭弹出提示框(用一个小窗体显示提示信息):例如在一个form窗体中弹出自动关闭的提示框1.首先创建一个弹出提示信息的窗体 AutoCloseMassageBox,在里面拖一个lable控件,去掉 ...
- 关于winform窗体关闭时弹出提示框,选择否时窗体也关闭的问题
在窗体中有FormClosing这个事件,这个事件是在窗体关闭时候运行的.如果要取消某个事件的操作,那么就在该事件中写上e.Cancel=true就能取消该事件,也就是不执行该事件.所以,你要在窗体关 ...
随机推荐
- 解决Android Studio Fetching Android SDK component information失败问题【转】
本文转载自:http://blog.csdn.net/isesar/article/details/41908089 Android Studio 安装完成后,如果直接启动,Android Studi ...
- HDU2049 不容易系列之(4)考新郎 —— 错排
题目链接:https://vjudge.net/problem/HDU-2049 不容易系列之(4)——考新郎 Time Limit: 2000/1000 MS (Java/Others) Me ...
- CSU - 1550 Simple String —— 字符串
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1550 题解: 1.A+B 与C的交集必须>=n 2.A与C的交集必须>= ...
- 人生苦短之Python列表拷贝
列表拷贝的几种方法: 1.工厂函数 b=list(a) >>> a=[1,2,3,4] >>> b=list(a) >>> b [1, 2, ...
- x264 FFmpeg Options Guide
https://sites.google.com/site/linuxencoding/x264-ffmpeg-mapping x264 FFmpeg Options Guide Please Not ...
- 程序移植到VS2010,编译成功但是无法启动lib文件
今天遇到的这个问题,是由于解决方案下有多个项目,其中包含生成库的项目,也有可执行程序的项目 解决方法:邮件解决方案,属性-通用属性-启动项目进行设置就OK了,我的是设置单启动项目为包含可执行程序的项目 ...
- BestCoder3 1002 BestCoder Sequence(hdu 4908) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4908 题目意思:给出 一个从1~N 的排列你和指定这个排列中的一个中位数m,从这个排列中找出长度为奇数 ...
- Linux下启动mongodb
完成安装mongodb(略) 创建数据目录: # mkdir /data/mongo 创建配置文件 # vi /data/mongo/mongodb.cnf dbpath=/data/mongo/ l ...
- C/C++获取Windows系统CPU和内存及硬盘使用情况
//1.获取Windows系统内存使用率 //windows 内存 使用率 DWORD getWin_MemUsage(){ MEMORYSTATUS ms; ::GlobalMemoryStatus ...
- js正则匹配字符串
这里我第一时间想到的就是用 js 的search 和 match ,其中最常见的是match: 1. str.search(regexp):search()方法不支持全局搜索,因为会忽略正则表达式参数 ...