System.Windows.Markup.XamlParseException: ""Предоставление значения для "System.Windows.Baml2006.TypeConverterMarkupExtension" вызвало исключение.": номер строки "9" и позиция в строке "10"."
Внутреннее исключение
NotSupportedException: CommandConverter cannot convert from System.String.
<Window x:Class="WpfTextEditor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF Text Editor" Height="600" Width="900"
Closing="Exit_Handler">
<Window.CommandBindings>
<CommandBinding Command="Save" Executed="Save_Handler"/>
<CommandBinding Command="Open" Executed="OpenFile_Handler"/>
<CommandBinding Command="Exitfoo" Executed="Exit_Handler"/>
<CommandBinding Command="SaveAs" Executed="SaveAs_Handler"/>
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Command="Save" Key="S" Modifiers="Ctrl"/>
<KeyBinding Command="Open" Key="O" Modifiers="Ctrl"/>
<KeyBinding Command="Exitfoo" Key="W" Modifiers="Ctrl"/>
<KeyBinding Command="SaveAs" Key="S" Modifiers="Ctrl+Shift"/>
</Window.InputBindings>
...using Microsoft.Win32;
using System;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using Spire.Doc;
using Spire.Doc.Documents;
using TextAlignment = System.Windows.TextAlignment;
using Paragraph = System.Windows.Documents.Paragraph;
using Hyperlink = System.Windows.Documents.Hyperlink;
using System.ComponentModel;
using System.Windows.Controls.Primitives;
using System.Windows.Threading;
using NLog;
namespace WpfTextEditor
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private TextPointer _lastSearchPosition;
private double _defaultEditorFontSize;
private bool _hasUnsavedChanges = false;
private string _currentFileName = "";
private bool _isFirstEvent_Editor_TextChanged = true;
private bool _intervalSaving = false;
private DispatcherTimer _intervalSavingTimer = new DispatcherTimer();
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
public MainWindow()
{
InitializeComponent();
_lastSearchPosition = Editor.Document.ContentStart;
_defaultEditorFontSize = Editor.Document.FontSize;
_intervalSavingTimer.Interval = TimeSpan.FromMinutes(1);
_intervalSavingTimer.Tick += IntervalSavingTimerHandler;
_logger.Info("WpfTextEditor started");
}
private void Exit()
{
if (_hasUnsavedChanges)
{
MessageBoxResult result = MessageBox.Show(
$"Сохранить изменения в файле {_currentFileName}?",
"Сохранение",
MessageBoxButton.YesNo);
if (result == MessageBoxResult.No)
{
return;
}
else if (result == MessageBoxResult.Yes)
{
Save();
}
}
_logger.Info("WpfTextEditor exited");
}
private void Exit_Handler(object sender, CancelEventArgs e)
{
Exit();
}
private void Exit_Handler(object sender, ExecutedRoutedEventArgs e)
{
Exit();
}
...Command="Exitfoo"CommandBinding.Command Property: https://learn.microsoft.com/en-us/dotnet/api/syste...ApplicationCommands Class: https://learn.microsoft.com/en-us/dotnet/api/syste...Ctrl+W для выхода из приложения является контр-интуитивным и использовать такой хоткей для выхода из приложения нельзя, т.к. этот хоткей традиционно используется для закрытия вкладок. Кроме того, винда имеет стандартный хоткей Alt+F4 для закрытия окна или выхода из приложения.Command — т.к. это всё XML, то там строка и из этой строки надо сделать конвертацию в тот тип, который указан в классе CommandBinding для свойства Command, а именно в тип, реализующий интерфейс ICommand.Window через цепочку наследования в предках есть класс UIElement, который и реализует свойство привязки команд через коллекцию:public class System.Windows.UIElement : Visual, IInputElement, IAnimatable
{
public CommandBindingCollection CommandBindings { get; }
}public class System.Windows.Input.CommandBinding
{
public ICommand Command { get; set; }
}ICommand, а именно в класс System.Windows.Input.RoutedUICommand(src). Соответственно список этих команд находится в статическом классе System.Windows.Input.ApplicationCommands, среди которых и происходит поиск нужной команды. Костыльно? Да, костыльно, ну хотя бы работает.