Windows Store apps开发[50]自定义Popup

本文由BeyondVincent原创,介绍如何自定义Popup,实现画面内弹出的交互对话框,具备不同弹出效果,并能响应背景点击自动消失。关键点包括使用UserControl添加Popup并提供显示接口,以及准备相关图片资源。

注:本文由BeyondVincent(破船)原创首发

        转载请注明出处:BeyondVincent(破船)@DevDiv.com



更多内容请查看下面的帖子


[DevDiv原创]Windows 8 开发Step by Step



小引

在上一篇文章中,我利用Popup和UserControl自定义了MessageDialog。那么这篇文章我接着自定义Popup。这次的效果是在画面内弹出一个对话框,对话框会以不同的方式弹出。当点击对话框以外的位置时,对话框会自动消失。


自定义Popup的关键技术点

利用UserControl控件,在里面添加Popup,然后对外界暴露一个是否显示对话框的接口。


准备好Popup是用到的图片资源,如下两个图:


                 



制作步骤

1、新建空白工程,名称为BV_CustomPopup
2、在项目工程上单击右键,选择添加->新建项->选择用户控件->输入名称BV_CustomPopup.xaml,然后确定
3、在 BV_CustomPopup.xaml中输入如下内容:
<UserControl
    x:Class="BV_WindowsRuntimeComponent.UI.BV_CustomPopup"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:BV_WindowsRuntimeComponent.UI"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" Height="200" Width="300">

    <Popup x:Name="popup" IsLightDismissEnabled="True" >
        <Popup.ChildTransitions>
            <TransitionCollection>
                <!--<PaneThemeTransition Edge="Bottom" />-->
                <!--<AddDeleteThemeTransition/>-->
                <!--<ContentThemeTransition/>-->
                <!--<EntranceThemeTransition/>-->
                <!--<EdgeUIThemeTransition Edge="Bottom"/>-->
                <PopupThemeTransition/>
                <!--<ReorderThemeTransition/>-->
                <!--<RepositionThemeTransition/>-->
            </TransitionCollection>
        </Popup.ChildTransitions>
        <Grid Width="300" Height="200" >
            <Grid.Background>
                <ImageBrush ImageSource="../picture/PopupBackground.png"></ImageBrush>
            </Grid.Background>
            
            <Image HorizontalAlignment="Left" Height="57" Margin="220,116,0,0" VerticalAlignment="Top" Width="59" Source="ms-appx:///BV_WindowsRuntimeComponent/picture/1.jpg"/>
            <TextBlock FontSize="30"  HorizontalAlignment="Center" Height="47" Margin="38,64,0,0" TextWrapping="Wrap" Text="世界破船最大" VerticalAlignment="Top" Width="180"/>
        </Grid>
    </Popup>
</UserControl>

大家注意,上面代码中,我们可以给Popup添加如下一些动画效果:
                <PaneThemeTransition Edge="Bottom" />
                <AddDeleteThemeTransition/>
                <ContentThemeTransition/>
                <EntranceThemeTransition/>
                <EdgeUIThemeTransition Edge="Bottom"/>
                <PopupThemeTransition/>
                <ReorderThemeTransition/>
                <RepositionThemeTransition/>

4、在 BV_CustomPopup.xaml.cs文件中,添加如下代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
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 http://go.microsoft.com/fwlink/?LinkId=234236

namespace BV_WindowsRuntimeComponent.UI
{
    public sealed partial class BV_CustomPopup : UserControl
    {
        public BV_CustomPopup()
        {
            this.InitializeComponent();
        }

        public bool IsOpen
        {

            get { return popup.IsOpen; }
            set { popup.IsOpen = value; }

        }
    }
}

5、在MainPage.xaml.cs文件中,添加如下代码:

private void btn1_Copy2_Click(object sender, RoutedEventArgs e)
{
    BV_WindowsRuntimeComponent.UI.BV_CustomPopup popup = new BV_WindowsRuntimeComponent.UI.BV_CustomPopup();
    grid.Children.Add(popup);
    popup.IsOpen = true;
}

6、运行效果如下:



1 错误 异常启动,请尝试重启浏览器,或者在设置中更换其他浏览器 Target page, context or browser has been closed Browser logs: <launching> C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe --disable-field-trial-config --disable-background-networking --disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-back-forward-cache --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-background-pages --disable-component-update --no-default-browser-check --disable-default-apps --disable-dev-shm-usage --disable-features=AcceptCHFrame,AutoExpandDetailsElement,AvoidUnnecessaryBeforeUnloadCheckSync,CertificateTransparencyComponentUpdater,DeferRendererTasksAfterInput,DestroyProfileOnBrowserClose,DialMediaRouteProvider,ExtensionManifestV2Disabled,GlobalMediaControls,HttpsUpgrades,ImprovedCookieControls,LazyFrameLoading,LensOverlay,MediaRouter,PaintHolding,ThirdPartyStoragePartitioning,Translate --allow-pre-commit-input --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-backgrounding --force-color-profile=srgb --metrics-recording-only --no-first-run --enable-automation --password-store=basic --use-mock-keychain --no-service-autorun --export-tagged-pdf --disable-search-engine-choice-screen --unsafely-disable-devtools-self-xss-warnings --no-sandbox --window-position=0,0 --no-first-run --no-default-browser-check --load-extension=C:/Users/xuyishi/AppData/Roaming/OCS Desktop/downloads/extensions/scriptcat --user-data-dir=C:\Users\xuyishi\AppData\Roaming\OCS Desktop\userDataDirs\305f214bb4224685a1affa28926615fe --remote-debugging-pipe about:blank <launched> pid=3760 Call log:  - <launching> C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe --disable-field-trial-config --disable-background-networking --disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-back-forward-cache --disable-breakpad --disable-client-s
05-17
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值