diff --git a/Debug.xaml.cs b/Debug.xaml.cs index c29b2d8..25d2917 100644 --- a/Debug.xaml.cs +++ b/Debug.xaml.cs @@ -1,6 +1,7 @@ using Microsoft.Win32; using System; using System.IO; +using System.Threading; using System.Windows; namespace AnotherReplayReader @@ -20,12 +21,15 @@ namespace AnotherReplayReader } } - public event Action NewText; + public event Action? NewText; public Proxy DebugMessage { get => new Proxy(); set => NewText?.Invoke(value.Payload); } + + public event Action? RequestedSave; + public void RequestSave() => RequestedSave?.Invoke(); } /// /// Debug.xaml 的交互逻辑 @@ -33,35 +37,48 @@ namespace AnotherReplayReader public sealed partial class Debug : Window { public static readonly DebugMessageWrapper Instance = new DebugMessageWrapper(); + private static Debug? _window = null; - public Debug() + public static void Initialize() { - InitializeComponent(); - Instance.NewText += t => Dispatcher.Invoke(() => _textBox.AppendText(t)); + Interlocked.CompareExchange(ref _window, new(), null); + _window.InitializeComponent(); + Instance.NewText += _window.AppendText; + Instance.RequestedSave += _window.ExportInMainWindow; } - private void OnExport_Click(object sender, RoutedEventArgs e) - { - var saveFileDialog = new SaveFileDialog - { - Filter = "文本文档 (*.txt)|*.txt|所有文件 (*.*)|*.*", - OverwritePrompt = true, - }; + public static new void ShowDialog() => (_window as Window)?.ShowDialog(); - var result = saveFileDialog.ShowDialog(this); - if (result == true) + private Debug() { } + + private void OnExport_Click(object sender, RoutedEventArgs e) => Export(this); + + private void OnClear_Click(object sender, RoutedEventArgs e) => _textBox.Clear(); + + private void AppendText(string s) => Dispatcher.Invoke(() => _textBox.AppendText(s)); + + private void ExportInMainWindow() => Export(Application.Current.MainWindow); + + private void Export(Window owner) + { + Dispatcher.Invoke(() => { - using (var file = saveFileDialog.OpenFile()) - using (var writer = new StreamWriter(file)) + var saveFileDialog = new SaveFileDialog { - writer.Write(_textBox.Text); - } - } - } + Filter = "文本文档 (*.txt)|*.txt|所有文件 (*.*)|*.*", + OverwritePrompt = true, + }; - private void OnClear_Click(object sender, RoutedEventArgs e) - { - _textBox.Clear(); + var result = saveFileDialog.ShowDialog(owner); + if (result == true) + { + using (var file = saveFileDialog.OpenFile()) + using (var writer = new StreamWriter(file)) + { + writer.Write(_textBox.Text); + } + } + }); } } } diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 517fd37..2be3209 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -492,11 +492,7 @@ namespace AnotherReplayReader detailsWindow.ShowDialog(); } - private void OnDebugButton_Click(object sender, RoutedEventArgs e) - { - var debug = new Debug(); - debug.ShowDialog(); - } + private void OnDebugButton_Click(object sender, RoutedEventArgs e) => Debug.ShowDialog(); private void OnPlayReplayButton_Click(object sender, RoutedEventArgs e) {