新时尚Windows8开发(17):自己也来做一做弹出对话框

2024-03-20 01:08

本文主要是介绍新时尚Windows8开发(17):自己也来做一做弹出对话框,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Windows Store应用程序有点像Web页面,一般而言,我们只有一个窗口,不会像传统的桌面应用程序那样,使用多个子窗体。

前面我们也讨论过MessageDialog类用来弹出对话框,但是,它只能显示文本信息,如果一些复杂的内容,就不能满足我们的要求了。本来考虑Windows.UI.Core命名空间下的CoreWindowDialog类,但,后来发现这个类貌似一个空壳子,反正我是不知道这个类是怎么用的,那么,咋办呢,有没有其它方法呢?

其实所谓的弹出层,说白了,其本质就是一个控件,这个控件我们如果玩过WPF,会觉得灰常熟悉,谁呢?就是这位帅哥——Popup(位于Windows.UI.Xaml.Controls.Primitives下),怎么样,似曾相识吧?

 

我们总结,每个弹出层,无论其内容是什么,都有以下共同特点:

1、有一个半透明的层覆盖在现有UI上,以阻止用户操作弹出对话框下的UI元素。

2、除了内容不同,弹出层的大小位置以及背景都是一个样的。

 

这样的话,我们不妨自己来写一个控件,这个控件具有内容模型,这样,在弹出框中需要什么内容,我们只需设置其Content就行了。

 

下面,我们就实地演练一下吧。

1、用动VS,新建一个空白页面项目。

2、在“解决方案资源管理器”上右击,从菜单中选择“添加”-“新建项”。

 

在接下来的窗口中选择“模板化控件”,输入控件名字,确定。

 

让后你会看到下面这个。

 

3、打开Generic.xaml,先为控件定义好模板。

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1">
<Style TargetType="local:PopControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:PopControl">
<Grid>
<Rectangle Canvas.ZIndex="0" Fill="Black" Opacity="0.4"/>
<ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" ContentTemplate="{TemplateBinding ContentTemplate}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>


模板一点也不复杂,接下来是核心部分,就是控件的逻辑代码。

using System;
using System.Collections.Generic;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media.Animation;
namespace App1
{
public class PopControl : ContentControl
{
Popup m_pop = null;
public PopControl()
{
this.DefaultStyleKey = typeof(PopControl);
// 弹出层的宽度等于窗口的宽度
this.Width = Window.Current.Bounds.Width;
// 弹出层的高度等于窗口的高度
this.Height = Window.Current.Bounds.Height;
this.HorizontalContentAlignment = Windows.UI.Xaml.HorizontalAlignment.Stretch;
this.m_pop = new Popup();
// 将当前控件作为Popup的Child
this.m_pop.Child = this;
}
/// <summary>
/// 获取Popup的ChildTransitions集合
/// </summary>
public TransitionCollection PopTransitions
{
get
{
if (m_pop.ChildTransitions == null)
{
m_pop.ChildTransitions = new TransitionCollection();
}
return m_pop.ChildTransitions;
}
}
/// <summary>
/// 显示弹出层
/// </summary>
public virtual void ShowPop()
{
if (this.m_pop != null)
{
this.m_pop.IsOpen = true;
}
}
/// <summary>
/// 隐藏弹出层
/// </summary>
public virtual void HidePop()
{
if (this.m_pop != null)
{
this.m_pop.IsOpen = false;
}
}
}
}


大至原理是这样的,把控件的大小设置和当前窗口的大小相等,这样确保弹出层可以完全覆盖在UI上。接着把当前控件作为Popup控件的Child元素,而控件的显示与隐藏,其实就是设置Popup的IsOpen属性。

为了方便派生类扩展,ShowPop和HidePop方法都用了virtual关键字。

 

4、接下来,新增一个用户控件,它作为弹出层的内容。

【XAML】

<UserControl
x:Class="App1.ucReg"
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="700"
d:DesignWidth="900">
<UserControl.Resources>
<Style x:Key="t" TargetType="TextBlock">
<Setter Property="FontSize" Value="20"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="6,0,21,0"/>
</Style>
<Style x:Key="w" TargetType="FrameworkElement">
<Setter Property="Margin" Value="5,7,0,5"/>
</Style>
</UserControl.Resources>
<Grid VerticalAlignment="Center" Background="Green">
<Grid Margin="0,50,0,32" Width="560">
<StackPanel>
<TextBlock Text="用户注册" Margin="0,0,0,34" Style="{StaticResource PageHeaderTextStyle}"/>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Style="{StaticResource t}" Grid.Row="0" Grid.Column="0" Text="姓名:"/>
<TextBox Grid.Row="0" Grid.Column="1" Style="{StaticResource w}"/>
<TextBlock Grid.Row="1" Grid.Column="0" Text="性别:" Style="{StaticResource t}"/>
<ComboBox Grid.Row="1" Grid.Column="1" Style="{StaticResource w}">
<ComboBoxItem Content="男" IsSelected="True"/>
<ComboBoxItem Content="女" />
</ComboBox>
<TextBlock Style="{StaticResource t}" Text="电邮:" Grid.Row="2" Grid.Column="0"/>
<TextBox Style="{StaticResource w}" Grid.Row="2" Grid.Column="1"/>
<TextBlock Style="{StaticResource t}" Text="手机:" Grid.Row="3" Grid.Column="0"/>
<TextBox Grid.Column="1" Grid.Row="3" Style="{StaticResource w}"/>
<TextBlock Text="地址:" Style="{StaticResource t}" Grid.Row="4" Grid.Column="0"/>
<TextBox Style="{StaticResource w}" Grid.Row="4" Grid.Column="1"/>
</Grid>
<StackPanel Orientation="Horizontal" Margin="0,15,0,7" HorizontalAlignment="Center">
<Button Content="确定" Padding="45,5" Click="onClick"/>
<Button Content="取消" Padding="45,5" Margin="22,0,0,0" Click="onClick"/>
</StackPanel>
</StackPanel>
</Grid>
</Grid>
</UserControl>


