From 083d6f96a4f9e5decb903a4577cbb77cf8d8da85 Mon Sep 17 00:00:00 2001 From: Bin Date: Thu, 20 Feb 2025 00:12:09 +0800 Subject: [PATCH] feat(models): enhance data binding and encapsulation (#42) Refactor properties in CustomInkCanvas, CanvasPage, Progress, and Settings classes to use public properties with [ObservableProperty] attributes. This improves data binding capabilities and encapsulation by replacing private fields with public properties. Additionally, simplify access to the StylusPlugIns collection in CustomInkCanvas. --- SketchNow/Controls/CustomInkCanvas.cs | 4 ++-- SketchNow/Models/CanvasPages.cs | 9 +++++---- SketchNow/Models/Progress.cs | 8 +++++--- SketchNow/Models/Settings.cs | 8 ++++---- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/SketchNow/Controls/CustomInkCanvas.cs b/SketchNow/Controls/CustomInkCanvas.cs index ef0ea18..c26998a 100644 --- a/SketchNow/Controls/CustomInkCanvas.cs +++ b/SketchNow/Controls/CustomInkCanvas.cs @@ -6,10 +6,10 @@ namespace SketchNow.Controls; public class CustomInkCanvas : InkCanvas { - CustomStylusPlugin _customStylusPlugin = new(); + private readonly CustomStylusPlugin _customStylusPlugin = new(); public CustomInkCanvas() { - this.StylusPlugIns.Add(_customStylusPlugin); + StylusPlugIns.Add(_customStylusPlugin); } } \ No newline at end of file diff --git a/SketchNow/Models/CanvasPages.cs b/SketchNow/Models/CanvasPages.cs index 4ff3784..324bbf4 100644 --- a/SketchNow/Models/CanvasPages.cs +++ b/SketchNow/Models/CanvasPages.cs @@ -8,16 +8,17 @@ namespace SketchNow.Models; public partial class CanvasPage : ObservableObject { - [ObservableProperty] private StrokeCollection _strokes = []; + [ObservableProperty] + public partial StrokeCollection Strokes { get; set; } = []; #region Undo/Redo /// - /// To Notify the Not ObservableProperty + /// To Notify the Not ObservableProperty /// [ObservableProperty] [NotifyCanExecuteChangedFor(nameof(UndoCommand), nameof(RedoCommand), nameof(ClearCommand))] - private int _counter; + public partial int Counter { get; set; } private readonly ObservableCollection _undoStack = []; private readonly ObservableCollection _redoStack = []; @@ -77,7 +78,7 @@ public CanvasPage() { // Listen for changes in the Strokes property Strokes.StrokesChanged += Strokes_Changed; - _undoStack.Add(CloneStrokeCollection(_strokes)); + _undoStack.Add(CloneStrokeCollection(Strokes)); } [RelayCommand(CanExecute = nameof(CanClear))] diff --git a/SketchNow/Models/Progress.cs b/SketchNow/Models/Progress.cs index 2c2c528..dc29f10 100644 --- a/SketchNow/Models/Progress.cs +++ b/SketchNow/Models/Progress.cs @@ -5,9 +5,11 @@ namespace SketchNow.Models; public partial class Progress : ObservableObject { [ObservableProperty] - private int _value; + public partial int Value { get; set; } + [ObservableProperty] - private bool _isIndeterminate; + public partial bool IsIndeterminate { get; set; } + [ObservableProperty] - private bool _isVisible; + public partial bool IsVisible { get; set; } } \ No newline at end of file diff --git a/SketchNow/Models/Settings.cs b/SketchNow/Models/Settings.cs index 2cf6312..9cf8c84 100644 --- a/SketchNow/Models/Settings.cs +++ b/SketchNow/Models/Settings.cs @@ -9,16 +9,16 @@ namespace SketchNow.Models; public partial class Settings : ObservableObject { [ObservableProperty] - private bool _isFitToCurve; + public partial bool IsFitToCurve { get; set; } [ObservableProperty] - private bool _isIgnorePressure; + public partial bool IsIgnorePressure { get; set; } [ObservableProperty] - private bool _isEraseByStroke; + public partial bool IsEraseByStroke { get; set; } [ObservableProperty] - private Brush _selectedBackground; + public partial Brush SelectedBackground { get; set; } public Settings() {