Browse Source

Implement RaiseAndSetIfChanged.

On devtools `ViewModelBase`.
Steven Kirk 8 years ago
parent
commit
0b9a8bff06

+ 3 - 3
src/Avalonia.Diagnostics/ViewModels/DevToolsViewModel.cs

@@ -38,7 +38,7 @@ namespace Avalonia.Diagnostics.ViewModels
         public ViewModelBase Content
         {
             get { return _content; }
-            private set { _content = value; RaisePropertyChanged(); }
+            private set { RaiseAndSetIfChanged(ref _content, value); }
         }
 
         public int SelectedTab
@@ -65,13 +65,13 @@ namespace Avalonia.Diagnostics.ViewModels
         public string FocusedControl
         {
             get { return _focusedControl; }
-            private set { _focusedControl = value; RaisePropertyChanged(); }
+            private set { RaiseAndSetIfChanged(ref _focusedControl, value); }
         }
 
         public string PointerOverElement
         {
             get { return _pointerOverElement; }
-            private set { _pointerOverElement = value; RaisePropertyChanged(); }
+            private set { RaiseAndSetIfChanged(ref _pointerOverElement, value); }
         }
 
         public void SelectControl(IControl control)

+ 3 - 3
src/Avalonia.Diagnostics/ViewModels/PropertyDetails.cs

@@ -38,19 +38,19 @@ namespace Avalonia.Diagnostics.ViewModels
         public string Priority
         {
             get { return _priority; }
-            private set { _priority = value; RaisePropertyChanged(); }
+            private set { RaiseAndSetIfChanged(ref _priority, value); }
         }
 
         public string Diagnostic
         {
             get { return _diagnostic; }
-            private set { _diagnostic = value; RaisePropertyChanged(); }
+            private set { RaiseAndSetIfChanged(ref _diagnostic, value); }
         }
 
         public object Value
         {
             get { return _value; }
-            private set { _value = value; RaisePropertyChanged(); }
+            private set { RaiseAndSetIfChanged(ref _value, value); }
         }
     }
 }

+ 2 - 2
src/Avalonia.Diagnostics/ViewModels/TreeNode.cs

@@ -56,7 +56,7 @@ namespace Avalonia.Diagnostics.ViewModels
         public string Classes
         {
             get { return _classes; }
-            private set { _classes = value; RaisePropertyChanged(); }
+            private set { RaiseAndSetIfChanged(ref _classes, value); }
         }
 
         public IVisual Visual
@@ -67,7 +67,7 @@ namespace Avalonia.Diagnostics.ViewModels
         public bool IsExpanded
         {
             get { return _isExpanded; }
-            set { _isExpanded = value; RaisePropertyChanged(); }
+            set { RaiseAndSetIfChanged(ref _isExpanded, value); }
         }
 
         public TreeNode Parent

+ 5 - 5
src/Avalonia.Diagnostics/ViewModels/TreePageViewModel.cs

@@ -1,7 +1,6 @@
 // Copyright (c) The Avalonia Project. All rights reserved.
 // Licensed under the MIT license. See licence.md file in the project root for full license information.
 
-using System.Reactive.Linq;
 using Avalonia.Controls;
 using Avalonia.VisualTree;
 
@@ -24,16 +23,17 @@ namespace Avalonia.Diagnostics.ViewModels
             get { return _selected; }
             set
             {
-                _selected = value;
-                RaisePropertyChanged();
-                Details = value != null ? new ControlDetailsViewModel(value.Visual) : null;
+                if (RaiseAndSetIfChanged(ref _selected, value))
+                {
+                    Details = value != null ? new ControlDetailsViewModel(value.Visual) : null;
+                }
             }
         }
 
         public ControlDetailsViewModel Details
         {
             get { return _details; }
-            private set { _details = value; RaisePropertyChanged(); }
+            private set { RaiseAndSetIfChanged(ref _details, value); }
         }
 
         public TreeNode FindNode(IControl control)

+ 13 - 0
src/Avalonia.Diagnostics/ViewModels/ViewModelBase.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Collections.Generic;
 using System.ComponentModel;
 using System.Runtime.CompilerServices;
 
@@ -8,6 +9,18 @@ namespace Avalonia.Diagnostics.ViewModels
     {
         public event PropertyChangedEventHandler PropertyChanged;
 
+        protected bool RaiseAndSetIfChanged<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
+        {
+            if (!EqualityComparer<T>.Default.Equals(field, value))
+            {
+                field = value;
+                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
+                return true;
+            }
+
+            return false;
+        }
+
         protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
         {
             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));