【C#】

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;
namespace App1
{
public sealed partial class ucReg : UserControl
{
PopControl _pc;
public ucReg(PopControl c)
{
this.InitializeComponent();
_pc = c;
}
private void onClick(object sender, RoutedEventArgs e)
{
if (_pc != null)
{
_pc.HidePop();
}
}
}
}


为了方便控制PopControl,在用户控件中声明一个PopControl,在用户控件类的构造函数中传递。

 

5、最后,我们在MainPage.xaml中测试这个弹出框。

【XAML】

<Page
x:Class="App1.MainPage"
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="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Button Content="弹出对话框" Click="onPop"/>
</Grid>
</Page>


【C#】

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;
namespace App1
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void onPop(object sender, RoutedEventArgs e)
{
PopControl pc = new PopControl();
ucReg uc = new ucReg(pc);
pc.Content = uc;
pc.ShowPop();
}
}
}


 

好了,现在可以运行,效果就像下图所示,还不错吧。

 

 

这篇关于新时尚Windows8开发(17):自己也来做一做弹出对话框的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/827797

相关文章

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

使用Go语言开发一个命令行文件管理工具

《使用Go语言开发一个命令行文件管理工具》这篇文章主要为大家详细介绍了如何使用Go语言开发一款命令行文件管理工具,支持批量重命名,删除,创建,移动文件,需要的小伙伴可以了解下... 目录一、工具功能一览二、核心代码解析1. 主程序结构2. 批量重命名3. 批量删除4. 创建文件/目录5. 批量移动三、如何安

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

基于Python开发PPTX压缩工具

《基于Python开发PPTX压缩工具》在日常办公中,PPT文件往往因为图片过大而导致文件体积过大,不便于传输和存储,所以本文将使用Python开发一个PPTX压缩工具,需要的可以了解下... 目录引言全部代码环境准备代码结构代码实现运行结果引言在日常办公中,PPT文件往往因为图片过大而导致文件体积过大,

使用DeepSeek API 结合VSCode提升开发效率

《使用DeepSeekAPI结合VSCode提升开发效率》:本文主要介绍DeepSeekAPI与VisualStudioCode(VSCode)结合使用,以提升软件开发效率,具有一定的参考价值... 目录引言准备工作安装必要的 VSCode 扩展配置 DeepSeek API1. 创建 API 请求文件2.

基于Python开发电脑定时关机工具

《基于Python开发电脑定时关机工具》这篇文章主要为大家详细介绍了如何基于Python开发一个电脑定时关机工具,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 简介2. 运行效果3. 相关源码1. 简介这个程序就像一个“忠实的管家”,帮你按时关掉电脑,而且全程不需要你多做

Java中的Opencv简介与开发环境部署方法

《Java中的Opencv简介与开发环境部署方法》OpenCV是一个开源的计算机视觉和图像处理库,提供了丰富的图像处理算法和工具,它支持多种图像处理和计算机视觉算法,可以用于物体识别与跟踪、图像分割与... 目录1.Opencv简介Opencv的应用2.Java使用OpenCV进行图像操作opencv安装j

基于Qt开发一个简单的OFD阅读器

《基于Qt开发一个简单的OFD阅读器》这篇文章主要为大家详细介绍了如何使用Qt框架开发一个功能强大且性能优异的OFD阅读器,文中的示例代码讲解详细,有需要的小伙伴可以参考一下... 目录摘要引言一、OFD文件格式解析二、文档结构解析三、页面渲染四、用户交互五、性能优化六、示例代码七、未来发展方向八、结论摘要

在 VSCode 中配置 C++ 开发环境的详细教程

《在VSCode中配置C++开发环境的详细教程》本文详细介绍了如何在VisualStudioCode(VSCode)中配置C++开发环境,包括安装必要的工具、配置编译器、设置调试环境等步骤,通... 目录如何在 VSCode 中配置 C++ 开发环境:详细教程1. 什么是 VSCode?2. 安装 VSCo

C#图表开发之Chart详解

《C#图表开发之Chart详解》C#中的Chart控件用于开发图表功能,具有Series和ChartArea两个重要属性,Series属性是SeriesCollection类型,包含多个Series对... 目录OverviChina编程ewSeries类总结OverviewC#中,开发图表功能的控件是Char