开发过程中难免要使用到消息框,然而系统提供的MessageBox却难以满足许多需求。一、MessageBox的背景颜色无法更改,这就无法满足需求要求的消息框颜色。二、MessageBox的提示形式过于单一,难以满足包含ListPicker、CheckBox等方式的弹出框,而CustomMessageBox的模版中包含了ContentPresenter,可以承载各种各样的内容控件。在多方面考虑之后,于是决定选择Toolkit的CustomMessageBox作为应用的弹出提示框。

然而在使用过程中发现CustomMessageBox还是存在些许问题的。

首先,当应用的SystemTray系统托盘的Opacity(透明度)为0的时候,消息框弹出的时候系统托盘仍然是透明的,很难看有木有啊!

为了处理掉这个问题,不得不对CustomMessageBox进行重载。

        double oldOpacity;
public MyCustomMessageBox():
base()
{
this.Dismissed += MyCustomMessageBox_Dismissed;
} void MyCustomMessageBox_Dismissed(object sender, DismissedEventArgs e)
{
SystemTray.Opacity = oldOpacity;
} public void Show()
{
oldOpacity = SystemTray.Opacity;
SystemTray.Opacity = 1;
this.Show();
}

然而,这还是比较小的问题。需求要求的消息框是要不受系统的主题背景影响的,而CustomMessageBox的遮罩背景却会在手机主题背景为白色的时候,遮罩背景变成透明的淡白色,这与应用自身设计的皮肤太不搭调了,很难看。这要如何处理掉呢?查看了CustomMessageBox的模版,发现模版中并没有有关于遮罩背景的部分。

<Style TargetType="controls:CustomMessageBox">
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilyNormal}"/>
<Setter Property="FontSize" Value="{StaticResource PhoneFontSizeNormal}"/>
<Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
<Setter Property="Background" Value="{StaticResource PhoneChromeBrush}"/>
<Setter Property="Margin" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:CustomMessageBox">
<Grid
Background="{TemplateBinding Background}"
HorizontalAlignment="Stretch">
<Grid
Width="480"
HorizontalAlignment="Left">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel
Grid.ColumnSpan="2"
Margin="0,0,0,18">
<TextBlock
x:Name="TitleTextBlock"
Text="{TemplateBinding Title}"
Foreground="{TemplateBinding Foreground}"
Visibility="Collapsed"
Margin="24,16,24,-6"
FontFamily="{StaticResource PhoneFontFamilySemiBold}"/>
<TextBlock
x:Name="CaptionTextBlock"
Text="{TemplateBinding Caption}"
Foreground="{TemplateBinding Foreground}"
Visibility="Collapsed"
Margin="24,8,24,0"
FontSize="{StaticResource PhoneFontSizeLarge}"
FontFamily="{StaticResource PhoneFontFamilySemiBold}"
TextWrapping="Wrap"
HorizontalAlignment="Left"/>
<TextBlock
x:Name="MessageTextBlock"
Text="{TemplateBinding Message}"
Foreground="{TemplateBinding Foreground}"
Margin="24,11,24,0"
Visibility="Collapsed"
FontSize="{StaticResource PhoneFontSizeMedium}"
FontFamily="{StaticResource PhoneFontFamilySemiLight}"
TextWrapping="Wrap"
HorizontalAlignment="Left"/>
<ContentPresenter Margin="12,0,0,0"/>
</StackPanel>
<Button
x:Name="LeftButton"
Grid.Row="1" Grid.Column="0"
Content="{TemplateBinding LeftButtonContent}"
IsEnabled="{Binding IsLeftButtonEnabled}"
Foreground="{TemplateBinding Foreground}"
Margin="12,0,0,12"
Visibility="Collapsed"
controls:TiltEffect.IsTiltEnabled="True"/>
<Button
x:Name="RightButton"
Grid.Row="1" Grid.Column="1"
Content="{TemplateBinding RightButtonContent}"
IsEnabled="{Binding IsRightButtonEnabled}"
Foreground="{TemplateBinding Foreground}"
Margin="0,0,12,12"
Visibility="Collapsed"
controls:TiltEffect.IsTiltEnabled="True"/>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

无奈之中只好去看CustomMessageBox的源码,最终发现遮罩的这一层原来是这样实现的。首先创建一个Grid容器,然后再创建一个Rectangle对象作为遮罩层,并且Fill值为系统的PhoneBackgroundColor这个值。将遮罩层Add到Grid容器中,将弹出框Add到Grid容器中,最后在将Grid容器添加到Popup然后弹出。那么可不可以这样做呢,在后台中找到遮罩的Rectangle,将其Fill固定下来。

        public void Show()
{
oldOpacity = SystemTray.Opacity;
SystemTray.Opacity = 1;
this.Show();
//获取Grid容器
Grid container = this.Parent as Grid;
//可视化树中查找Rectangle遮罩层
Rectangle rec = BV_ToolAPI.FindFirstChildOfType<Rectangle>(container);
rec.Fill = new SolidColorBrush(Color.FromArgb(0x99, 0, 0, 0));
}

