Skip to content

Commit f2c496a

Browse files
committed
修复由于显示程序描述名称导致的重复添加判断出错造成的崩溃
1 parent a705ad0 commit f2c496a

File tree

7 files changed

+118
-51
lines changed

7 files changed

+118
-51
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace PreLaunchTaskr.Common.Helpers;
9+
10+
/// <summary>
11+
/// 对指定路径的文件获取描述文本
12+
/// </summary>
13+
public class FileDescriber
14+
{
15+
public static string Describe(string path)
16+
{
17+
FileVersionInfo info = FileVersionInfo.GetVersionInfo(path);
18+
19+
if (!string.IsNullOrWhiteSpace(info.FileDescription))
20+
return info.FileDescription;
21+
22+
if (!string.IsNullOrWhiteSpace(info.ProductName))
23+
return info.ProductName;
24+
25+
return System.IO.Path.GetFileName(path);
26+
}
27+
}

PreLaunchTaskr.GUI.WinUI3/ViewModels/ItemModels/ProgramListItem.cs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
using Microsoft.UI.Xaml.Media.Imaging;
44

55
using PreLaunchTaskr.Common;
6+
using PreLaunchTaskr.Common.Helpers;
67
using PreLaunchTaskr.Core;
78
using PreLaunchTaskr.Core.Entities;
89
using PreLaunchTaskr.GUI.Common.AbstractViewModels.ItemModels;
910
using PreLaunchTaskr.GUI.WinUI3.Utils;
1011

1112
using System;
1213
using System.Collections.Generic;
14+
using System.Diagnostics;
1315
using System.Text;
1416

1517
namespace PreLaunchTaskr.GUI.WinUI3.ViewModels.ItemModels;
@@ -19,26 +21,10 @@ public partial class ProgramListItem : ObservableObject, IProgramListItem<Bitmap
1921
public ProgramListItem(ProgramInfo programInfo)
2022
{
2123
ProgramInfo = programInfo;
22-
Name = GetProgramNameFromPath(programInfo.Path)!;
24+
Name = FileDescriber.Describe(programInfo.Path)!;
2325
Icon = IconBitmapImageReader.ReadAssociated(programInfo.Path) ?? defaultProgramIcon;
2426
changed = false;
2527
}
26-
27-
private static string GetProgramNameFromPath(string path)
28-
{
29-
if (System.IO.File.Exists(path) && System.IO.Path.GetExtension(path).ToLowerInvariant() == ".exe")
30-
{
31-
var info = System.Diagnostics.FileVersionInfo.GetVersionInfo(path);
32-
33-
// 优先使用FileDescription,如果为空则使用ProductName,最后使用文件名
34-
if (!string.IsNullOrWhiteSpace(info.FileDescription))
35-
return info.FileDescription;
36-
37-
if (!string.IsNullOrWhiteSpace(info.ProductName))
38-
return info.ProductName;
39-
}
40-
return System.IO.Path.GetFileName(path);
41-
}
4228

4329
public int Id => ProgramInfo.Id;
4430

PreLaunchTaskr.GUI.WinUI3/ViewModels/PageModels/MainViewModel.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
using System;
2-
using CommunityToolkit.Mvvm.ComponentModel;
1+
using CommunityToolkit.Mvvm.ComponentModel;
32

43
using Microsoft.UI.Dispatching;
54
using Microsoft.UI.Xaml.Media.Imaging;
65

76
using PreLaunchTaskr.Common;
7+
using PreLaunchTaskr.Common.Helpers;
88
using PreLaunchTaskr.Core;
99
using PreLaunchTaskr.Core.Entities;
1010
using PreLaunchTaskr.GUI.Common.AbstractViewModels.PageModels;
1111
using PreLaunchTaskr.GUI.WinUI3.ViewModels.ItemModels;
1212

13+
using System;
1314
using System.Collections.Generic;
1415
using System.Collections.ObjectModel;
16+
using System.Diagnostics;
1517
using System.Linq;
1618
using System.Text;
1719
using System.Threading.Tasks;
@@ -73,6 +75,12 @@ public bool AddProgram(string name, string path)
7375
return true;
7476
}
7577

