Sample01 1.0.0.0
Sample01 사용 방법을 보여 주는 예제 프로젝트입니다.
로딩중...
검색중...
일치하는것 없음
MainWindowViewModel.cs
이 파일의 문서화 페이지로 가기
1using Dreamine.Hybrid.Interfaces;
2using Dreamine.MVVM.Attributes;
3using Dreamine.MVVM.ViewModels;
6using System;
7using System.Collections.ObjectModel;
8using System.Threading;
9using System.Threading.Tasks;
10using System.Windows;
11
13{
22 public sealed partial class MainWindowViewModel : ViewModelBase, IDisposable
23 {
32 private readonly IHybridMessageBus _bus;
41 private readonly IHybridStateStore<CounterState> _store;
50 private readonly IDisposable? _dashboardSub;
59 private readonly IDisposable? _counterSub;
68 private bool _disposed;
69
78 [DreamineProperty] private string _title = "🌇 저녁시간이 다가옵니다!";
87 [DreamineProperty] private int _clickCount;
96 [DreamineProperty] private string _statusMessage = "대기 중...";
105 [DreamineProperty] private ObservableCollection<string> _logs = new();
106
140 IHybridMessageBus bus,
141 IHybridStateStore<CounterState> store)
142 {
143 _bus = bus ?? throw new ArgumentNullException(nameof(bus));
144 _store = store ?? throw new ArgumentNullException(nameof(store));
145
146 ApplyState(_store.State);
147
150 }
151
160 [DreamineCommand("OnClick")]
161 private partial void Click();
162
171 [DreamineCommand("OnReset")]
172 private partial void Reset();
173
208 CancellationToken cancellationToken)
209 {
210 if (_disposed || cancellationToken.IsCancellationRequested)
211 {
212 return Task.CompletedTask;
213 }
214
215 return RunOnUiAsync(() =>
216 {
217 Logs.Add($"[{DateTime.Now:HH:mm:ss}] Dashboard Action: {message.Action}");
218 });
219 }
220
254 CounterChangedMessage message,
255 CancellationToken cancellationToken)
256 {
257 if (_disposed || cancellationToken.IsCancellationRequested)
258 {
259 return Task.CompletedTask;
260 }
261
262 return RunOnUiAsync(() =>
263 {
264 ClickCount = message.Count;
265 StatusMessage = $"현재 클릭 수: {ClickCount}";
266 Logs.Add($"[{DateTime.Now:HH:mm:ss}] (Blazor) 카운트 변경: {ClickCount}");
267
268 _store.SetState(new CounterState(
269 Count: ClickCount,
270 LastSource: "Blazor → WPF → StateStore",
271 LastUpdated: DateTime.Now));
272 });
273 }
274
283 private void OnClick()
284 {
286
287 ClickCount++;
288 StatusMessage = $"현재 클릭 수: {ClickCount}";
289 Logs.Add($"[{DateTime.Now:HH:mm:ss}] (WPF) 버튼 클릭 {ClickCount}회");
290
291 _store.SetState(new CounterState(
292 Count: ClickCount,
293 LastSource: "WPF → StateStore",
294 LastUpdated: DateTime.Now));
295
296 _ = PublishCounterChangedAsync(ClickCount);
297 }
298
307 private void OnReset()
308 {
310
311 ClickCount = 0;
312 StatusMessage = "초기화 완료";
313 Logs.Clear();
314
315 _store.SetState(new CounterState(
316 Count: 0,
317 LastSource: "WPF → StateStore",
318 LastUpdated: DateTime.Now));
319
320 _ = PublishCounterChangedAsync(ClickCount);
321 }
322
347 private Task PublishCounterChangedAsync(int count)
348 {
349 return _bus.PublishAsync(new CounterChangedMessage(count));
350 }
351
368 private void ApplyState(CounterState state)
369 {
370 ClickCount = state.Count;
371 StatusMessage = $"현재 클릭 수: {ClickCount}";
372 }
373
406 private static Task RunOnUiAsync(Action action)
407 {
408 if (action is null)
409 {
410 throw new ArgumentNullException(nameof(action));
411 }
412
413 Application? application = Application.Current;
414
415 if (application?.Dispatcher is null)
416 {
417 action();
418 return Task.CompletedTask;
419 }
420
421 if (application.Dispatcher.CheckAccess())
422 {
423 action();
424 return Task.CompletedTask;
425 }
426
427 return application.Dispatcher.InvokeAsync(action).Task;
428 }
429
438 public void Dispose()
439 {
440 if (_disposed)
441 {
442 return;
443 }
444
445 _counterSub?.Dispose();
446 _dashboardSub?.Dispose();
447
448 _disposed = true;
449 GC.SuppressFinalize(this);
450 }
451
468 private void ThrowIfDisposed()
469 {
470 if (_disposed)
471 {
472 throw new ObjectDisposedException(nameof(MainWindowViewModel));
473 }
474 }
475 }
476}
record CounterState(int Count, string LastSource, DateTime? LastUpdated)
Task OnCounterChangedAsync(CounterChangedMessage message, CancellationToken cancellationToken)
MainWindowViewModel(IHybridMessageBus bus, IHybridStateStore< CounterState > store)
readonly IHybridStateStore< CounterState > _store
Task OnDashboardActionRequestedAsync(DashboardActionRequestedMessage message, CancellationToken cancellationToken)