using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Tango.Core.Commands;
namespace Tango.Core
{
///
/// Represents an extension to the standard core object with support for property and relay commands changed event.
///
///
[Serializable]
public class ExtendedObject : INotifyPropertyChanged
{
///
/// Occurs when a property has changed.
///
[field: NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
///
/// Raises the property changed event.
///
/// Name of the property.
protected virtual void RaisePropertyChanged(String propName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
///
/// Raises the property changed event.
///
/// Name of the property.
protected virtual void RaisePropertyChangedAuto([CallerMemberName] string caller = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(caller));
}
///
/// Raises all relay commands CanExecute methods in the current instance.
///
protected virtual void InvalidateRelayCommands()
{
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
foreach (var prop in this.GetType().GetProperties().Where(x => x.PropertyType == typeof(RelayCommand)))
{
var value = prop.GetValue(this) as RelayCommand;
if (value != null)
{
value.RaiseCanExecuteChanged();
}
}
}));
}
///
/// Gets a value indicating whether we are currently in VS design mode.
///
[NotMapped]
public bool DesignMode
{
get { return (DesignerProperties.GetIsInDesignMode(new DependencyObject())); }
}
///
/// Invokes the specified action on the UI Thread.
///
/// The action.
protected virtual void InvokeUI(Action action)
{
Application.Current.Dispatcher.BeginInvoke(action);
}
///
/// Invokes the specified action on the UI Thread.
///
/// The action.
protected virtual void InvokeUINow(Action action)
{
Application.Current.Dispatcher.Invoke(action);
}
}
}