78+
//public bool AddProgram(string path)
79+
//{
80+
// string name = FileDescriber.Describe(path);
81+
// return AddProgram(name, path);
82+
//}
83+
7684
public bool RemoveProgram(ProgramListItem item)
7785
{
7886
Programs.Remove(item);

PreLaunchTaskr.GUI.WinUI3/Views/MainPage.xaml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,18 @@
4848
<ColumnDefinition Width="auto" />
4949
</Grid.ColumnDefinitions>
5050

51-
<StackPanel Grid.Column="0" ToolTipService.ToolTip="{x:Bind Path}">
52-
<TextBlock Text="{x:Bind Name}" TextWrapping="NoWrap" />
51+
<StackPanel Grid.Column="0">
52+
<TextBlock
53+
Text="{x:Bind Name}"
54+
TextTrimming="CharacterEllipsis"
55+
TextWrapping="NoWrap"
56+
ToolTipService.ToolTip="{x:Bind Name}" />
5357
<TextBlock
5458
Opacity="0.5"
5559
Text="{x:Bind Path}"
5660
TextTrimming="CharacterEllipsis"
57-
TextWrapping="NoWrap" />
61+
TextWrapping="NoWrap"
62+
ToolTipService.ToolTip="{x:Bind Path}" />
5863
</StackPanel>
5964

6065
<ToggleSwitch

PreLaunchTaskr.GUI.WinUI3/Views/MainPage.xaml.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ private async void SelectProgramFromFile()
8383
if (string.IsNullOrWhiteSpace(path))
8484
return;
8585

86-
string filename = Path.GetFileName(path);
87-
if (! viewModel!.AddProgram(filename, path))
86+
string name = FileDescriber.Describe(path);
87+
if (! viewModel!.AddProgram(name, path))
8888
{
89-
await ShowFileNameExistedMessageBox(filename);
89+
await ShowFileNameExistedMessageBox(name);
9090
}
9191
}
9292

@@ -105,10 +105,12 @@ private async void InputProgramPath()
105105
return;
106106
}
107107

108-
string name = Path.GetFileName(path);
108+
string name = FileDescriber.Describe(path);
109109

110110
if (! viewModel!.AddProgram(name, path))
111+
{
111112
await ShowFileNameExistedMessageBox(name);
113+
}
112114
}
113115

114116
private async void SelectInstalledProgram()
@@ -161,10 +163,11 @@ private async void SelectInstalledProgram()
161163
if (string.IsNullOrWhiteSpace(path))
162164
return;
163165

164-
string filename = Path.GetFileName(path);
165-
if (! viewModel!.AddProgram(filename, path))
166+
string name = FileDescriber.Describe(path);
167+
168+
if (! viewModel!.AddProgram(name, path))
166169
{
167-
await ShowFileNameExistedMessageBox(filename);
170+
await ShowFileNameExistedMessageBox(name);
168171
}
169172
}
170173

@@ -286,7 +289,7 @@ private async void AddProgramFromDrop(object _, DragEventArgs e)
286289

