Browse Source

Implement RaiseAndSetIfChanged.

On devtools `ViewModelBase`.
pull/1008/head
Steven Kirk 9 years ago
parent
commit
0b9a8bff06
  1. 6
      src/Avalonia.Diagnostics/ViewModels/DevToolsViewModel.cs
  2. 6
      src/Avalonia.Diagnostics/ViewModels/PropertyDetails.cs
  3. 4
      src/Avalonia.Diagnostics/ViewModels/TreeNode.cs
  4. 10
      src/Avalonia.Diagnostics/ViewModels/TreePageViewModel.cs
  5. 13
      src/Avalonia.Diagnostics/ViewModels/ViewModelBase.cs

6
src/Avalonia.Diagnostics/ViewModels/DevToolsViewModel.cs

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

6
src/Avalonia.Diagnostics/ViewModels/PropertyDetails.cs

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

4
src/Avalonia.Diagnostics/ViewModels/TreeNode.cs

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

10
src/Avalonia.Diagnostics/ViewModels/TreePageViewModel.cs

@ -1,7 +1,6 @@
// Copyright (c) The Avalonia Project. All rights reserved. // 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. // 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.Controls;
using Avalonia.VisualTree; using Avalonia.VisualTree;
@ -24,16 +23,17 @@ namespace Avalonia.Diagnostics.ViewModels
get { return _selected; } get { return _selected; }
set set
{ {
_selected = value; if (RaiseAndSetIfChanged(ref _selected, value))
RaisePropertyChanged(); {
Details = value != null ? new ControlDetailsViewModel(value.Visual) : null; Details = value != null ? new ControlDetailsViewModel(value.Visual) : null;
}
} }
} }
public ControlDetailsViewModel Details public ControlDetailsViewModel Details
{ {
get { return _details; } get { return _details; }
private set { _details = value; RaisePropertyChanged(); } private set { RaiseAndSetIfChanged(ref _details, value); }
} }
public TreeNode FindNode(IControl control) public TreeNode FindNode(IControl control)

13
src/Avalonia.Diagnostics/ViewModels/ViewModelBase.cs

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@ -8,6 +9,18 @@ namespace Avalonia.Diagnostics.ViewModels
{ {
public event PropertyChangedEventHandler PropertyChanged; 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) protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{ {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

Loading…
Cancel
Save