lixiaojun
2024-07-25 8b2f98400842e022aefd5f2f935ca62239a75228
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
namespace Yw.WinFrmUI
{
    /// <summary>
    /// 步骤管理器
    /// </summary>
    public class WizardManager<T> : IWizardManager<T> where T : class, new()
    {
        /// <summary>
        /// 
        /// </summary>
        public WizardManager(T t)
        {
            _vm = t;
        }
 
 
        #region 事件
 
        /// <summary>
        /// 选择页面改变事件
        /// </summary>
        public event Action<IWizardPage<T>, int> SelectedPageChangedEvent;
 
        /// <summary>
        /// 选择页面状态改变事件
        /// </summary>
        public event Action<IWizardPage<T>, int> SelectedPageStateChangedEvent;
 
        #endregion
 
        #region 初始化
 
        /// <summary>
        /// 初始化管理器
        /// </summary>
        /// <param name="pages">步骤页面数组</param>
        public virtual void InitialManager(IWizardPage<T>[] pages)
        {
            _pages = pages;
            if (_pages != null && _pages.Length > 0)
            {
                _isInitialize = true;
                foreach (var page in _pages)
                {
                    page.PageStateChangedEvent += () =>
                    {
                        if (page == this.SelectedPage)
                        {
                            TriggerSelectedPageStateChangedEvent();
                        }
                    };
                }
                _selectedIndex = 0;
                this.SelectedPage.InitialPage(_vm);
                TriggerSelectedPageChangedEvent();
            }
            else
            {
                _isInitialize = false;
            }
        }
 
        /// <summary>
        /// 步骤页面数组
        /// </summary>
        protected IWizardPage<T>[] _pages;
 
        /// <summary>
        /// 是否初始化
        /// </summary>
        protected bool _isInitialize = false;
 
        /// <summary>
        /// vm对象
        /// </summary>
        protected readonly T _vm = null;
 
        #endregion
 
        #region 选择
 
        /// <summary>
        /// 选择页面
        /// </summary>
        public virtual IWizardPage<T> SelectedPage
        {
            get
            {
                if (_isInitialize)
                {
                    return _pages[this.SelectedIndex];
                }
                return default;
            }
        }
 
        /// <summary>
        /// 选择索引
        /// </summary>
        public virtual int SelectedIndex
        {
            get { return _selectedIndex; }
        }
 
        /// <summary>
        /// 选择索引
        /// </summary>
        protected int _selectedIndex;
 
        #endregion
 
        #region 操作
 
        /// <summary>
        /// 上一步
        /// </summary>
        public virtual bool Prev()
        {
            if (!_isInitialize)
            {
                return false;
            }
            if (!this.SelectedPage.AllowPrev)
            {
                return false;
            }
            if (this.SelectedPage.CanPrev())
            {
                --_selectedIndex;
                this.SelectedPage.InitialPage(_vm);
                TriggerSelectedPageChangedEvent();
                return true;
            }
            return false;
        }
 
        /// <summary>
        /// 下一步
        /// </summary>
        public virtual bool Next()
        {
            if (!_isInitialize)
            {
                return false;
            }
            if (!this.SelectedPage.AllowNext)
            {
                return false;
            }
            if (this.SelectedPage.CanNext())
            {
                ++_selectedIndex;
                this.SelectedPage.InitialPage(_vm);
                TriggerSelectedPageChangedEvent();
                return true;
            }
            return false;
        }
 
        /// <summary>
        /// 取消
        /// </summary>
        public virtual bool Cancel()
        {
            if (!_isInitialize)
            {
                return false;
            }
            if (!this.SelectedPage.AllowCancel)
            {
                return false;
            }
            if (this.SelectedPage.CanCancel())
            {
                return true;
            }
            return false;
        }
 
        /// <summary>
        /// 完成
        /// </summary>
        public virtual bool Complete()
        {
            if (!_isInitialize)
            {
                return false;
            }
            if (!this.SelectedPage.AllowComplete)
            {
                return false;
            }
            if (this.SelectedPage.CanComplete())
            {
                return true;
            }
            return false;
        }
 
        #endregion
 
        #region 方法
 
        /// <summary>
        /// 触发选择页面改变事件
        /// </summary>
        protected void TriggerSelectedPageChangedEvent()
        {
            this.SelectedPageChangedEvent?.Invoke(this.SelectedPage, this.SelectedIndex);
        }
 
        /// <summary>
        /// 触发选择页面状态改变事件
        /// </summary>
        protected void TriggerSelectedPageStateChangedEvent()
        {
            this.SelectedPageStateChangedEvent?.Invoke(this.SelectedPage, this.SelectedIndex);
        }
 
        #endregion
 
 
    }
}