WPF自定义控件三:消息提示框

1|0需求:实现全局消息提示框

1|1一:创建全局Message

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
public class Message
    {
        private static readonly Style infoStyle = (Style)Application.Current.Resources["InfoMessage"];
        private static readonly Style warningStyle = (Style)Application.Current.Resources["WarningMessage"];
        private static readonly Style successStyle = (Style)Application.Current.Resources["SuccessMessage"];
        private static readonly Style errorStyle = (Style)Application.Current.Resources["ErrorMessage"];
 
        #region 全局
        public static void Show(MessageType type, UIElement element, int millisecondTimeOut = 3000, bool isClearable = true)
        {
            if (millisecondTimeOut <= 0)
            {
                isClearable = true;
            }
 
            MessageWindow messageWindow = MessageWindow.GetInstance();
            messageWindow.Dispatcher.VerifyAccess();
 
            MessageCard messageCard;
            switch (type)
            {
                default:
                case MessageType.None:
                    messageCard = new MessageCard
                    {
                        Content = element,
                        IsClearable = isClearable
                    };
                    break;
                case MessageType.Info:
                    messageCard = new MessageCard
                    {
                        Content = element,
                        IsClearable = isClearable,
                        Style = infoStyle
                    };
                    break;
                case MessageType.Warning:
                    messageCard = new MessageCard
                    {
                        Content = element,
                        IsClearable = isClearable,
                        Style = warningStyle
                    };
                    break;
                case MessageType.Success:
                    messageCard = new MessageCard
                    {
                        Content = element,
                        IsClearable = isClearable,
                        Style = successStyle
                    };
                    break;
                case MessageType.Error:
                    messageCard = new MessageCard
                    {
                        Content = element,
                        IsClearable = isClearable,
                        Style = errorStyle
                    };
                    break;
            }
 
            messageWindow.AddMessageCard(messageCard, millisecondTimeOut);
            messageWindow.Show();
        }
 
        public static void Show(UIElement element, int millisecondTimeOut = 3000, bool isClearable = true)
        {
            Show(MessageType.None, element, millisecondTimeOut, isClearable);
        }
 
        public static void ShowInfo(UIElement element, int millisecondTimeOut = 3000, bool isClearable = true)
        {
            Show(MessageType.Info, element, millisecondTimeOut, isClearable);
        }
 
        public static void ShowSuccess(UIElement element, int millisecondTimeOut = 3000, bool isClearable = true)
        {
            Show(MessageType.Success, element, millisecondTimeOut, isClearable);
        }
 
        public static void ShowWarning(UIElement element, int millisecondTimeOut = 3000, bool isClearable = true)
        {
            Show(MessageType.Warning, element, millisecondTimeOut, isClearable);
        }
 
        public static void ShowError(UIElement element, int millisecondTimeOut = 3000, bool isClearable = true)
        {
            Show(MessageType.Error, element, millisecondTimeOut, isClearable);
        }
 
        public static void Show(MessageType type, string message, int millisecondTimeOut = 3000, bool isClearable = true)
        {
            Show(type, new TextBlock { Text = message }, millisecondTimeOut, isClearable);
        }
 
        public static void Show(string message, int millisecondTimeOut = 3000, bool isClearable = true)
        {
            Show(MessageType.None, new TextBlock { Text = message }, millisecondTimeOut, isClearable);
        }
 
        public static void ShowInfo(string message, int millisecondTimeOut = 3000, bool isClearable = true)
        {
            Show(MessageType.Info, new TextBlock { Text = message }, millisecondTimeOut, isClearable);
        }
 
        public static void ShowSuccess(string message, int millisecondTimeOut = 3000, bool isClearable = true)
        {
            Show(MessageType.Success, new TextBlock { Text = message }, millisecondTimeOut, isClearable);
        }
 
        public static void ShowWarning(string message, int millisecondTimeOut = 3000, bool isClearable = true)
        {
            Show(MessageType.Warning, new TextBlock { Text = message }, millisecondTimeOut, isClearable);
        }
 
        public static void ShowError(string message, int millisecondTimeOut = 3000, bool isClearable = true)
        {
            Show(MessageType.Error, new TextBlock { Text = message }, millisecondTimeOut, isClearable);
        }
        #endregion
 
 
        #region 指定容器
        public static void Show(string containerIdentifier, MessageType type, UIElement element, int millisecondTimeOut = 3000, bool isClearable = true)
        {
            if (!MessageContainer.Containers.ContainsKey(containerIdentifier))
            {
                return;
            }
 
            if (millisecondTimeOut <= 0)
            {
                isClearable = true;
            }
 
            Panel messagePanel = MessageContainer.Containers[containerIdentifier];
            messagePanel.Dispatcher.VerifyAccess();
 
            MessageCard messageCard;
            switch (type)
            {
                default:
                case MessageType.None:
                    messageCard = new MessageCard
                    {
                        Content = element,
                        IsClearable = isClearable
                    };
                    break;
                case MessageType.Info:
                    messageCard = new MessageCard
                    {
                        Content = element,
                        IsClearable = isClearable,
                        Style = infoStyle
                    };
                    break;
                case MessageType.Warning:
                    messageCard = new MessageCard
                    {
                        Content = element,
                        IsClearable = isClearable,
                        Style = warningStyle
                    };
                    break;
                case MessageType.Success:
                    messageCard = new MessageCard
                    {
                        Content = element,
                        IsClearable = isClearable,
                        Style = successStyle
                    };
                    break;
                case MessageType.Error:
                    messageCard = new MessageCard
                    {
                        Content = element,
                        IsClearable = isClearable,
                        Style = errorStyle
                    };
                    break;
            }
 
            messagePanel.Children.Add(messageCard);
 
            // 进入动画
            Storyboard enterStoryboard = new Storyboard();
 
            DoubleAnimation opacityAnimation = new DoubleAnimation
            {
                From = 0,
                To = 1,
                Duration = new Duration(TimeSpan.FromMilliseconds(300)),
                EasingFunction = new CubicEase { EasingMode = EasingMode.EaseIn }
            };
            Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath(UIElement.OpacityProperty));
 
            DoubleAnimation transformAnimation = new DoubleAnimation
            {
                From = -30,
                To = Application.Current.MainWindow.Height / 2-100,
                Duration = new Duration(TimeSpan.FromMilliseconds(300)),
                EasingFunction = new CubicEase { EasingMode = EasingMode.EaseIn }
            };
            Storyboard.SetTargetProperty(transformAnimation, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)"));
 
            enterStoryboard.Children.Add(opacityAnimation);
            enterStoryboard.Children.Add(transformAnimation);
            if (millisecondTimeOut > 0)
            {
                // 进入动画完成
                enterStoryboard.Completed += async (sender, e) =>
                {
                    await Task.Run(() =>
                    {
                        Thread.Sleep(millisecondTimeOut);
                    });
 
                    messagePanel.Children.Remove(messageCard);
                };
 
            }
 
            messageCard.BeginStoryboard(enterStoryboard);
            // 退出动画
            //Storyboard exitStoryboard = new Storyboard();
 
            //DoubleAnimation exitOpacityAnimation = new DoubleAnimation
            //{
            //    From = 1,
            //    To = Application.Current.MainWindow.Height / 2,
            //    Duration = new Duration(TimeSpan.FromMilliseconds(300)),
            //    EasingFunction = new CubicEase { EasingMode = EasingMode.EaseIn }
            //};
            //Storyboard.SetTargetProperty(exitOpacityAnimation, new PropertyPath(UIElement.OpacityProperty));
 
            //DoubleAnimation exitTransformAnimation = new DoubleAnimation
            //{
            //    From = 0,
            //    To = -30,
            //    Duration = new Duration(TimeSpan.FromMilliseconds(300)),
            //    EasingFunction = new CubicEase { EasingMode = EasingMode.EaseIn }
            //};
            //Storyboard.SetTargetProperty(exitTransformAnimation, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)"));
 
            //exitStoryboard.Children.Add(exitOpacityAnimation);
            //exitStoryboard.Children.Add(exitTransformAnimation);
 
            //if (millisecondTimeOut > 0)
            //{
            //    // 进入动画完成
            //    enterStoryboard.Completed += async (sender, e) =>
            //    {
            //        await Task.Run(() =>
            //        {
            //            Thread.Sleep(millisecondTimeOut);
            //        });
 
            //        messageCard.BeginStoryboard(exitStoryboard);
            //    };
 
            //}
 
            // 退出动画完成
            //exitStoryboard.Completed += (sender, e) =>
            //{
            //    messagePanel.Children.Remove(messageCard);
            //};
 
            //messageCard.BeginStoryboard(enterStoryboard);
        }
 
        public static void Show(string containerIdentifier, UIElement element, int millisecondTimeOut = 3000, bool isClearable = true)
        {
            Show(containerIdentifier, MessageType.None, element, millisecondTimeOut, isClearable);
        }
 
        public static void ShowInfo(string containerIdentifier, UIElement element, int millisecondTimeOut = 3000, bool isClearable = true)
        {
            Show(containerIdentifier, MessageType.Info, element, millisecondTimeOut, isClearable);
        }
 
        public static void ShowSuccess(string containerIdentifier, UIElement element, int millisecondTimeOut = 3000, bool isClearable = true)
        {
            Show(containerIdentifier, MessageType.Success, element, millisecondTimeOut, isClearable);
        }
 
        public static void ShowWarning(string containerIdentifier, UIElement element, int millisecondTimeOut = 3000, bool isClearable = true)
        {
            Show(containerIdentifier, MessageType.Warning, element, millisecondTimeOut, isClearable);
        }
 
        public static void ShowError(string containerIdentifier, UIElement element, int millisecondTimeOut = 3000, bool isClearable = true)
        {
            Show(containerIdentifier, MessageType.Error, element, millisecondTimeOut, isClearable);
        }
 
        public static void Show(string containerIdentifier, MessageType type, string message, int millisecondTimeOut = 3000, bool isClearable = true)
        {
            Show(containerIdentifier, type, new TextBlock { Text = message }, millisecondTimeOut, isClearable);
        }
 
        public static void Show(string containerIdentifier, string message, int millisecondTimeOut = 3000, bool isClearable = true)
        {
            Show(containerIdentifier, MessageType.None, new TextBlock { Text = message }, millisecondTimeOut, isClearable);
        }
 
        public static void ShowInfo(string containerIdentifier, string message, int millisecondTimeOut = 3000, bool isClearable = true)
        {
            Show(containerIdentifier, MessageType.Info, new TextBlock { Text = message, Foreground = new SolidColorBrush(Colors.White) }, millisecondTimeOut, isClearable);
        }
 
        public static void ShowSuccess(string containerIdentifier, string message, int millisecondTimeOut = 3000, bool isClearable = true)
        {
            Show(containerIdentifier, MessageType.Success, new TextBlock { Text = message, Foreground = new SolidColorBrush(Colors.White) }, millisecondTimeOut, isClearable);
        }
 
        public static void ShowWarning(string containerIdentifier, string message, int millisecondTimeOut = 3000, bool isClearable = true)
        {
            Show(containerIdentifier, MessageType.Warning, new TextBlock { Text = message, Foreground = new SolidColorBrush(Colors.White) }, millisecondTimeOut, isClearable);
        }
 
        public static void ShowError(string containerIdentifier, string message, int millisecondTimeOut = 3000, bool isClearable = true)
        {
            Show(containerIdentifier, MessageType.Error, new TextBlock { Text = message, Foreground = new SolidColorBrush(Colors.White) }, millisecondTimeOut, isClearable);
        }
        #endregion
    }
 
    public enum MessageType
    {
        None = 0,
        Info,
        Success,
        Warning,
        Error
    }

