Windows Phone 7 自定义弹出窗口

简介:

Windows Phone内置的MessageBox弹出窗口局限性太大,不能满足各种个性化的弹出窗口的需求,即使使用第三方的控件库也会有一些局限性,又或者封装的东西太多了,那么这时候就需要自己去根据自己的需求去自定义一个弹出窗口了。

  大概的原理就是使用Popup控件来实现弹出窗的效果,Popup控件可以把包含在其中的控件显示在最外面,从而可以把当前页面的控件都给盖住了,再加点半透明的效果,若隐若现的,一个弹窗就出来了。好吧,下面来看一下Demo。

  先看一下demo的结构。

  

  Generic.xaml

< ResourceDictionary
    xmlns
= " http://schemas.microsoft.com/winfx/2006/xaml/presentation "
    xmlns:x
= " http://schemas.microsoft.com/winfx/2006/xaml "
    xmlns:
local = " clr-namespace:MessageControl;assembly=MessageControl "
    xmlns:d
= " http://schemas.microsoft.com/expression/blend/2008 "
    xmlns:mc
= " http://schemas.openxmlformats.org/markup-compatibility/2006 "    
    mc:Ignorable
= " d "
    
>
    
< Style TargetType = " local:MyMessage " >
        
< Setter  Property = " FontFamily "  Value = " {StaticResource PhoneFontFamilyNormal} " />
        
< Setter  Property = " FontSize "  Value = " {StaticResource PhoneFontSizeNormal} " />
        
< Setter  Property = " Foreground "  Value = " {StaticResource PhoneForegroundBrush} " />
        
< Setter  Property = " Background "  Value = " Snow " />
        
< Setter  Property = " Width "  Value = " 480 "   />
        
< Setter  Property = " Height "  Value = " 800 "   />
        
< ! -- 定义模板的Template -->
        
< Setter  Property = " Template " >
            
< Setter.Value >
                
< ControlTemplate TargetType = " local:MyMessage " >
                    
< Grid VerticalAlignment = " Stretch " >
                        
< Rectangle x:Name = " backgroundRect "  Grid.Row = " 0 "  Fill = " Black "  Opacity = " 0.7 " />
                        
< Border 
                            VerticalAlignment
= " Top "  
                            BorderThickness
= " 3 "  
                            BorderBrush
= " Black " >
                            
< StackPanel Margin = " 0 " >
                                
< ContentPresenter x:Name = " body " />
                            
</ StackPanel >
                        
</ Border >
                    
</ Grid >
                
</ ControlTemplate >
            
</ Setter.Value >
        
</ Setter >
    
</ Style >
</ ResourceDictionary >
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using Microsoft.Phone.Controls;

namespace MessageControl
{
    
public  class MyMessage : ContentControl
    {
        
private  System.Windows.Controls.ContentPresenter body;
        
private  System.Windows.Shapes.Rectangle backgroundRect;
        
private   object  content;

        
public  MyMessage()
        {
            
// 这将类的styleKey设置为MyMessage,这样在模板中的style才能通过TargetType = " local:MyMessage " 与之相互绑定
            this.DefaultStyleKey 
=  typeof(MyMessage);
        }
        
// 重写OnApplyTemplate()方法获取模板样式的子控件
        
public  override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            this.body 
=  this.GetTemplateChild( " body " as  ContentPresenter;
            this.backgroundRect 
=  this.GetTemplateChild( " backgroundRect " as  Rectangle;
            InitializeMessagePrompt();
        }
        
// 使用Popup控件来制作弹窗
        internal Popup ChildWindowPopup
        {
            
get ;
            
private   set ;
        }
        
// 获取当前应用程序的UI框架PhoneApplicationFrame
        
private  static PhoneApplicationFrame RootVisual
        {
            
get
            {
                return Application.Current 
==   null  ?  null  : Application.Current.RootVisual  as  PhoneApplicationFrame;
            }
        }
        
// 弹窗的内容,定义为object,可以赋值为各种各样的控件
        
public   object  MessageContent
        {
            
get
            {
                return this.content;
            }
            
set
            {
                this.content 
=  value;
            }
        }
        
// 隐藏弹窗
        
public  void Hide()
        {
            
if  (this.body ! =   null )
            {
                
// 关闭Popup控件
                this.ChildWindowPopup.IsOpen 
=   false ;
            }
        }
        
// 判断弹窗是否打开
        
public  bool IsOpen 
        {
            
get  
            {
                return ChildWindowPopup !
=   null   &&  ChildWindowPopup.IsOpen;
            } 
        }
        
// 打开弹窗
        
public  void Show()
        {
            
if  (this.ChildWindowPopup  ==   null )
            {
                this.ChildWindowPopup 
=   new  Popup();
                this.ChildWindowPopup.Child 
=  this;
            }

            
if  (this.ChildWindowPopup ! =   null   &&  Application.Current.RootVisual ! =   null )
            {
                InitializeMessagePrompt();
                this.ChildWindowPopup.IsOpen 
=   true ;
            }
        }
        
// 初始化弹窗
        
private  void InitializeMessagePrompt()
        {
            
if  (this.body  ==   null )
                return;
            this.backgroundRect.Visibility 
=  System.Windows.Visibility.Visible;
            
// 把模板中得body控件内容赋值为你传过来的控件
            this.body.Content 
=  MessageContent;
            this.Height 
=   800 ;
        }
    }
}

  简单地创建有一个控件作为弹窗的内容,测试一下弹窗的效果,当然弹窗的控件你可以定义为你想要的各种内容。

