WIn_RustTools/Editor/EditorWin.xaml.cs
muqing f21ffd521a 更新异常处理、控件属性和事件处理程序
在 `App.xaml.cs` 文件中,捕获 `Exception` 异常时,添加了调试输出信息,输出内容为“发生未知错误: ”加上异常信息。
在 `EditorWin.xaml` 文件中,将 `TreeView` 的 `CanReorderItems` 属性从 `True` 修改为 `False`。
在 `EditorWin.xaml` 文件中,`TabView` 控件添加了 `AddTabButtonClick` 和 `TabCloseRequested` 事件处理程序,并将 `IsAddTabButtonVisible` 属性设置为 `False`。
在 `EditorWin.xaml.cs` 文件中,添加了 `MyTabView_AddTabButtonClick` 和 `MyTabView_TabCloseRequested` 方法,用于处理添加和关闭选项卡的事件。
在 `RustTools.csproj` 文件中,调整了注释的格式,使其在 VS Code 中显示正确的图标,并调整了 `ItemGroup` 的格式。
2025-01-16 19:22:27 +08:00

111 lines
3.0 KiB
C#

using System.Collections.ObjectModel;
using System.Diagnostics;
using Microsoft.UI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using RustTools.muqing;
using WinRT.Interop;
namespace RustTools.Editor;
/// <summary>
/// 编辑器主窗口
/// </summary>
public sealed partial class EditorWin : WindowEx
{
public static ObservableCollection<TabViewItem> TabViewList = new();
//目录列表
public ObservableCollection<FileItem> DataSource = new();
/// <summary>
/// 编辑器主窗口
/// 传入路径
/// </summary>
/// <param name="path"></param>
public EditorWin(string path)
{
InitializeComponent();
gj.SetBackTheme(this);
ExtendsContentIntoTitleBar = true;
var frame = App.AppTitlebar as FrameworkElement;
if (frame != null)
{
gj.UpdateTitleBar(this, frame.ActualTheme);
page.RequestedTheme = frame.ActualTheme;
}
//Debug.WriteLine(path);
var directoryInfo = new DirectoryInfo(path);
Title = directoryInfo.Name;
TitleText.Text = directoryInfo.Name;
DataSource = EditorLoad.GetData(directoryInfo.FullName);
treeView.ItemsSource = DataSource;
Closed += EditorWin_Closed;
tabview.TabItemsSource = TabViewList;
}
//用户有没有保存
private bool IsSave = true;
private ContentDialog? ClosedDialog = null;
private async void EditorWin_Closed(object sender, WindowEventArgs e)
{
if (IsSave == false)
{
// 防止窗口关闭
e.Handled = true;
}
if (ClosedDialog != null)
{
return;
}
// 显示一个对话框,告知用户窗口不能关闭
ClosedDialog = new ContentDialog
{
XamlRoot = page.XamlRoot,
Title = "警告",
Content = "你还有未保存的文件在编译器中。",
PrimaryButtonText = "保存关闭",
PrimaryButtonStyle = Application.Current.Resources["AccentButtonStyle"] as Style,
SecondaryButtonText = "直接关闭",
CloseButtonText = "取消"
};
var result = await ClosedDialog.ShowAsync();
if (result == ContentDialogResult.Primary)
{
IsSave = true;
Application.Current.Exit();
return;
}
else if (result == ContentDialogResult.Secondary)
{
IsSave = true;
Application.Current.Exit();
return;
}
ClosedDialog = null;
}
// 添加新选项卡
private void MyTabView_AddTabButtonClick(TabView sender, object args)
{
var newTab = new TabViewItem
{
Header = $"New Tab {TabViewList.Count + 1}",
Content = new TextBlock { Text = "This is a new tab." }
};
TabViewList.Add(newTab);
}
// 关闭选项卡
private void MyTabView_TabCloseRequested(TabView sender, TabViewTabCloseRequestedEventArgs args)
{
if (args.Tab is TabViewItem tab)
{
TabViewList.Remove(tab);
}
}
}