1|2二:创建消息提示控件

1|01:创建名为MessageCard资源字典与MessageCard类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  <LinearGradientBrush x:Key="GridBackGrounds1" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="#FF061118" Offset="0.191"/>
        <GradientStop Color="#FF173A52" Offset="1"/>
    </LinearGradientBrush>
 
<Style x:Key="MessageCard" TargetType="{x:Type local:MessageCard}">
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="BorderBrush" Value="{StaticResource BorderGray}"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="CornerRadius" Value="2.5"/>
        <Setter Property="Background" Value="{DynamicResource GridBackGrounds1}"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="ThemeColorBrush" Value="{DynamicResource DefaultForeground}"/>
        <Setter Property="Margin" Value="2.5"/>
        <Setter Property="Padding" Value="5"/>
        <Setter Property="MinHeight" Value="60"/>
        <Setter Property="MinWidth" Value="200"/>
        <Setter Property="IsShwoIcon" Value="False"/>
        <Setter Property="FontSize" Value="18"/>
        <Setter Property="RenderTransform">
            <Setter.Value>
                <TranslateTransform />
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:MessageCard}">
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding ThemeColorBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}" Margin="{TemplateBinding Margin}"
                            CornerRadius="{TemplateBinding CornerRadius}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding ThemeColorBrush}"
                                    BorderThickness="{TemplateBinding BorderThickness}" Effect="{StaticResource AllDirectionEffect}"
                                    Padding="{TemplateBinding Padding}" CornerRadius="{TemplateBinding CornerRadius}"
                                    Grid.ColumnSpan="3"/>
 
                            
                            <ContentPresenter x:Name="contentPresenter" Focusable="False" Grid.Column="1"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          RecognizesAccessKey="True"
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="8 0"/>
                            <Button x:Name="clearButton"  Foreground="{TemplateBinding ThemeColorBrush}" 
                                    Grid.Column="2" Style="{x:Null}" Height="20" BorderThickness="0" BorderBrush="Transparent" Background="Transparent" <br>local:ButtonHelper.ButtonStyle="Link" Visibility="{TemplateBinding IsClearable,Converter={StaticResource boolToVisibility}}"<br>Content="X"<br>>
                                 
                            </Button>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
 
    <Style x:Key="WarningMessage" TargetType="{x:Type local:MessageCard}" BasedOn="{StaticResource MessageCard}">
        <Setter Property="ThemeColorBrush" Value="{StaticResource WarningBrush}"/>
        <Setter Property="IconType" Value="ErrorWarningFill"/>//IOC提示类型
        <Setter Property="IsShwoIcon" Value="True"/>
         
    </Style>
 
    <Style x:Key="SuccessMessage" TargetType="{x:Type local:MessageCard}" BasedOn="{StaticResource MessageCard}">
        <Setter Property="ThemeColorBrush" Value="{StaticResource SuccessBrush}"/>
        <Setter Property="IconType" Value="CheckboxCircleFill"/>
        <Setter Property="IsShwoIcon" Value="True"/>
         
    </Style>
 
    <Style x:Key="ErrorMessage" TargetType="{x:Type local:MessageCard}" BasedOn="{StaticResource MessageCard}">
        <Setter Property="ThemeColorBrush" Value="{StaticResource ErrorBrush}"/>
        <Setter Property="IconType" Value="CloseCircleFill"/>
        <Setter Property="IsShwoIcon" Value="True"/>
         
    </Style>
 
    <Style x:Key="InfoMessage" TargetType="{x:Type local:MessageCard}" BasedOn="{StaticResource MessageCard}">
        <Setter Property="ThemeColorBrush" Value="{StaticResource InfoBrush}"/>
        <Setter Property="IconType" Value="InformationFill"/>
        <Setter Property="IsShwoIcon" Value="True"/>
      
    </Style>