  WindowsPhoneControl1.xaml

< UserControl x:Class = " TestMessageControl.WindowsPhoneControl1 "
    xmlns
= " http://schemas.microsoft.com/winfx/2006/xaml/presentation "
    xmlns:x
= " http://schemas.microsoft.com/winfx/2006/xaml "
    xmlns:d
= " http://schemas.microsoft.com/expression/blend/2008 "
    xmlns:mc
= " http://schemas.openxmlformats.org/markup-compatibility/2006 "
    mc:Ignorable
= " d "
    FontFamily
= " {StaticResource PhoneFontFamilyNormal} "
    FontSize
= " {StaticResource PhoneFontSizeNormal} "
    Foreground
= " {StaticResource PhoneForegroundBrush} "
    d:DesignHeight
= " 250 "  d:DesignWidth = " 480 " >
    
    
< Grid x:Name = " LayoutRoot "  Background = " LightBlue " >
        
< Button Content = " "  Height = " 72 "  HorizontalAlignment = " Left "  Margin = " 40,169,0,0 "  Name = " button1 "  VerticalAlignment = " Top "  Width = " 160 "   />
        
< Button Content = " 关闭 "  Height = " 72 "  HorizontalAlignment = " Left "  Margin = " 254,169,0,0 "  Name = " button2 "  VerticalAlignment = " Top "  Width = " 160 "  Click = " button2_Click "   />
        
< TextBlock Height = " 53 "  HorizontalAlignment = " Left "  Margin = " 54,72,0,0 "  Name = " textBlock1 "  Text = " 《深入浅出Windows Phone 7应用开发》 "  VerticalAlignment = " Top "  Width = " 369 "   />
    
</ Grid >
</ UserControl >

using System.Windows;
using System.Windows.Controls;

namespace TestMessageControl
{
    
public  partial class WindowsPhoneControl1 : UserControl
    {
        
public  WindowsPhoneControl1()
        {
            InitializeComponent();
        }

        
private  void button2_Click( object  sender, RoutedEventArgs e)
        {
            (App.Current 
as  App).myMessage.Hide();
        }
    }
}

  在App.xaml.cs中定义为全局弹窗

namespace TestMessageControl
{
    
public  partial class App : Application
    {
        ……
        
public  MyMessage myMessage  =   new  MyMessage { MessageContent  =   new  WindowsPhoneControl1() };
        ……
    }
}

  MainPage.xaml.cs

  单击事件

using Microsoft.Phone.Controls;
using MessageControl;
namespace TestMessageControl
{
    
public  partial class MainPage : PhoneApplicationPage
    {
        
public  MainPage()
        {
            InitializeComponent();
        }
        
private  void button1_Click( object  sender, RoutedEventArgs e)
        {
            (App.Current 
as  App).myMessage.Show();
        }
    }
}

  好了,来看一下运行的效果吧。








本文转自 wws5201985 51CTO博客,原文链接:http://blog.51cto.com/wws5201985/812709,如需转载请自行联系原作者
目录
相关文章
|
7天前
|
存储 安全 搜索推荐
Windows之隐藏特殊文件夹(自定义快捷桌面程序)
Windows之隐藏特殊文件夹(自定义快捷桌面程序)
|
2月前
|
XML Go 数据格式
Windows自定义后台进程并设置为开机启动
可以在`Windows`上配置任意一个可执行文件后台启动,并且设置为开机启动。
Windows自定义后台进程并设置为开机启动
基于windows10下使用bat脚本设置自定义开机启动项
基于windows10下使用bat脚本设置自定义开机启动项
1198 0
基于windows10下使用bat脚本设置自定义开机启动项
|
Windows
技巧:Windows自定义运行命令
技巧:Windows自定义运行命令
157 0
技巧:Windows自定义运行命令
|
Android开发 iOS开发 Windows
Windows Phone 寿终正寝了,这些经典机型你还记得吗?
不久前,随着最后一家WP手机厂商惠普宣布取消今后Windows Phone的研发计划,以及微软官方声明对WP8.1系统今后所有升级维护的终止,WP手机,作为曾经和安卓手机、苹果手机并驾齐驱的三大智能手机之一,正式寿终正寝。
1278 0
Windows Phone 寿终正寝了,这些经典机型你还记得吗?
|
XML 开发框架 前端开发
Windows Phone快速入门需掌握哪些能力
在此之前,先普及下Windows Phone的概念和开发工具的介绍。 Windows Phone是微软公司开发的手机操作系统,它将微软旗下的Xbox Live游戏、Xbox Music音乐与独特的视频体验集成至手机中。2012年6月21日,微软正式发布Windows Phone 8,采用和Windows 8相同的Windows NT内核,同时也针对市场的Windows Phone 7.5发布Windows Phone 7.8。
135 0
Windows Phone快速入门需掌握哪些能力
|
编解码 前端开发 JavaScript
Windows Phone 下开发 LBS 应用
基于位置的服务(Location Based Service,LBS),它是通过电信移动运营商的无线电通讯网络(如GSM网、CDMA网)或外部定位方式(如GPS)获取移动终端用户的位置信息(地理坐标,或大地坐标),在GIS(Geographic Information System,地理信息系统)平台的支持下,为用户提供相应服务的一种增值业务。
164 0
|
移动开发 Android开发 开发者
Windows Phone 8.1 新功能汇总 开发者预览版开放下载
在Build 2014大会上,微软正式发布了传闻已久的Windows Phone 8.1系统,所有的Windows Phone 8手机都可以升级,微软这次可谓是十分厚道。虽然并非迭代升级,但WP 8.1还是拥有很多重大更新,对于微软进一步完善移动平台拥有积极的意义。下面,就一起来了解一下WP 8.1的主要新特性。
234 0
Windows Phone 8.1 新功能汇总 开发者预览版开放下载