BililiveRecorder/BililiveRecorder.WPF/Models/Commands.cs

55 lines
1.6 KiB
C#
Raw Normal View History

2020-12-11 19:22:04 +08:00
using System;
using System.Diagnostics;
using System.Windows;
2020-12-11 19:22:04 +08:00
using System.Windows.Input;
using ModernWpf.Controls;
2021-02-23 18:03:37 +08:00
#nullable enable
2020-12-11 19:22:04 +08:00
namespace BililiveRecorder.WPF.Models
{
public class Commands : ICommand
{
#region Static Commands
public static Commands OpenLink { get; } = new Commands
{
ExecuteDelegate = o => { try { Process.Start(o.ToString()); } catch (Exception) { } }
};
public static Commands OpenContentDialog { get; } = new Commands
{
2021-02-23 18:03:37 +08:00
#pragma warning disable VSTHRD101 // Avoid unsupported async delegates
ExecuteDelegate = async o => { try { await (o as ContentDialog)!.ShowAsync(); } catch (Exception) { } }
#pragma warning restore VSTHRD101 // Avoid unsupported async delegates
2020-12-11 19:22:04 +08:00
};
public static Commands Copy { get; } = new Commands
{
ExecuteDelegate = e => { try { if (e is string str) Clipboard.SetText(str); } catch (Exception) { } }
};
2020-12-11 19:22:04 +08:00
#endregion
2021-02-23 18:03:37 +08:00
public Predicate<object>? CanExecuteDelegate { get; set; }
public Action<object>? ExecuteDelegate { get; set; }
2020-12-11 19:22:04 +08:00
#region ICommand Members
2021-01-01 14:46:27 +08:00
public void Execute(object parameter) => this.ExecuteDelegate?.Invoke(parameter);
2020-12-11 19:22:04 +08:00
2021-01-01 14:46:27 +08:00
public bool CanExecute(object parameter) => this.CanExecuteDelegate switch
2020-12-11 19:22:04 +08:00
{
null => true,
2021-01-01 14:46:27 +08:00
_ => this.CanExecuteDelegate(parameter),
2020-12-11 19:22:04 +08:00
};
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
#endregion
}
}