using System; using System.Collections.Generic; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Animation; using Zt.UI.Silver.Utils; namespace Zt.UI.Silver { public class MessageCard : ContentControl { public static readonly RoutedEvent CloseEvent; static MessageCard() { CloseEvent = EventManager.RegisterRoutedEvent("Close", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MessageCard)); DefaultStyleKeyProperty.OverrideMetadata(typeof(MessageCard), new FrameworkPropertyMetadata(typeof(MessageCard))); } #region 事件 // 关闭消息事件 public event RoutedEventHandler Close { add { base.AddHandler(MessageCard.CloseEvent, value); } remove { base.RemoveHandler(MessageCard.CloseEvent, value); } } #endregion #region 依赖属性 public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(MessageCard), new PropertyMetadata(default(CornerRadius))); public CornerRadius CornerRadius { get { return (CornerRadius)GetValue(CornerRadiusProperty); } set { SetValue(CornerRadiusProperty, value); } } public static readonly DependencyProperty ThemeColorBrushProperty = DependencyProperty.Register("ThemeColorBrush", typeof(SolidColorBrush), typeof(MessageCard), new PropertyMetadata(default(SolidColorBrush))); public SolidColorBrush ThemeColorBrush { get { return (SolidColorBrush)GetValue(ThemeColorBrushProperty); } set { SetValue(ThemeColorBrushProperty, value); } } public static readonly DependencyProperty IconTypeProperty = DependencyProperty.Register("IconType", typeof(IconType), typeof(MessageCard), new PropertyMetadata(default(IconType))); public IconType IconType { get { return (IconType)GetValue(IconTypeProperty); } set { SetValue(IconTypeProperty, value); } } public static readonly DependencyProperty IsShwoIconProperty = DependencyProperty.Register("IsShwoIcon", typeof(bool), typeof(MessageCard), new PropertyMetadata(default(bool))); public bool IsShwoIcon { get { return (bool)GetValue(IsShwoIconProperty); } set { SetValue(IsShwoIconProperty, value); } } public static readonly DependencyProperty IsClearableProperty = DependencyProperty.Register("IsClearable", typeof(bool), typeof(MessageCard), new PropertyMetadata(default(bool), OnIsClearbleChanged)); public bool IsClearable { get { return (bool)GetValue(IsClearableProperty); } set { SetValue(IsClearableProperty, value); } } private static void OnIsClearbleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is MessageCard messageCard) { RoutedEventHandler handle = (sender, args) => { if (VisualTreeHelper.GetParent(messageCard) is Panel panel) { // 退出动画 Storyboard exitStoryboard = new Storyboard(); DoubleAnimation exitOpacityAnimation = new DoubleAnimation { From = 1, To = 0, Duration = new Duration(TimeSpan.FromMilliseconds(300)), EasingFunction = new CubicEase { EasingMode = EasingMode.EaseIn } }; Storyboard.SetTargetProperty(exitOpacityAnimation, new PropertyPath(FrameworkElement.OpacityProperty)); DoubleAnimation exitTransformAnimation = new DoubleAnimation { From = 0, To = -30, Duration = new Duration(TimeSpan.FromMilliseconds(300)), EasingFunction = new CubicEase { EasingMode = EasingMode.EaseIn } }; Storyboard.SetTargetProperty(exitTransformAnimation, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)")); exitStoryboard.Children.Add(exitOpacityAnimation); exitStoryboard.Children.Add(exitTransformAnimation); // 动画完成 exitStoryboard.Completed += (a, b) => { panel.Children.Remove(messageCard); RoutedEventArgs eventArgs = new RoutedEventArgs(MessageCard.CloseEvent, messageCard); messageCard.RaiseEvent(eventArgs); }; messageCard.BeginStoryboard(exitStoryboard); // 执行动画 } }; messageCard.Foreground =new SolidColorBrush( Colors.Black); messageCard.Loaded += (sender, arg) => { if (messageCard.Template.FindName("clearButton", messageCard) is Button clearButton) { if (messageCard.IsClearable) { clearButton.Click += handle; } else { clearButton.Click -= handle; } } }; messageCard.Unloaded += (sender, arg) => { if (messageCard.Template.FindName("clearButton", messageCard) is Button clearButton) { if (messageCard.IsClearable) { clearButton.Click -= handle; } } }; } } #endregion } }

1|02:创建名为MessageWindow的窗体

<Window x:Class="Zt.UI.Silver.MessageWindow" 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" xmlns:local="clr-namespace:Zt.UI.Silver" mc:Ignorable="d" Background="Transparent" WindowStyle="None" AllowsTransparency="True" WindowState="Maximized" ShowInTaskbar="False" WindowStartupLocation="CenterOwner" VerticalAlignment="Top" Topmost="True"> <StackPanel x:Name="messageStackPanel" Margin="10"> </StackPanel> </Window>

1|0后台代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
public partial class MessageWindow : Window
   {
       private static MessageWindow messageWindow = null;
 
       private MessageWindow()
       {
           InitializeComponent();
       }
 
       public static MessageWindow GetInstance()
       {
           if (messageWindow == null)
           {
               messageWindow = new MessageWindow();
           }
           else if (!messageWindow.IsLoaded)
           {
               messageWindow = new MessageWindow();
           }
           return messageWindow;
       }
 
       public void AddMessageCard(MessageCard messageCard, int millisecondTimeOut)
       {
           messageCard.Close += MessageCard_Close;
 
           messageStackPanel.Children.Add(messageCard);
 
           // 进入动画
           Storyboard enterStoryboard = new Storyboard();
 
           DoubleAnimation opacityAnimation = new DoubleAnimation
           {
               From = 0,
               To = 1,
               Duration = new Duration(TimeSpan.FromMilliseconds(300)),
               EasingFunction = new CubicEase { EasingMode = EasingMode.EaseIn }
           };
           Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath(OpacityProperty));
 
           DoubleAnimation transformAnimation = new DoubleAnimation
           {
               From = -30,
               To = 0,
               Duration = new Duration(TimeSpan.FromMilliseconds(300)),
               EasingFunction = new CubicEase { EasingMode = EasingMode.EaseIn }
           };
           Storyboard.SetTargetProperty(transformAnimation, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)"));
 
           enterStoryboard.Children.Add(opacityAnimation);
           enterStoryboard.Children.Add(transformAnimation);
 
           // 退出动画
           Storyboard exitStoryboard = new Storyboard();
 
           DoubleAnimation exitOpacityAnimation = new DoubleAnimation
           {
               From = 1,
               To = 0,
               Duration = new Duration(TimeSpan.FromMilliseconds(300)),
               EasingFunction = new CubicEase { EasingMode = EasingMode.EaseIn }
           };
           Storyboard.SetTargetProperty(exitOpacityAnimation, new PropertyPath(OpacityProperty));
 
           DoubleAnimation exitTransformAnimation = new DoubleAnimation
           {
               From = 0,
               To = -30,
               Duration = new Duration(TimeSpan.FromMilliseconds(300)),
               EasingFunction = new CubicEase { EasingMode = EasingMode.EaseIn }
           };
           Storyboard.SetTargetProperty(exitTransformAnimation, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)"));
 
           exitStoryboard.Children.Add(exitOpacityAnimation);
           exitStoryboard.Children.Add(exitTransformAnimation);
 
           // 进入动画完成
           if (millisecondTimeOut > 0)
           {
               enterStoryboard.Completed += async (sender, e) =>
               {
                   await Task.Run(() =>
                   {
                       Thread.Sleep(millisecondTimeOut);
                   });
 
                   Dispatcher.Invoke(() =>
                   {
                       messageCard.BeginStoryboard(exitStoryboard);
                   });
               };
           }
 
           // 退出动画完成
           exitStoryboard.Completed += (sender, e) =>
           {
               Dispatcher.Invoke(() =>
               {
                   messageStackPanel.Children.Remove(messageCard);
                   if (messageStackPanel.Children.Count == 0)
                   {
                       this.Close();
                   }
               });
           };
 
           messageCard.BeginStoryboard(enterStoryboard);
       }
 
       /// <summary>
       /// 消息卡片关闭按钮事件
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void MessageCard_Close(object sender, RoutedEventArgs e)
       {
           if (messageStackPanel.Children.Count == 0)
           {
               this.Close();
           }
       }
   }

