Win10 UWP Popup

来源:互联网 发布:手机淘宝怎么删晒图 编辑:程序博客网 时间:2024/06/16 01:28


简单Popup

创建文件 -> XAML ->  空白页(BlankPage) -> 随便起名为 Page000.xaml

<Page    x:Class="App1.Page000"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:local="using:App1"    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    mc:Ignorable="d">    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">        <StackPanel>            <Button Content="show Popup (using offset)" Click="ShowPopupOffsetClicked" />        </StackPanel>        <Popup VerticalOffset="10" HorizontalOffset="200" x:Name="StandardPopup">            <Border BorderBrush="{StaticResource ApplicationForegroundThemeBrush}" BorderThickness="2" Width="200" Height="200">                <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">                    <TextBlock Text="Simple Popup" FontSize="24.667" HorizontalAlignment="Center"/>                    <Button Content="Close" Click="ClosePopupClicked" HorizontalAlignment="Center"/>                </StackPanel>            </Border>        </Popup>    </Grid></Page>

Page000.xaml.cs:

namespace App1{    /// <summary>    /// 可用于自身或导航至 Frame 内部的空白页。    /// </summary>    public sealed partial class Page000 : Page    {        public Page000()        {            this.InitializeComponent();        }        private void ShowPopupOffsetClicked(object sender, RoutedEventArgs e)        {            if (!StandardPopup.IsOpen) { StandardPopup.IsOpen = true; }        }        private void ClosePopupClicked(object sender, RoutedEventArgs e)        {            if (StandardPopup.IsOpen) { StandardPopup.IsOpen = false; }        }    }}








自定义Popup:

创建文件 -> XAML -> 用户控件(UserControl) -> 随便起名为 PopupUserControl.xaml

<UserControl    x:Class="App1.PopupUserControl"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:local="using:App1"    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    mc:Ignorable="d"    d:DesignHeight="300"    d:DesignWidth="400">    <Grid Background="#f5f5f5">        <StackPanel>            <TextBlock Text="Type some input" FontSize="24.667"/>            <TextBox Width="300" Height="55"/>            <Button Content="Save" Click="Button_Click"/>        </StackPanel>    </Grid></UserControl>

PopupUserControl.xaml.cs:

namespace App1{    public sealed partial class PopupUserControl : UserControl    {        public PopupUserControl()        {            this.InitializeComponent();        }        private void Button_Click(object sender, RoutedEventArgs e)        {            Popup p = this.Parent as Popup;            if (p != null)            {                p.IsOpen = false;            }        }    }}

加载方式:

创建文件 -> XAML -> 空白页(BlankPage) -> 随便起名为 Page001.xaml

<Page    x:Class="App1.Page001"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:local="using:App1"    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    mc:Ignorable="d">    <Grid x:Name="Output" HorizontalAlignment="Left" VerticalAlignment="Top">        <StackPanel>            <Button Content="Show Popup (using offset)" Click="ShowPopupOffsetClicked" />        </StackPanel>        <Popup x:Name="ParentedPopup" HorizontalOffset="200" VerticalOffset="200">            <local:PopupUserControl />        </Popup>    </Grid></Page>

Page001.xaml.cs:

namespace App1{    /// <summary>    /// 可用于自身或导航至 Frame 内部的空白页。    /// </summary>    public sealed partial class Page001 : Page    {        public Page001()        {            this.InitializeComponent();        }        private void ShowPopupOffsetClicked(object sender, RoutedEventArgs e)        {            if (!ParentedPopup.IsOpen)            {                ParentedPopup.IsOpen = true;            }        }    }}










资料来源:

点击打开链接

1 0
原创粉丝点击