first edit
This commit is contained in:
10
XLIMS.MVVM/AssemblyInfo.cs
Normal file
10
XLIMS.MVVM/AssemblyInfo.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System.Windows;
|
||||
|
||||
[assembly: ThemeInfo(
|
||||
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
|
||||
//(used if a resource is not found in the page,
|
||||
// or application resource dictionaries)
|
||||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
|
||||
//(used if a resource is not found in the page,
|
||||
// app, or any theme specific resource dictionaries)
|
||||
)]
|
||||
112
XLIMS.MVVM/Base/AsyncRelayCommand.cs
Normal file
112
XLIMS.MVVM/Base/AsyncRelayCommand.cs
Normal file
@@ -0,0 +1,112 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace XLIMS.MVVM.Base
|
||||
{
|
||||
/// <summary>
|
||||
/// Асинхронная команда без параметра.
|
||||
/// </summary>
|
||||
public class AsyncRelayCommand : ICommand
|
||||
{
|
||||
private readonly Func<Task> _execute;
|
||||
private readonly Func<bool>? _canExecute;
|
||||
|
||||
private bool _isExecuting;
|
||||
|
||||
public AsyncRelayCommand(Func<Task> execute, Func<bool>? canExecute = null)
|
||||
{
|
||||
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
|
||||
_canExecute = canExecute;
|
||||
}
|
||||
|
||||
public bool CanExecute(object? parameter)
|
||||
{
|
||||
if (_isExecuting) return false;
|
||||
return _canExecute == null || _canExecute();
|
||||
}
|
||||
|
||||
public async void Execute(object? parameter)
|
||||
{
|
||||
if (!CanExecute(parameter)) return;
|
||||
|
||||
try
|
||||
{
|
||||
_isExecuting = true;
|
||||
CommandManager.InvalidateRequerySuggested();
|
||||
|
||||
await _execute();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_isExecuting = false;
|
||||
CommandManager.InvalidateRequerySuggested();
|
||||
}
|
||||
}
|
||||
|
||||
public event EventHandler? CanExecuteChanged
|
||||
{
|
||||
add { CommandManager.RequerySuggested += value; }
|
||||
remove { CommandManager.RequerySuggested -= value; }
|
||||
}
|
||||
|
||||
// Удобный статический метод для создания из метода с параметром (если вдруг понадобится)
|
||||
public static AsyncRelayCommand FromAsync(Func<object?, Task> execute, Func<object?, bool>? canExecute = null)
|
||||
{
|
||||
return new AsyncRelayCommand(() => execute(null), canExecute == null ? null : () => canExecute(null));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Асинхронная команда с параметром (типизированная).
|
||||
/// </summary>
|
||||
public class AsyncRelayCommand<T> : ICommand
|
||||
{
|
||||
private readonly Func<T?, Task> _execute;
|
||||
private readonly Predicate<T?>? _canExecute;
|
||||
|
||||
private bool _isExecuting;
|
||||
|
||||
public AsyncRelayCommand(Func<T?, Task> execute, Predicate<T?>? canExecute = null)
|
||||
{
|
||||
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
|
||||
_canExecute = canExecute;
|
||||
}
|
||||
|
||||
public bool CanExecute(object? parameter)
|
||||
{
|
||||
if (_isExecuting) return false;
|
||||
if (_canExecute == null) return true;
|
||||
|
||||
// Если параметр неправильного типа — считаем, что нельзя выполнить
|
||||
return parameter is T typedParam && _canExecute(typedParam);
|
||||
}
|
||||
|
||||
public async void Execute(object? parameter)
|
||||
{
|
||||
if (!CanExecute(parameter)) return;
|
||||
|
||||
T? typedParam = parameter is T p ? p : default;
|
||||
|
||||
try
|
||||
{
|
||||
_isExecuting = true;
|
||||
CommandManager.InvalidateRequerySuggested();
|
||||
|
||||
await _execute(typedParam);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_isExecuting = false;
|
||||
CommandManager.InvalidateRequerySuggested();
|
||||
}
|
||||
}
|
||||
|
||||
public event EventHandler? CanExecuteChanged
|
||||
{
|
||||
add { CommandManager.RequerySuggested += value; }
|
||||
remove { CommandManager.RequerySuggested -= value; }
|
||||
}
|
||||
}
|
||||
}
|
||||
89
XLIMS.MVVM/Base/RelayCommand.cs
Normal file
89
XLIMS.MVVM/Base/RelayCommand.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace XLIMS.MVVM.Base
|
||||
{
|
||||
public class RelayCommand : ICommand
|
||||
{
|
||||
#region Fields
|
||||
|
||||
readonly Action<object> _execute;
|
||||
readonly Predicate<object> _canExecute;
|
||||
|
||||
#endregion // Fields
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new command that can always execute.
|
||||
/// </summary>
|
||||
/// <param name="execute">The execution logic.</param>
|
||||
public RelayCommand(Action<object> execute)
|
||||
: this(execute, null)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new command.
|
||||
/// </summary>
|
||||
/// <param name="execute">The execution logic.</param>
|
||||
/// <param name="canExecute">The execution status logic.</param>
|
||||
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
|
||||
{
|
||||
if (execute == null)
|
||||
throw new ArgumentNullException("execute");
|
||||
|
||||
_execute = execute;
|
||||
_canExecute = canExecute;
|
||||
}
|
||||
|
||||
#endregion // Constructors
|
||||
|
||||
#region ICommand Members
|
||||
|
||||
[DebuggerStepThrough]
|
||||
public bool CanExecute(object parameter)
|
||||
{
|
||||
return _canExecute == null ? true : _canExecute(parameter);
|
||||
}
|
||||
|
||||
public event EventHandler CanExecuteChanged
|
||||
{
|
||||
add { CommandManager.RequerySuggested += value; }
|
||||
remove { CommandManager.RequerySuggested -= value; }
|
||||
}
|
||||
|
||||
public void Execute(object parameter)
|
||||
{
|
||||
_execute(parameter);
|
||||
}
|
||||
|
||||
#endregion // ICommand Members
|
||||
}
|
||||
|
||||
public class RelayCommand<T> : ICommand
|
||||
{
|
||||
private readonly Action<T> _execute;
|
||||
private readonly Func<T, bool> _canExecute;
|
||||
|
||||
public RelayCommand(Action<T> execute, Func<T, bool> canExecute = null)
|
||||
{
|
||||
_execute = execute;
|
||||
_canExecute = canExecute;
|
||||
}
|
||||
|
||||
public bool CanExecute(object parameter)
|
||||
=> _canExecute == null || _canExecute((T)parameter);
|
||||
|
||||
public void Execute(object parameter)
|
||||
=> _execute((T)parameter);
|
||||
|
||||
public event EventHandler CanExecuteChanged
|
||||
{
|
||||
add => CommandManager.RequerySuggested += value;
|
||||
remove => CommandManager.RequerySuggested -= value;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
25
XLIMS.MVVM/Base/ViewModelBase.cs
Normal file
25
XLIMS.MVVM/Base/ViewModelBase.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Linq.Expressions;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace XLIMS.MVVM.Base
|
||||
{
|
||||
public abstract class ViewModelBase : INotifyPropertyChanged
|
||||
{
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
protected void OnPropertyChanged([CallerMemberName] String propertyName="")
|
||||
{
|
||||
PropertyChangedEventHandler handler = PropertyChanged;
|
||||
|
||||
if (handler != null)
|
||||
{
|
||||
handler(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
31
XLIMS.MVVM/Converters/BoolConverter.cs
Normal file
31
XLIMS.MVVM/Converters/BoolConverter.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Windows.Data;
|
||||
using System.Globalization;
|
||||
|
||||
namespace XLIMS.MVVM.Converters
|
||||
{
|
||||
[ValueConversion(typeof(Boolean), typeof(String))]
|
||||
public class BoolConverter : IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
try
|
||||
{
|
||||
Boolean data = (Boolean)value;
|
||||
return data;
|
||||
}
|
||||
catch { return value; }
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
string strValue = value.ToString();
|
||||
Boolean resultData;
|
||||
if (Boolean.TryParse(strValue, out resultData))
|
||||
{
|
||||
return resultData;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
35
XLIMS.MVVM/Converters/CompositeCollectionConverter.cs
Normal file
35
XLIMS.MVVM/Converters/CompositeCollectionConverter.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace XLIMS.MVVM.Converters
|
||||
{
|
||||
[ValueConversion(typeof(object),typeof(CompositeCollection))]
|
||||
public class CompositeCollectionConverter : IMultiValueConverter
|
||||
{
|
||||
|
||||
public object Convert(object[] values
|
||||
, Type targetType
|
||||
, object parameter
|
||||
, System.Globalization.CultureInfo culture)
|
||||
{
|
||||
var res = new CompositeCollection();
|
||||
foreach (var item in values)
|
||||
if (item is IEnumerable)
|
||||
res.Add(new CollectionContainer()
|
||||
{
|
||||
Collection = item as IEnumerable
|
||||
});
|
||||
else res.Add(item);
|
||||
return res;
|
||||
}
|
||||
|
||||
public object[] ConvertBack(object value
|
||||
, Type[] targetTypes
|
||||
, object parameter
|
||||
, System.Globalization.CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
25
XLIMS.MVVM/Converters/DateConverter.cs
Normal file
25
XLIMS.MVVM/Converters/DateConverter.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Windows.Data;
|
||||
using System.Globalization;
|
||||
|
||||
namespace XLIMS.MVVM.Converters
|
||||
{
|
||||
[ValueConversion(typeof(DateTime), typeof(string))]
|
||||
public class DateConverter : IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
if (!(value is DateTime)) return string.Empty;
|
||||
DateTime test = (DateTime)value;
|
||||
string date = test.ToString();
|
||||
return (date);
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
var strValue = value.ToString();
|
||||
DateTime resultDateTime;
|
||||
return DateTime.TryParse(strValue, out resultDateTime) ? resultDateTime : value;
|
||||
}
|
||||
}
|
||||
}
|
||||
34
XLIMS.MVVM/Converters/ImageConverter.cs
Normal file
34
XLIMS.MVVM/Converters/ImageConverter.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Data;
|
||||
using System.Drawing;
|
||||
|
||||
namespace XLIMS.MVVM.Converters
|
||||
{
|
||||
[ValueConversion(typeof(Byte[]), typeof(Image))]
|
||||
public class ImageConverter : IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
try
|
||||
{
|
||||
var ms = new MemoryStream((byte[])value);
|
||||
var returnImage = Image.FromStream(ms);
|
||||
return returnImage;
|
||||
}
|
||||
catch { return value; }
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
//MemoryStream ms = new MemoryStream();
|
||||
//value.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
32
XLIMS.MVVM/Converters/VisibilityConverter.cs
Normal file
32
XLIMS.MVVM/Converters/VisibilityConverter.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Windows.Data;
|
||||
using System.Globalization;
|
||||
using System.Windows;
|
||||
|
||||
namespace XLIMS.MVVM.Converters
|
||||
{
|
||||
[ValueConversion(typeof(string), typeof(Visibility))]
|
||||
public class VisibilityConverter : IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
if (value != null)
|
||||
{
|
||||
return Visibility.Visible;
|
||||
//string s = value.ToString();
|
||||
//object s1 = value;
|
||||
//if (s1 is decimal)
|
||||
// if ((decimal.Parse(s1.ToString())) == 0) return Visibility.Collapsed;
|
||||
//if (s1 is bool)
|
||||
// if ((bool.Parse(s1.ToString())) == false) return Visibility.Collapsed;
|
||||
//if (string.IsNullOrEmpty(s)) return Visibility.Collapsed;
|
||||
}
|
||||
return Visibility.Collapsed;
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
10
XLIMS.MVVM/XLIMS.MVVM.csproj
Normal file
10
XLIMS.MVVM/XLIMS.MVVM.csproj
Normal file
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0-windows</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<UseWPF>true</UseWPF>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user