[原创] MessageLine控件, 告别MessageBox的烦扰
[Original] MessageLine : Say Goodbye to MessageBox
1. I hate MessageBox, indeed. And you?
有木有觉得程序中经常弹出的MessageBox很烦? 至少我很讨厌这个, 所以自己写了一个MessageLine控件取代MessageBox.

2. MessageLine 控件关键属性
1: a. PreTitle : string, MessageLine的标题;
2: b. MessageBody : string, MessageLine的消息内容; (使用的时候最好双向绑定)
3: c. Duration: uint, 每条消息显示的时间;
效果图:![]()
3.MessageLine 源码
a. *.cs file:
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.Windows.Controls;
6: using System.Windows;
7: using System.Windows.Threading;
8:
9: namespace Vancee.Control
10: { 11: class MessageLine : Control
12: { 13: public MessageLine()
14: { 15: tmr.Tick += new EventHandler(tmr_Tick);
16: tmr.Stop();
17: }
18:
19: /// <summary>
20: /// 消息产生的时间
21: /// </summary>
22: private static DateTime _messageCreatedTime = DateTime.MinValue;
23:
24: /// <summary>
25: /// 定时器(Interval = 0.1s)
26: /// </summary>
27: static DispatcherTimer tmr = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(100) }; 28: private void tmr_Tick(object sender, EventArgs e)
29: { 30: // 超过定时
31: if (DateTime.Now - _messageCreatedTime > TimeSpan.FromSeconds(Duration))
32: { 33: tmr.Stop();
34: MessageBody = "";
35: _messageCreatedTime = DateTime.MinValue;
36: }
37: }
38:
39:
40: /// <summary>
41: /// MessageLine 标题
42: /// </summary>
43: public string PreTitle
44: { 45: get { return (string)GetValue(PreTitleProperty); } 46: set { SetValue(PreTitleProperty, value); } 47: }
48: public static readonly DependencyProperty PreTitleProperty =
49: DependencyProperty.Register("PreTitle", typeof(string), typeof(MessageLine), null); 50:
51:
52: /// <summary>
53: /// 消息正文(此属性外部需要TwoWay绑定)
54: /// </summary>
55: public string MessageBody
56: { 57: get { return (string)GetValue(MessageBodyProperty); } 58: set
59: { 60: SetValue(MessageBodyProperty, value);
61: }
62: }
63: public static readonly DependencyProperty MessageBodyProperty =
64: DependencyProperty.Register("MessageBody", typeof(string), typeof(MessageLine), null, new ValidateValueCallback(OnMessageChanged)); 65:
66: static bool OnMessageChanged(object value)
67: { 68: var msg = value as string;
69: if (msg != "")
70: { 71: _messageCreatedTime = DateTime.Now;
72: tmr.Start();
73: }
74: return true;
75: }
76:
77:
78: /// <summary>
79: /// MessageBody显示时间长度
80: /// </summary>
81: public int Duration
82: { 83: get { return (int)GetValue(DurationProperty); } 84: set { SetValue(DurationProperty, value); } 85: }
86: public static readonly DependencyProperty DurationProperty =
87: DependencyProperty.Register("Duration", typeof(int), typeof(MessageLine), null); 88: }
89: }
b. *.xmal file:
1: <ControlTemplate x:Key="MessageLine" TargetType="{x:Type EKey_Game_Fng_Tools_GameServerEditor_View:MessageLine}"> 2: <Grid d:IsLocked="True">
3: <Grid.ColumnDefinitions>
4: <ColumnDefinition Width="auto"/>
5: <ColumnDefinition Width="*"/>
6: </Grid.ColumnDefinitions>
7: <TextBlock x:Name="txtPreTitle" TextWrapping="Wrap" d:LayoutOverrides="Height" MinWidth="100" Text="{TemplateBinding PreTitle}" Foreground="Gray" d:IsLocked="True"/> 8: <TextBlock x:Name="txtMessageBody" TextWrapping="Wrap" Text="{TemplateBinding MessageBody}" d:LayoutOverrides="Height" Grid.ColumnSpan="1" Grid.Column="1" MinWidth="100" d:IsLocked="True"/> 9: </Grid>
10: </ControlTemplate>
TAG: