Mock my homebrew MVVM code!!!



  • ViewModelBase and DialogViewModel are used to invert the handling of window creation to be automated by the view models.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Markup;
    
    namespace stuff
    {
        /// <summary>
        /// Any window that can close and supports a datacontext
        /// </summary>
        public interface IView
        {
            object DataContext { get; set; }
            void Close();
        }
    
        /// <summary>
        /// Represents the base class for all window views. Allows the viewmodel to consume the view.
        /// </summary>
        /// <typeparam name="ViewType"></typeparam>
        public class ViewModelBase<ViewType> : DataViewModel
            where ViewType : IView
        {
            private readonly ViewType view;
            public object DataContext
            {
                get { return this.View.DataContext; }
                set { this.View.DataContext = value; }
            }
            public ViewType View
            {
                get
                {
                    return this.view;
                }
            }
            public ViewModelBase(ViewType view)
            {
                this.view = view;
                this.View.DataContext = this;
            }
        }
    
        /// <summary>
        /// This class allows a dialog view model to consume a dialog window as a view.
        /// </summary>
        public class DialogViewModelBase : ViewModelBase<BaseDialogWindow>
        {
            public DialogViewModelBase(BaseDialogWindow window) : base(window) { }
            public virtual bool? ShowDialog()
            {
                return this.View.ShowDialog();
            }
            public System.Windows.Window Owner
            {
                get { return this.View.Owner; }
                protected set { this.View.Owner = value; }
            }
            public bool? DialogResult
            {
                get
                {
                    return this.View.DialogResult;
                }
                protected set
                {
                    this.View.DialogResult = value;
                }
            }
            public void Close()
            {
                this.View.Close();
            }
    
            private RelayCommand command;
            public RelayCommand OkCommand
            {
                get
                {
                    this.command = new RelayCommand(this.Ok);
                    return this.command;
                }
                set
                {
                    this.command = value;
                }
            }
    
            void Ok()
            {
                DialogResult = true;
                Close();
            }
            public RelayCommand CancelCommand
            {
                get
                {
                    this.command = new RelayCommand(this.Cancel);
                    return this.command;
                }
                set
                {
                    this.command = value;
                }
            }
    
            void Cancel()
            {
                DialogResult = false;
                Close();
            }
        }
        public class RelayCommand : ICommand
        {
            public event EventHandler CanExecuteChanged
            {
                add { CommandManager.RequerySuggested += value; }
                remove { CommandManager.RequerySuggested -= value; }
            }
            private Action methodToExecute;
            private Func<bool> canExecuteEvaluator;
            public RelayCommand(Action methodToExecute, Func<bool> canExecuteEvaluator)
            {
                this.methodToExecute = methodToExecute;
                this.canExecuteEvaluator = canExecuteEvaluator;
            }
            public RelayCommand(Action methodToExecute)
                : this(methodToExecute, null)
            {
            }
            public bool CanExecute(object parameter)
            {
                if (this.canExecuteEvaluator == null)
                {
                    return true;
                }
                else
                {
                    bool result = this.canExecuteEvaluator.Invoke();
                    return result;
                }
            }
            public void Execute(object parameter)
            {
                this.methodToExecute.Invoke();
            }
        }
        public interface IDialogView : IView
        {
            bool? ShowDialog();
            bool? DialogResult { get; }
            System.Windows.Window Owner { get; }
        }
        public class BaseDialogWindow : Window, IView
        {
            public BaseDialogWindow()
            {
                this.Owner = App.Current.MainWindow;
                this.ShowInTaskbar = false;
                this.ResizeMode = System.Windows.ResizeMode.NoResize;
                this.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
            }
        }
    
    
        public class DataViewModel : INotifyPropertyChanged
        {
            private Dictionary<string, object> _properties = new Dictionary<string, object>();
    
            protected TValue GetValue<TValue>([CallerMemberName]string propertyName = null)
            {
    
                if (propertyName == null) throw new ArgumentException("Property Name cannot be null");
                object value = null;
                if (!_properties.TryGetValue(propertyName, out value))
                {
                    _properties[propertyName] = value = default(TValue);
                }
                return (TValue)value;
            }
    
            internal TValue GetValueInternal<TValue>(string propertyName) { return GetValue<TValue>(propertyName); }
            internal void SetValueInternal<TValue>(TValue value, string propertyName) { SetValue<TValue>(value, propertyName); }
    
            protected void SetValue<TValue>(TValue value, [CallerMemberName]string propertyName = null)
            {
                if (propertyName == null) throw new ArgumentException("Property Name cannot be null");
                _properties[propertyName] = value;
                NotifyPropertyChanged(propertyName);
            }
    
            protected void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
            public event PropertyChangedEventHandler PropertyChanged;
        }
    
        public class EnumerationExtension : MarkupExtension
        {
            private Type _enumType;
    
    
            public EnumerationExtension(Type enumType)
            {
                if (enumType == null)
                    throw new ArgumentNullException("enumType");
    
                EnumType = enumType;
            }
    
            public Type EnumType
            {
                get { return _enumType; }
                private set
                {
                    if (_enumType == value)
                        return;
    
                    var enumType = Nullable.GetUnderlyingType(value) ?? value;
    
                    if (enumType.IsEnum == false)
                        throw new ArgumentException("Type must be an Enum.");
    
                    _enumType = value;
                }
            }
    
            public override object ProvideValue(IServiceProvider serviceProvider)
            {
                var enumValues = Enum.GetValues(EnumType);
    
                return (
                  from object enumValue in enumValues
                  select new EnumerationMember
                  {
                      Value = enumValue,
                      Description = GetDescription(enumValue)
                  }).ToArray();
            }
    
            private string GetDescription(object enumValue)
            {
                var descriptionAttribute = EnumType
                  .GetField(enumValue.ToString())
                  .GetCustomAttributes(typeof(DescriptionAttribute), false)
                  .FirstOrDefault() as DescriptionAttribute;
    
    
                return descriptionAttribute != null
                  ? descriptionAttribute.Description
                  : enumValue.ToString();
            }
    
            public class EnumerationMember
            {
                public string Description { get; set; }
                public object Value { get; set; }
            }
        }
    
    
    }
    
    


  • 0_1490713272775_upload-5487a27a-dd1f-4d2a-99c0-741258944ba6

    Can we mock hljs instead?


  • Banned

    This post is deleted!

Log in to reply