SampleCrossUi.WinForms 1.0.0.0
SampleCrossUi.WinForms 사용 방법을 보여 주는 예제 프로젝트입니다.
로딩중...
검색중...
일치하는것 없음
LightBulbPage.cs
이 파일의 문서화 페이지로 가기
1using System.ComponentModel;
2using Dreamine.UI.WinForms;
3using Dreamine.UI.WinForms.Controls;
4using SampleCrossUi.Shared.ViewModels;
5
7
16public sealed class LightBulbPage : UserControl
17{
26 private readonly LightBulbViewModel _vm;
35 private readonly Label _title;
44 private readonly Label _description;
53 private readonly DreamineLightBulb _bulb;
62 private readonly Label _status;
71 private readonly DreamineButton _toggleButton;
80 private readonly CheckBox _powerCheckBox;
89 private readonly Label _count;
98 private bool _refreshing;
99
108 public LightBulbPage() : this(new LightBulbViewModel(new LightBulbEvent(new LightBulbModel()))) { }
109
126 public LightBulbPage(LightBulbViewModel vm)
127 {
128 _vm = vm;
129
130 BackColor = DreamineTheme.AppBackground;
131 Dock = DockStyle.Fill;
132
133 var layout = new TableLayoutPanel
134 {
135 Dock = DockStyle.Fill,
136 ColumnCount = 1,
137 RowCount = 6,
138 Padding = new Padding(24),
139 };
140 layout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
141 layout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
142 layout.RowStyles.Add(new RowStyle(SizeType.Absolute, 180));
143 layout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
144 layout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
145 layout.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
146
147 _title = new Label
148 {
149 Text = "Light Bulb",
150 AutoSize = true,
151 ForeColor = Color.White,
152 Font = new Font("Segoe UI", 22f, FontStyle.Bold),
153 Anchor = AnchorStyles.None,
154 Margin = new Padding(0, 0, 0, 8)
155 };
156
157 _description = new Label
158 {
159 Text = "A button command and a checkbox binding drive the same shared state.",
160 AutoSize = true,
161 ForeColor = Color.FromArgb(184, 198, 221),
162 Font = new Font("Segoe UI", 10f),
163 Anchor = AnchorStyles.None,
164 Margin = new Padding(0, 0, 0, 18)
165 };
166
167 _bulb = new DreamineLightBulb
168 {
169 Width = 160,
170 Height = 170,
171 Diameter = 112,
172 Anchor = AnchorStyles.None,
173 BackColor = DreamineTheme.AppBackground,
174 Margin = new Padding(0, 0, 0, 6)
175 };
176
177 _status = new Label
178 {
179 AutoSize = true,
180 ForeColor = Color.FromArgb(141, 203, 255),
181 Font = new Font("Consolas", 10f, FontStyle.Bold),
182 Anchor = AnchorStyles.None,
183 Margin = new Padding(0, 0, 0, 18)
184 };
185
186 var row = new FlowLayoutPanel
187 {
188 AutoSize = true,
189 Anchor = AnchorStyles.None,
190 FlowDirection = FlowDirection.LeftToRight,
191 WrapContents = false,
192 };
193
194 _toggleButton = new DreamineButton
195 {
196 Content = "Toggle",
197 Width = 110,
198 Height = 40,
199 Margin = new Padding(0, 0, 14, 0),
200 };
201
202 _powerCheckBox = new CheckBox
203 {
204 Text = "Power",
205 AutoSize = true,
206 ForeColor = Color.White,
207 BackColor = DreamineTheme.AppBackground,
208 Margin = new Padding(0, 10, 14, 0),
209 };
210
211 _count = new Label
212 {
213 AutoSize = true,
214 ForeColor = Color.FromArgb(217, 227, 241),
215 Font = new Font("Consolas", 9f),
216 Margin = new Padding(0, 12, 0, 0),
217 };
218
219 row.Controls.Add(_toggleButton);
220 row.Controls.Add(_powerCheckBox);
221 row.Controls.Add(_count);
222
223 layout.Controls.Add(_title, 0, 0);
224 layout.Controls.Add(_description, 0, 1);
225 layout.Controls.Add(_bulb, 0, 2);
226 layout.Controls.Add(_status, 0, 3);
227 layout.Controls.Add(row, 0, 4);
228 Controls.Add(layout);
229
230 _toggleButton.Click += (_, _) => _vm.ToggleCommand.Execute(null);
231 _powerCheckBox.CheckedChanged += (_, _) =>
232 {
233 if (_refreshing || _vm.IsOn == _powerCheckBox.Checked) return;
234 _vm.ToggleCommand.Execute(null);
235 };
236 _vm.PropertyChanged += OnViewModelPropertyChanged;
237
238 RefreshState();
239 }
240
265 private void OnViewModelPropertyChanged(object? sender, PropertyChangedEventArgs e)
266 {
267 if (IsDisposed) return;
268 if (InvokeRequired) BeginInvoke(RefreshState);
269 else RefreshState();
270 }
271
280 private void RefreshState()
281 {
282 _refreshing = true;
283 _powerCheckBox.Checked = _vm.IsOn;
284 _bulb.IsOn = _vm.IsOn;
285 _status.Text = _vm.StatusText;
286 _count.Text = $"Toggled {_vm.ToggleCount}x";
287 _refreshing = false;
288 }
289
306 protected override void Dispose(bool disposing)
307 {
308 if (disposing)
309 _vm.PropertyChanged -= OnViewModelPropertyChanged;
310 base.Dispose(disposing);
311 }
312}
void OnViewModelPropertyChanged(object? sender, PropertyChangedEventArgs e)
override void Dispose(bool disposing)