silverlight
发布时间:2022-05-04 发布网站:大佬教程 code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在silverlight中定制自己的MessageBox(消息框),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
概述
在silverlight中,如果想
使用“消息框”可使用下面的方法,即:HtmlPage.Window.Alert("消息框内容")。 如果想要显示更加复杂的内容或定义消息框样式的话,基本上没有什么好的方法。最近在网上看到了一篇文章, 该文章的作者也谈到了上面所说的话题,相关
链接如下: The Curious Incident of the
messageBox in the S
在silverlight中,如果想
使用“消息框”可使用下面的
方法,即:HtmlPage.Window.Alert("消息框
内容")。
如果想要
显示更加复杂的
内容或定义消息框样式的话,基本上没有什么好的
方法。最近在网上看到了一篇
文章,
该
文章的作者也谈到了上面所说的话题,相关
链接如下:
The Curious Incident of the MessageBox in the Silverlight App
在线演示如下:
http://silverlight.services.live.com/invoke/72193/messagebox/iframe.html
首先是普通样式:
接着是显示图形样式:
使用新的样式风格:
首先请下载本文中的源码(本人已部分
修改了原文中的源码和
相应的样式,以
便进行DEMO演示)。下面是相
应的类图和说明:
接下来,将会以这个类图来逐个解释相应类结构信息,首先看一下
message
BoxControls(相应
内容见注释):
Code
/// <sumMary>
/// 消息框结果
/// </sumMary>
@H_675_119@public @H_
675_119@enum
messageBoxResult
{
Yes, //是
No, //否
Cancel //取消
}
/// <sumMary>
/// 消息事件参数
/// </sumMary>
@H_
675_119@public
@H_
675_119@class
messageBoxResultEventArgs : EventArgs
{
@H_
675_119@public
messageBoxResult result { @H_
675_119@get
; @H_
675_119@set
; }
@H_
675_119@public
@H_
675_119@ob
ject
AsyncState { @H_
675_119@get
; @H_
675_119@set
; }
}
/// <sumMary>
/// 消息框控件类,该模板包括三个组件(三个Button和一个Panel)
/// </sumMary>
[TemplatePart(Name = rootElement, Type = @H_
675_119@typeof
(Panel))]
[TemplatePart(Name = YesButtonElement, Type = @H_
675_119@typeof
(Button))]
[TemplatePart(Name = NoButtonElement, Type = (@H_
675_119@typeof
(Button)))]
[TemplatePart(Name = CancelButtonElement, Type = (@H_
675_119@typeof
(Button)))]
@H_
675_119@public
@H_
675_119@class
messageBoxControl : ContentControl
{
@H_
675_119@public
@H_
675_119@event
EventHandler<messageBoxResultEventArgs> messageBoxDismissed;
@H_
675_119@public
messageBoxControl()
{
DefaultStyleKey = @H_
675_119@typeof
(messageBoxControl);
}
@H_
675_119@public
@H_
675_119@override
@H_
675_119@void
OnApplyTemplate()
{
@H_
675_119@#region
取消之前的事件绑定
@H_
675_119@if
(yesButton != @H_
675_119@null
)
{
yesButton.Click -= OnYesButton;
}
@H_
675_119@if
(noButton != @H_
675_119@null
)
{
noButton.Click -= OnNoButton;
}
@H_
675_119@if
(cancelButton != @H_
675_119@null
)
{
cancelButton.Click -= OnCancelButton;
}
@H_
675_119@#endregion
rootElement = @H_
675_119@base
.GetTemplateChild(RootElement) @H_
675_119@as
Panel;
yesButton = @H_
675_119@base
.GetTemplateChild(YesButtonElement) @H_
675_119@as
Button;
noButton = @H_
675_119@base
.GetTemplateChild(NoButtonElement) @H_
675_119@as
Button;
cancelButton = @H_
675_119@base
.GetTemplateChild(CancelButtonElement) @H_
675_119@as
Button;
@H_
675_119@#region
如果grid中有相应元素时,则绑定相应事件(详见下面的代码)
@H_
675_119@if
(yesButton != @H_
675_119@null
)
{
yesButton.Click += OnYesButton;
}
@H_
675_119@if
(noButton != @H_
675_119@null
)
{
noButton.Click += OnNoButton;
}
@H_
675_119@if
(cancelButton != @H_
675_119@null
)
{
cancelButton.Click += OnCancelButton;
}
@H_
675_119@#endregion
}
@H_
675_119@void
OnYesButton(@H_
675_119@ob
ject
sender, EventArgs args)
{
FiredisR_287_11845@issed(messageBoxResult.Yes);
}
@H_
675_119@void
OnNoButton(@H_
675_119@ob
ject
sender, EventArgs args)
{
FiredisR_287_11845@issed(messageBoxResult.No);
}
@H_
675_119@void
OnCancelButton(@H_
675_119@ob
ject
sender, EventArgs args)
{
FiredisR_287_11845@issed(messageBoxResult.Cancel);
}
/// <sumMary>
/// 调用绑定的事件,并传递相应参数
/// </sumMary>
/// <param NAME="result"></param>
@H_
675_119@void
FiredisR_287_11845@issed(messageBoxResult result)
{
//当绑定的事件不为空时(绑定部分参见messageBox的构造函数)
@H_
675_119@if
(messageBoxDismissed != @H_
675_119@null
)
{
messageBoxDismissed(@H_
675_119@this
, @H_
675_119@new
messageBoxResultEventArgs() { result = result });
}
}
Button yesButton;
Button noButton;
Button cancelButton;
Panel rootElement;
@H_
675_119@#region
赋值信息参见generic.xaml中的"x:Name"声明
@H_
675_119@public
@H_
675_119@const
@H_
675_119@
String
rootElement = @H_
944_712@"@H_
944_712@RootElement@H_
944_712@"
;
@H_
675_119@public
@H_
675_119@const
@H_
675_119@
String
YesButtonElement = @H_
944_712@"@H_
944_712@YesButtonElement@H_
944_712@"
;
@H_
675_119@public
@H_
675_119@const
@H_
675_119@
String
NoButtonElement = @H_
944_712@"@H_
944_712@NoButtonElement@H_
944_712@"
;
@H_
675_119@public
@H_
675_119@const
@H_
675_119@
String
CancelButtonElement = @H_
944_712@"@H_
944_712@CancelButtonElement@H_
944_712@"
;
@H_
675_119@#endregion
}
而
message
Box这个控件使用封装类结构如下(相关
内容见注释):
Code
@H_675_119@public @H_
675_119@class
UserControlContentAccessor : UserControl
{
/// <sumMary>
/// 获取当前UserControl的ContentProperty属性
/// </sumMary>
/// <param NAME="uc">当前UserControl</param>
/// <returns>ContentProperty属性</returns>
@H_
675_119@public
@H_
675_119@static
UIElement GetContent(UserControl uC)
{
@H_
675_119@return
((UIElement)uc.GetValue(UserControl.ContentProperty));
}
/// <sumMary>
/// 设置当前UserControl的ContentProperty属性
/// </sumMary>
/// <param NAME="uc">当前UserControl</param>
/// <param NAME="element">要设置的内容属性</param>
@H_
675_119@public
@H_
675_119@static
@H_
675_119@void
SetContent(UserControl uc, UIElement element)
{
uc.SETVALue(UserControl.ContentProperty, element);
}
}
/// <sumMary>
/// 消息框类,该类可以看成是对"消息框控件类"使用封装(封装了事件绑定和内容信息)
/// </sumMary>
@H_
675_119@public
@H_
675_119@static
@H_
675_119@class
messageBox
{
/// <sumMary>
/// 实际页面视图中的元素(用于当消息框关闭后,还原页面元素时使用)
/// </sumMary>
@H_
675_119@private
@H_
675_119@static
UIElement realVisual;
/// <sumMary>
/// 用于绑定当前页面中根元素节点
/// </sumMary>
@H_
675_119@private
@H_
675_119@static
Grid parentGrid;
/// <sumMary>
/// 状态值
/// </sumMary>
@H_
675_119@private
@H_
675_119@static
@H_
675_119@ob
ject
asyncState;
/// <sumMary>
/// 用户绑定回调事件属性
/// </sumMary>
@H_
675_119@private
@H_
675_119@static
EventHandler<messageBoxResultEventArgs> userCallBACk;
@H_
675_119@public
@H_
675_119@static
@H_
675_119@void
ShowAsync(@H_
675_119@ob
ject
content)
{
ShowAsync(content, @H_
675_119@null
);
}
@H_
675_119@public
@H_
675_119@static
@H_
675_119@void
ShowAsync(@H_
675_119@ob
ject
content,
EventHandler<messageBoxResultEventArgs> callBACk)
{
ShowAsync(content, @H_
675_119@null
, callBACk);
}
@H_
675_119@public
@H_
675_119@static
@H_
675_119@void
ShowAsync(@H_
675_119@ob
ject
content, @H_
675_119@ob
ject
userState, userState, callBACk,
EventHandler<messageBoxResultEventArgs> callBACk, Style controlTemplatE)
{
messageBoxControl control = @H_
675_119@new
messageBoxControl();
control.Content = content;
//绑定指定样式
@H_
675_119@if
(controlTemplate != @H_
675_119@null
)
{
control.Style = controlTemplate;
}
ShowAsync(control, callBACk);
}
@H_
675_119@public
@H_
675_119@static
@H_
675_119@void
ShowAsync(messageBoxControl control,
EventHandler<messageBoxResultEventArgs> callBACk)
{
UserControl uc = Application.Current.RootVisual @H_
675_119@as
UserControl;
@H_
675_119@if
(uc != @H_
675_119@null
)
{
asyncState = userState;//用户状态绑定
userCallBACk = callBACk;//回调方法
realVisual = UserControlContentAccessor.GetContent(uc);
realVisual.IsHitTestVisible = @H_
675_119@
false
; //使底层控件点击不可见
parentGrid = @H_
675_119@new
Grid();//声明一个Grid对象,用于加载新的内容
UserControlContentAccessor.SetContent(uc, parentGrid);
parentGrid.Children.Add(realVisual); //加载realVisual内容(注:此处内容中的控制已不支持点击了)
parentGrid.Children.Add(control); //加载消息框实例,后加载的显示在上(前)面
control.messageBoxDismissed += OnDismissed; //绑定要处理的事件,该事件会在点击消息框中的"yes"或"no"按钮时执行
}
}
@H_
675_119@static
@H_
675_119@void
OnDismissed(@H_
675_119@ob
ject
sender, messageBoxResultEventArgs E)
{
messageBoxControl control = sender @H_
675_119@as
messageBoxControl;
UserControl uc = Application.Current.RootVisual @H_
675_119@as
UserControl;
@H_
675_119@if
(uc != @H_
675_119@null
)
{ //清除之前的页面UI元素,并还原页面初始时的元素设置
parentGrid.Children.Clear();
realVisual.IsHitTestVisible = @H_
675_119@true
;
UserControlContentAccessor.SetContent(uc, realVisual);
}
@H_
675_119@if
(control != @H_
675_119@null
)
{
control.messageBoxDismissed -= OnDismissed;
}
@H_
675_119@try
{
@H_
675_119@if
(userCallBACk != @H_
675_119@null
)
{
//执行用户绑定的事件(并传递事件参数)
userCallBACk(@H_
675_119@null
, @H_
675_119@new
messageBoxResultEventArgs()
{
result = e.Result,
AsyncState = asyncState
});
}
}
@H_
675_119@finally
{
realVisual = @H_
675_119@null
;
parentGrid = @H_
675_119@null
;
asyncState = @H_
675_119@null
;
userCallBACk = @H_
675_119@null
;
}
}
}
其实通过上面的类,我们
可以看出作者是如何在
当前页面中
显示消息框信息的,也就是上面
代码段里
的如下
代码:
realVisual
=
UserControlContentAccessor.GetContent(uc);
realVisual.IsHitTestVisible
=
@H_
675_119@
false
;
//
使底层控件点击不可见
parentGrid
=
@H_
675_119@new
Grid();
//
声明一个Grid对象,用于加载新的内容
UserControlContentAccessor.SetContent(uc, parentGrid);
parentGrid.Children.Add(realVisual);
//
加载realVisual内容(注:此处内容中的控制已不支持点击了)
parentGrid.Children.Add(control);
//
加载消息框实例,后加载的显示在上(前)面
也就是通过realVisual来保存原有的
页面元素信息,
然后重新按指定顺序(先realVisual再
messagecontrol)
加载UIElement来实现
显示消息框的方式,当然这种有HACK味道的做法到底
效果好不好,连原作者都表示怀疑,他
本人也感觉还应有更好的Solution。
当然realVisual变量的
一个重要用处在于当消息框被
关闭时,用它来还原
页面中的元素,而这块
代码就是上面
所说的OnDismissed
方法所做的事了,
代码如下:
OnDismissed (
@H_
675_119@ob
ject
sender, messageBoxResultEventArgs E)
{
messageBoxControl control
=
sender
@H_
675_119@as
messageBoxControl;
UserControl uc
=
Application.Current.RootVisual
@H_
675_119@as
UserControl;
@H_
675_119@if
(uc
!=
@H_
675_119@null
)
{
//
清除之前的页面UI元素,并还原页面初始时的元素设置
parentGrid.Children.Clear();
realVisual.IsHitTestVisible
=
@H_
675_119@true
;
UserControlContentAccessor.SetContent(uc, realVisual);
}
}
这样,我们可以在应用程序中使用该类来
显示相应的消息框了,其声明和使用
代码如下:
//
普通样式
@H_
675_119@void
OnNormalClick(
@H_
675_119@ob
ject
sender, EventArgs args)
{
messageBox.ShowAsync(
@H_
944_712@"
@H_
944_712@简单
调用, 无回调, 无状态, 无样式!
@H_
944_712@"
);
//
下面注释的代码包括状态和回调事件
//
messageBox.ShowAsync("As prevIoUsly but with a callBACk - hit NO", (s, E) =>
//
{
//
Debug.Assert(e.Result == messageBoxResult.No);
//
});
//
messageBox.ShowAsync("As prevIoUsly but with state - hit YES", 101, E) =>
//
{
//
Debug.Assert((e.Result == messageBoxResult.Yes) && ((int)e.AsyncState == 101));
//
});
}
//
显示图形
@H_
675_119@void
OnShapeClick(
@H_
675_119@ob
ject
sender, EventArgs args)
{
messageBox.ShowAsync(
@H_
675_119@new
Ellipse()
{
Width
=
80
,
Height
=
80
,
Fill
=
@H_
675_119@new
SolidColorBrush(Colors.Green)
});
}
//
转换样式
@H_
675_119@void
OnChangeStyleClick(
@H_
675_119@ob
ject
sender, EventArgs args)
{
Style myStyle
=
@H_
675_119@this
.resources[
@H_
944_712@"
@H_
944_712
@myStyle
@H_
944_712@"
]
@H_
675_119@as
Style;
messageBox.ShowAsync(
@H_
944_712@"
@H_
944_712@使用
一个不同的样式
@H_
944_712@"
,
101
,
//
状态
(s, E)
=>
//
处理事件
{
@H_
675_119@if
(e.Result
==
messageBoxResult.No
&&
((
@H_
675_119@int
)e.AsyncState
==
101
))
{
HtmlPage.Window.Alert(
@H_
944_712@"
@H_
944_712@您点击了No按钮
@H_
944_712@"
);
}
@H_
675_119@if
(e.Result
==
messageBoxResult.Yes)
{
HtmlPage.Window.Alert(
@H_
944_712@"
@H_
944_712@您点击了Yes按钮
@H_
944_712@"
);
}
},
myStylE);
}
说到这里,还有
一个内容没有介绍,也就是作者所定义的两个样式
文件,其中之一被放置到了generic.xaml
中,以
便做了控制
默认加载样式,其绑定直接在
message
BoxControl构造
函数中完成,如下:
@H_
675_119@public
messageBoxControl()
{
DefaultStyleKey
=
@H_
675_119@typeof
(messageBoxControl);
}
而另外的样式被放在了page.xaml中,以
便于程序运行时访问,这里就不多作介绍了。不过本人已
修改了这两
个样式中的一些数值,主要是为了
显示时比例更好看一些。
好了,今天的
内容就先到这里了。
tag : silverlight,
messagex
Box
作者: 代震军,daizhj
原帖
链接:
http://files.cnblogs.com/daizhj/silverlight_MessageBox.rar
大佬总结
以上是大佬教程为你收集整理的在silverlight中定制自己的MessageBox(消息框)全部内容,希望文章能够帮你解决在silverlight中定制自己的MessageBox(消息框)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。