287290
if (Path.GetExtension(targetName)?.ToLowerInvariant() == ".exe")
288291
{
289-
if (!viewModel!.AddProgram(targetName, targetPath))
292+
if (!viewModel!.AddProgram(FileDescriber.Describe(targetPath), targetPath))
290293
{
291294
duplicatedFileNames.Add(item.Path);
292295
}

PreLaunchTaskr.GUI.WinUI3/Views/ProgramConfigPage.xaml

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -109,42 +109,75 @@
109109
Orientation="Horizontal"
110110
Spacing="8">
111111

112-
<DropDownButton>
113-
<TextBlock Text="启动" />
112+
<Button Style="{StaticResource MyEllipsisButtonStyle}">
113+
<SymbolIcon Symbol="More" />
114114

115-
<DropDownButton.Flyout>
115+
<Button.Flyout>
116116
<MenuFlyout Placement="Bottom">
117-
<MenuFlyoutItem
118-
Click="Launch_Click"
119-
DataContext="{x:Bind viewModel.ProgramListItem}"
120-
Text="启动">
117+
<MenuFlyoutItem Click="CopyFilePath_Click" Text="复制程序所在路径">
121118
<MenuFlyoutItem.Icon>
122-
<SymbolIcon Symbol="Go" />
119+
<SymbolIcon Symbol="Copy" />
123120
</MenuFlyoutItem.Icon>
124121
</MenuFlyoutItem>
125122

126123
<MenuFlyoutItem
127-
Click="LaunchAsAdmin_Click"
128-
DataContext="{x:Bind viewModel.ProgramListItem}"
129-
Text="以管理员身份启动">
124+
Click="GoToFilePath_Click"
125+
DataContext="{x:Bind}"
126+
Text="打开程序所在位置">
130127
<MenuFlyoutItem.Icon>
131-
<SymbolIcon Symbol="Admin" />
128+
<SymbolIcon Symbol="Go" />
132129
</MenuFlyoutItem.Icon>
133130
</MenuFlyoutItem>
134131

135-
<MenuFlyoutItem IsEnabled="False" Text="是否按配置启动与此应用的开关有关">
136-
<MenuFlyoutItem.Icon>
137-
<!-- Info -->
138-
<FontIcon Glyph="&#xE946;" />
139-
</MenuFlyoutItem.Icon>
140-
</MenuFlyoutItem>
132+
<MenuFlyoutSubItem Text="启动">
133+
<MenuFlyoutSubItem.Icon>
134+
<SymbolIcon Symbol="Play" />
135+
</MenuFlyoutSubItem.Icon>
136+
137+
<MenuFlyoutItem
138+
Click="Launch_Click"
139+
DataContext="{x:Bind viewModel.ProgramListItem}"
140+
Text="启动">
141+
<MenuFlyoutItem.Icon>
142+
<SymbolIcon Symbol="Play" />
143+
</MenuFlyoutItem.Icon>
144+
</MenuFlyoutItem>
145+
146+
<MenuFlyoutItem
147+
Click="LaunchAsAdmin_Click"
148+
DataContext="{x:Bind viewModel.ProgramListItem}"
149+
Text="以管理员身份启动">
150+
<MenuFlyoutItem.Icon>
151+
<SymbolIcon Symbol="Admin" />
152+
</MenuFlyoutItem.Icon>
153+
</MenuFlyoutItem>
154+
155+
<MenuFlyoutItem IsEnabled="False" Text="是否按配置启动与此应用的开关有关">
156+
<MenuFlyoutItem.Icon>
157+
<!-- Info -->
158+
<FontIcon Glyph="&#xE946;" />
159+
</MenuFlyoutItem.Icon>
160+
</MenuFlyoutItem>
161+
</MenuFlyoutSubItem>
162+
163+
<!--<MenuFlyoutSubItem Text="移除" Foreground="Red">
164+
<MenuFlyoutSubItem.Icon>
165+
<SymbolIcon Symbol="Delete" />
166+
</MenuFlyoutSubItem.Icon>
167+
168+
<MenuFlyoutItem Text="确认移除" Foreground="Red" Click="MenuFlyoutItem_Click">
169+
<MenuFlyoutItem.Icon>
170+
<SymbolIcon Symbol="Delete" />
171+
</MenuFlyoutItem.Icon>
172+
</MenuFlyoutItem>
173+
</MenuFlyoutSubItem>-->
141174
</MenuFlyout>
142-
</DropDownButton.Flyout>
143-
</DropDownButton>
175+
</Button.Flyout>
176+
</Button>
144177

145178
<Button
146179
Click="{x:Bind viewModel.SaveChanges}"
147-
Content="立即应用设置"
180+
Content="应用设置"
148181
Style="{ThemeResource AccentButtonStyle}" />
149182
</StackPanel>
150183
</my:MyGrid>

PreLaunchTaskr.GUI.WinUI3/Views/ProgramConfigPage.xaml.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,9 @@ private void GoToFilePath_Click(object sender, RoutedEventArgs e)
109109
{
110110
WindowsHelper.OpenPathInExplorer(viewModel.Path);
111111
}
112+
113+
private void MenuFlyoutItem_Click(object sender, RoutedEventArgs e)
114+
{
115+
116+
}
112117
}

0 commit comments

Comments
 (0)