Files
ShrlAlgoToolkit/WPFAppTest/MainWindow.xaml.cs

109 lines
2.9 KiB
C#
Raw Normal View History

2024-09-22 11:05:41 +08:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
2025-04-24 20:56:44 +08:00
using WpfApp;
using WPFluent.Appearance;
using WPFluent.Controls;
2025-04-24 20:56:44 +08:00
using TextBox = System.Windows.Controls.TextBox;
2024-09-22 11:05:41 +08:00
2025-04-24 20:56:44 +08:00
namespace WPFAppTest;
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : FluentWindowEx
2024-09-22 11:05:41 +08:00
{
public MainWindow()
2024-09-22 11:05:41 +08:00
{
DataContext = new MainViewModel();
InitializeComponent();
2025-04-24 20:56:44 +08:00
//var res = Resources;
// 创建并添加自定义的 TraceListener
2025-04-24 20:56:44 +08:00
//var textBoxListener = new TextBoxTraceListener(DebugTextBox);
//Debug.Listeners.Add(textBoxListener);
//Debug.WriteLine("这是一个调试输出示例。");
App.FetchResources(Resources);
ApplicationThemeManager.Apply(this);
ApplicationThemeManager.Changed -= ApplicationThemeManager_Changed;
ApplicationThemeManager.Changed += ApplicationThemeManager_Changed;
App.FetchResources(Resources);
2024-09-22 11:05:41 +08:00
}
//public static BitmapSource ToBitmapSource(Bitmap bitmap)
//{
// if (bitmap == null)
// {
// return null;
// }
// return Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
//}
2025-04-24 20:56:44 +08:00
private void ApplicationThemeManager_Changed(ApplicationTheme currentApplicationTheme, System.Windows.Media.Color systemAccent)
{
ApplicationThemeManager.Apply(this);
}
2025-04-24 20:56:44 +08:00
private void Button_Click(object sender, RoutedEventArgs e)
{
if (ApplicationThemeManager.GetAppTheme() == ApplicationTheme.Light)
{
ApplicationThemeManager.Apply(ApplicationTheme.Dark);
}
else
{
ApplicationThemeManager.Apply(ApplicationTheme.Light);
}
}
}
public class TextBoxTraceListener : TraceListener
{
private TextBox _textBox;
public TextBoxTraceListener(TextBox textBox)
2024-09-22 11:05:41 +08:00
{
_textBox = textBox;
}
2024-09-22 11:05:41 +08:00
public override void Write(string message)
{
AppendText(message);
}
2024-09-22 11:05:41 +08:00
public override void WriteLine(string message)
{
AppendText(message + Environment.NewLine);
}
2024-09-22 11:05:41 +08:00
private void AppendText(string message)
{
if (_textBox.Dispatcher.CheckAccess())
2024-09-22 11:05:41 +08:00
{
_textBox.AppendText(message);
_textBox.ScrollToEnd();
2024-09-22 11:05:41 +08:00
}
else
2024-09-22 11:05:41 +08:00
{
_textBox.Dispatcher.Invoke(() =>
2024-09-22 11:05:41 +08:00
{
_textBox.AppendText(message);
_textBox.ScrollToEnd();
});
2024-09-22 11:05:41 +08:00
}
}
}