alielie
2025-03-04 2b1a6417285597bc58ef28aa3f01aeee2532cd19
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
using Panuon.WPF.UI;
using System.Windows;
using System.Windows.Controls;
 
namespace HMI.UI.Util
{
    public interface ToastHandler
    {
        void Success(Window window,
                string message,
                MessageBoxIcon icon,
                ToastPosition position, double offset, int durationMs,
                ToastWindow targetWindow);
 
        void Warning(Window window,
                string message,
                MessageBoxIcon icon,
                ToastPosition position, double offset, int durationMs,
                ToastWindow targetWindow);
 
        void Error(Window window,
                string message,
                MessageBoxIcon icon,
                ToastPosition position, double offset, int durationMs,
                ToastWindow targetWindow);
 
        void Info(Window window,
                string message,
                MessageBoxIcon icon,
                ToastPosition position, double offset, int durationMs,
                ToastWindow targetWindow);
    }
 
    public class ToastHelper
    {
        private static ToastHandler Instance;
 
        public static void Register(ToastHandler helper)
        {
            if (Instance == null) { Instance = helper; }
        }
 
        public static void Success(Window window,
            string message,
            MessageBoxIcon icon = MessageBoxIcon.Success,
            ToastPosition position = ToastPosition.Top, double offset = 0, int durationMs = 3000,
            ToastWindow targetWindow = ToastWindow.ActiveWindow)
        {
            Instance.Success(window, message, icon, position, offset, durationMs, targetWindow);
        }
 
        public static void Warning(Window window,
            string message,
            MessageBoxIcon icon = MessageBoxIcon.Warning,
            ToastPosition position = ToastPosition.Top, double offset = 0, int durationMs = 3000,
            ToastWindow targetWindow = ToastWindow.ActiveWindow)
        {
            Instance.Warning(window, message, icon, position, offset, durationMs, targetWindow);
        }
 
        public static void Error(Window window,
            string message,
            MessageBoxIcon icon = MessageBoxIcon.Error,
            ToastPosition position = ToastPosition.Top, double offset = 0, int durationMs = 3000,
            ToastWindow targetWindow = ToastWindow.ActiveWindow)
        {
            Instance.Error(window, message, icon, position, offset, durationMs, targetWindow);
        }
 
        public static void Info(Window window,
            string message,
            MessageBoxIcon icon = MessageBoxIcon.Info,
            ToastPosition position = ToastPosition.Top, double offset = 0, int durationMs = 3000,
            ToastWindow targetWindow = ToastWindow.ActiveWindow)
        {
            Instance.Info(window, message, icon, position, offset, durationMs, targetWindow);
        }
    }
 
    public static class ToastUtil
    {
 
        public static void Success(this Window window,
            string message,
            MessageBoxIcon icon = MessageBoxIcon.Success,
            ToastPosition position = ToastPosition.Top, double offset = 0, int durationMs = 3000,
            ToastWindow targetWindow = ToastWindow.ActiveWindow)
        {
            ToastHelper.Success(window, message, icon, position, offset, durationMs, targetWindow);
        }
 
        public static void Warning(this Window window,
            string message,
            MessageBoxIcon icon = MessageBoxIcon.Warning,
            ToastPosition position = ToastPosition.Top, double offset = 0, int durationMs = 3000,
            ToastWindow targetWindow = ToastWindow.ActiveWindow)
        {
            ToastHelper.Warning(window, message, icon, position, offset, durationMs, targetWindow);
        }
 
        public static void Error(this Window window,
            string message,
            MessageBoxIcon icon = MessageBoxIcon.Error,
            ToastPosition position = ToastPosition.Top, double offset = 0, int durationMs = 3000,
            ToastWindow targetWindow = ToastWindow.ActiveWindow)
        {
            ToastHelper.Error(window, message, icon, position, offset, durationMs, targetWindow);
        }
 
        public static void Info(this Window window,
            string message,
            MessageBoxIcon icon = MessageBoxIcon.Info,
            ToastPosition position = ToastPosition.Top, double offset = 0, int durationMs = 3000,
            ToastWindow targetWindow = ToastWindow.ActiveWindow)
        {
            ToastHelper.Info(window, message, icon, position, offset, durationMs, targetWindow);
        }
 
 
        public static void Success(this UserControl control,
            string message,
            MessageBoxIcon icon = MessageBoxIcon.Success,
            ToastPosition position = ToastPosition.Top, double offset = 0, int durationMs = 3000,
            ToastWindow targetWindow = ToastWindow.ActiveWindow)
        {
            ToastHelper.Success(Window.GetWindow(control), message, icon, position, offset, durationMs, targetWindow);
        }
 
        public static void Warning(this UserControl control,
            string message,
            MessageBoxIcon icon = MessageBoxIcon.Warning,
            ToastPosition position = ToastPosition.Top, double offset = 0, int durationMs = 3000,
            ToastWindow targetWindow = ToastWindow.ActiveWindow)
        {
            ToastHelper.Warning(Window.GetWindow(control), message, icon, position, offset, durationMs, targetWindow);
        }
 
        public static void Error(this UserControl control,
            string message,
            MessageBoxIcon icon = MessageBoxIcon.Error,
            ToastPosition position = ToastPosition.Top, double offset = 0, int durationMs = 3000,
            ToastWindow targetWindow = ToastWindow.ActiveWindow)
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                ToastHelper.Error(Window.GetWindow(control), message, icon, position, offset, durationMs, targetWindow);
            });
        }
 
        public static void Info(this UserControl control,
           string message,
           MessageBoxIcon icon = MessageBoxIcon.Error,
           ToastPosition position = ToastPosition.Top, double offset = 0, int durationMs = 3000,
           ToastWindow targetWindow = ToastWindow.ActiveWindow)
        {
            ToastHelper.Info(Window.GetWindow(control), message, icon, position, offset, durationMs, targetWindow);
        }
    }
}