1|3三:添加转换器BoolToVisibilityConverter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class BoolToVisibilityConverter : IValueConverter
   {
       public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
       {
           if ((bool)value)
           {
               return Visibility.Visible;
           }
           else
           {
               return Visibility.Collapsed;
           }
       }
 
       public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
       {
           return DependencyProperty.UnsetValue;
       }
   }

  

1|4四:用法

1|0APP引用

1
2
<ResourceDictionary Source="pack://application:,,,/Zt.UI.Silver;component/Themes/MessageCard.xaml" />
 <Style TargetType="{x:Type local:MessageCard}" BasedOn="{StaticResource MessageCard}"/><br><br>

1|0Main 下使用   

<zt:MessageContainer Identifier="MessageContainer" Grid.RowSpan="10"/>

1|0CS:后台代码

局部提示 Message.ShowError("MessageContainer","123",1000);
全局提示 Message.ShowError("123",1000);

1|5五:演示

 


__EOF__

本文作者可乐加冰
本文链接https://www.cnblogs.com/zt199510/p/14510575.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   可乐_加冰  阅读(2457)  评论(3编辑  收藏  举报
编辑推荐:
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· .NET 进程 stackoverflow异常后,还可以接收 TCP 连接请求吗?
阅读排行:
· 本地部署 DeepSeek:小白也能轻松搞定!
· 基于DeepSeek R1 满血版大模型的个人知识库,回答都源自对你专属文件的深度学习。
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 在缓慢中沉淀,在挑战中重生!2024个人总结!
· 大人,时代变了! 赶快把自有业务的本地AI“模型”训练起来!
点击右上角即可分享
微信分享提示