Ok,这样,CustomMessageBox就不会再根据系统的主题背景颜色而变化了。

CustomMessageBox使用总结的更多相关文章

  1. 准备.Net转前端开发-WPF界面框架那些事,UI快速实现法

    题外话 打开博客园,查看首页左栏的”推荐博客”,排名前五的博客分别是(此处非广告):Artech.小坦克.圣殿骑士.腾飞(Jesse).数据之巅.再看看它们博客的最新更新时间:Artech(2014- ...

  2. 《深入理解Windows Phone 8.1 UI控件编程》基于最新的Runtime框架

    <深入理解Windows Phone 8.1 UI控件编程>本书基于最新的Windows Phone 8.1 Runtime SDK编写,全面深入地论述了最酷的UI编程技术:实现复杂炫酷的 ...

  3. wp8使用Beetle.NetPackage实现基于TCP通讯的订单查询

    在新版本的Beetle.NetPackage中提供了对Protobuf和Controller的支持,所以在WP8下使用Beetle.NetPackage进行基于TCP的数据交互则一件非常简单事情.下面 ...

  4. 自定义SWT控件六之自定义Tab

    6.自定义tab 本章节提供的自定义tab 分为两类 tab上带删除按钮和添加按钮,可删除tab和添加tab tab不可删除和添加 6.1 不可删除tab package com.view.contr ...

  5. 自定义SWT控件五之自定义穿梭框

    5.自定义穿梭框 package com.view.control.shuttlebox; import java.util.ArrayList; import java.util.HashMap; ...

随机推荐

  1. linux 安装svn最新版本

    一.安装svn yum install -y subversion-* 结果为Complete就为正确 查看svn版本号 svnserve --version 1.6.11版本 mkdir /opt/ ...

  2. 3.5html学习笔记之框模型,盒子模型

    块元素:block 两个相邻的元素外边距,两者之间取最大. 浮动(定位机制针对框来说): 1.position:absolute,relative,fixed 1)relative:在原来的位置上进行 ...

  3. iOS-自定义导航栏后侧滑返回功能失效

    iPhone有一个回退按钮在所有的导航条上.这是一个简单的没有文字箭头. 在一开始写项目的时候,就要做好一个准备,导航栏是自定义还是使用系统的,后期有什么改动,有什么比较特殊的需求.当然这些在更改需求 ...

  4. 在提交SVN时有时候会报svn is already locked 错误

    svn is already locked 解决方案: 如题所述经常在更新代码的时候会产生这样的问题!并且在对应的目录上操作Clean Up 没有任何的效果!如下解决方法. 在出错文件夹下,鼠标右键T ...

  5. 【阿里云产品公测】PTS压力测试WP站搜索

    [阿里云产品公测]PTS压力测试WP站搜索 作者:阿里云用户cnsjw PTS性能测试服务是一个非常非常强大的压力测试工具.可以模拟百人同时访问网站的情况,并监测ECS和RDS的各项指标,生成非常详细 ...

  6. 【开源项目8】Android开源项目分类汇总【畜生级别】

    欢迎大家推荐好的Android开源项目,可直接Commit或在 收集&提交页 中告诉我,欢迎Star.Fork :) 微博:Trinea    主页:www.trinea.cn    邮箱:t ...

  7. 【Linux/Ubuntu学习1】Linux /etc 目录详解

    /etc目录 包含很多文件.许多网络配置文件也在/etc 中. /etc/rc   or/etc/rc.d   or/etc/rc*.d   启动.或改变运行级时运行的scripts或scripts的 ...

  8. Java线程同步的方式

     java允许多线程并发控制,当多个线程同时操作一个可共享的资源变量时(如数据的增删改查),      将会导致数据不准确,相互之间产生冲突,因此加入同步锁以避免在该线程没有完成操作之前,被其他线程的 ...

  9. Unity3d之动态连接Mesh Renderer和Collider

    using UnityEngine; using System.Collections; public class dynaMesh : MonoBehaviour {     public Skin ...

  10. Jquery插件(CKEditor)

    描述 在html页面实现像word一样的编辑功能(可视化HTML编辑器) 解决方法 ckeditor插件官方网站 http://ckeditor.com/ 使用 1:去官方下载ckeditor插件,添 ...