AnotherReplayReader/Debug.xaml.cs
2021-10-13 16:47:59 +02:00

68 lines
1.8 KiB
C#

using Microsoft.Win32;
using System;
using System.IO;
using System.Windows;
namespace AnotherReplayReader
{
public sealed class DebugMessageWrapper
{
public readonly struct Proxy
{
public readonly string Payload;
public Proxy(string text)
{
Payload = text;
}
public static Proxy operator +(Proxy p, string text)
{
return string.IsNullOrEmpty(p.Payload) ? new Proxy(text) : new Proxy(p.Payload + text);
}
}
public event Action<string> NewText;
public Proxy DebugMessage
{
get => new Proxy();
set => NewText?.Invoke(value.Payload);
}
}
/// <summary>
/// Debug.xaml 的交互逻辑
/// </summary>
public sealed partial class Debug : Window
{
public static readonly DebugMessageWrapper Instance = new DebugMessageWrapper();
public Debug()
{
InitializeComponent();
Instance.NewText += t => Dispatcher.Invoke(() => _textBox.AppendText(t));
}
private void OnExport_Click(object sender, RoutedEventArgs e)
{
var saveFileDialog = new SaveFileDialog
{
Filter = "文本文档 (*.txt)|*.txt|所有文件 (*.*)|*.*",
OverwritePrompt = true,
};
var result = saveFileDialog.ShowDialog(this);
if (result == true)
{
using (var file = saveFileDialog.OpenFile())
using (var writer = new StreamWriter(file))
{
writer.Write(_textBox.Text);
}
}
}
private void OnClear_Click(object sender, RoutedEventArgs e)
{
_textBox.Clear();
}
}
}