Skip to content

Commit 658f27b

Browse files
committed
1、使用单独的ViewModel来绑定页面数据
2、优化页面布局
1 parent 7a1a5ac commit 658f27b

File tree

15 files changed

+163
-49
lines changed

15 files changed

+163
-49
lines changed

src/FindDuplicateFiles/Common/GlobalArgs.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace FindDuplicateFiles.Common
1+
using FindDuplicateFiles.Model;
2+
3+
namespace FindDuplicateFiles.Common
24
{
35
public class GlobalArgs
46
{

src/FindDuplicateFiles/Extensions/ExtensionMethods.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,5 @@ public static bool IsNotEmpty(this string input)
1111
{
1212
return !string.IsNullOrEmpty(input);
1313
}
14-
15-
1614
}
1715
}

src/FindDuplicateFiles/FindDuplicateFiles.csproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
<None Remove="Images\about.png" />
1717
<None Remove="Images\close.png" />
1818
<None Remove="Images\delete.png" />
19+
<None Remove="Images\delete_bin.png" />
1920
<None Remove="Images\github.png" />
2021
<None Remove="Images\home_page.png" />
2122
<None Remove="Images\horizontal_line.png" />
2223
<None Remove="Images\icon.png" />
2324
<None Remove="Images\loader.png" />
2425
<None Remove="Images\maximize.png" />
26+
<None Remove="Images\multiple_choice.png" />
2527
<None Remove="Images\package_search.png" />
2628
<None Remove="Images\reset.png" />
2729
<None Remove="Images\restore.png" />
@@ -47,6 +49,9 @@
4749
<Resource Include="Images\delete.png">
4850
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
4951
</Resource>
52+
<Resource Include="Images\delete_bin.png">
53+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
54+
</Resource>
5055
<Resource Include="Images\github.png">
5156
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
5257
</Resource>
@@ -65,6 +70,9 @@
6570
<Resource Include="Images\maximize.png">
6671
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
6772
</Resource>
73+
<Resource Include="Images\multiple_choice.png">
74+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
75+
</Resource>
6876
<Resource Include="Images\package_search.png">
6977
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
7078
</Resource>
@@ -88,4 +96,8 @@
8896
</Resource>
8997
</ItemGroup>
9098

99+
<ItemGroup>
100+
<Folder Include="Model\" />
101+
</ItemGroup>
102+
91103
</Project>
718 Bytes
Loading
219 Bytes
Loading

src/FindDuplicateFiles/MainWindow.xaml

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6-
xmlns:local="clr-namespace:FindDuplicateFiles"
6+
xmlns:local="clr-namespace:FindDuplicateFiles"
77
mc:Ignorable="d"
88
Title="重复文件查找" Height="650" Width="1000" WindowStartupLocation="CenterScreen" Icon="/Images/icon.png"
99
MouseLeftButtonDown="Window_MouseLeftButtonDown">
10-
1110
<WindowChrome.WindowChrome>
1211
<WindowChrome UseAeroCaptionButtons="False" />
1312
</WindowChrome.WindowChrome>
@@ -78,7 +77,7 @@
7877
<PropertyGroupDescription PropertyName="Key"></PropertyGroupDescription>
7978
</CollectionViewSource.GroupDescriptions>
8079
</CollectionViewSource>
81-
80+
<BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
8281
</Window.Resources>
8382

8483
<Grid Background="{DynamicResource Background}">
@@ -236,13 +235,32 @@
236235
</Grid.ColumnDefinitions>
237236

238237
<Grid Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0" Grid.RowSpan="2">
238+
239239
<Border BorderBrush="{DynamicResource BlockBorder}" BorderThickness="1">
240240
<DockPanel>
241-
<StackPanel Height="0" DockPanel.Dock="Top" Name="PanelLoading" Orientation="Horizontal" Margin="5,8,5,8">
242-
<Image Margin="0,0,5,0" Name="ImgLoading" Width="24" Height="24" Source="/Images/loader.png" HorizontalAlignment="Center" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5"></Image>
243-
<Label Name="LblMessage" Foreground="{DynamicResource FontForeground}">准备查找....</Label>
241+
<!-- 重复文件列表功能区 -->
242+
<StackPanel Height="25" DockPanel.Dock="Top" Orientation="Horizontal" Margin="2,5,0,5">
243+
<Button Name="BtnChooseFile" Click="BtnChooseFile_Click" ToolTip="批量选中重复文件" Style="{StaticResource ChangeButtonIsMouseOver}" Background="Transparent">
244+
<Image Width="20" Height="20" Source="/Images/multiple_choice.png"></Image>
245+
</Button>
246+
<Button Name="BtnDeleteFile" Click="BtnDeleteFile_Click" ToolTip="删除选中文件" Style="{StaticResource ChangeButtonIsMouseOver}" Background="Transparent">
247+
<Image Width="20" Height="20" Source="/Images/delete_bin.png"></Image>
248+
</Button>
249+
250+
<!-- loading模块 -->
251+
<StackPanel Name="Tmp" Orientation="Horizontal" Margin="10,0,0,0" Visibility="{Binding IsShowLoading, Mode=OneWay, Converter={StaticResource BoolToVisConverter}}">
252+
<Image Name="ImgLoading" Width="20" Height="20" Source="/Images/loader.png" HorizontalAlignment="Center" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5"></Image>
253+
<Label Name="LblMessage" Foreground="{DynamicResource FontForeground}" Margin="0,0,0,-3">准备查找....</Label>
254+
</StackPanel>
244255
</StackPanel>
245-
<ListView Name="ListViewDuplicateFile" SelectionChanged="ListViewDuplicateFile_SelectionChanged" ItemsSource="{Binding Source={StaticResource DuplicateFiles}}" Background="{DynamicResource ListViewBackground}" BorderBrush="{DynamicResource BlockBorder}" BorderThickness="0" >
256+
<!-- 重复文件列表 -->
257+
<ListView
258+
Name="ListViewDuplicateFile"
259+
SelectionChanged="ListViewDuplicateFile_SelectionChanged"
260+
ItemsSource="{Binding Source={StaticResource DuplicateFiles}}"
261+
Background="{DynamicResource ListViewBackground}"
262+
BorderBrush="{DynamicResource BlockBorder}"
263+
BorderThickness="0" >
246264
<ListView.ItemContainerStyle>
247265
<Style TargetType="ListViewItem">
248266
<Setter Property="Foreground" Value="{DynamicResource FontForeground}"/>
@@ -260,6 +278,13 @@
260278
</ListView.ItemContainerStyle>
261279
<ListView.View>
262280
<GridView>
281+
<GridViewColumn>
282+
<GridViewColumn.CellTemplate>
283+
<DataTemplate>
284+
<CheckBox />
285+
</DataTemplate>
286+
</GridViewColumn.CellTemplate>
287+
</GridViewColumn>
263288
<GridViewColumn Width="230" Header="文件名" DisplayMemberBinding="{Binding Name}"></GridViewColumn>
264289
<GridViewColumn Width="150" Header="修改时间" DisplayMemberBinding="{Binding LastWriteTime, StringFormat=yyyy-MM-dd HH:mm:ss}"></GridViewColumn>
265290
<GridViewColumn Width="100" Header="大小" DisplayMemberBinding="{Binding Size}"></GridViewColumn>
@@ -294,8 +319,10 @@
294319
</DockPanel>
295320
</Border>
296321
</Grid>
297-
<Grid Name="GridImage" Grid.Column="1" Grid.Row="1" Background="{DynamicResource FontForeground}" Visibility="Hidden" Margin="0,0,0,18">
298-
<Image Name="ImgSelected"></Image>
322+
<Grid Name="GridImage" Grid.Column="1" Grid.Row="1" Background="{DynamicResource Title}" Visibility="Hidden" Margin="0,0,18,18">
323+
<Border BorderBrush="{DynamicResource BlockBorder}" BorderThickness="1">
324+
<Image Name="ImgSelected"></Image>
325+
</Border>
299326
</Grid>
300327
</Grid>
301328
</Grid>

src/FindDuplicateFiles/MainWindow.xaml.cs

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Collections.ObjectModel;
7+
using System.ComponentModel;
78
using System.IO;
89
using System.Linq;
910
using System.Windows;
@@ -13,6 +14,8 @@
1314
using System.Windows.Media;
1415
using System.Windows.Media.Animation;
1516
using System.Windows.Media.Imaging;
17+
using FindDuplicateFiles.Model;
18+
using FindDuplicateFiles.ViewModel;
1619

1720
namespace FindDuplicateFiles
1821
{
@@ -21,21 +24,13 @@ namespace FindDuplicateFiles
2124
/// </summary>
2225
public partial class MainWindow : Window
2326
{
24-
/// <summary>
25-
/// 已选取的要搜索的文件夹
26-
/// </summary>
27-
public ObservableCollection<string> SearchFolders { get; set; }
2827
/// <summary>
2928
/// 是否正在进行搜索
3029
/// </summary>
3130
private bool _isSearching;
32-
private readonly SearchFilesJob _searchFilesJob = new();
33-
34-
/// <summary>
35-
/// 重复文件集合
36-
/// </summary>
37-
public ObservableCollection<DuplicateFileInfo> DuplicateFiles { get; set; }
3831

32+
private readonly SearchFilesJob _searchFilesJob = new();
33+
private readonly MainWindowViewModel _myModel = new();
3934
public MainWindow()
4035
{
4136
InitializeComponent();
@@ -70,7 +65,7 @@ private void InitializeSearchCondition()
7065
ChkIgnoreSmallFile.IsChecked = false;
7166
RdoAllFile.IsChecked = true;
7267

73-
SearchFolders.Clear();
68+
_myModel.SearchFolders.Clear();
7469
}
7570
private void InitializeLoadingBlock()
7671
{
@@ -84,20 +79,15 @@ private void InitializeLoadingBlock()
8479
RotateTransform rt = new RotateTransform();
8580
ImgLoading.RenderTransform = rt;
8681
rt.BeginAnimation(RotateTransform.AngleProperty, da);
87-
PanelLoading.Height = 0;
88-
PanelLoading.Margin = new Thickness(0, 0, 0, 0);
8982
}
9083

9184
/// <summary>
9285
/// UI数据绑定
9386
/// </summary>
9487
private void BindingItemsSource()
9588
{
96-
SearchFolders = new ObservableCollection<string>();
97-
ListBoxSearchFolders.ItemsSource = SearchFolders;
98-
99-
DuplicateFiles = new ObservableCollection<DuplicateFileInfo>();
100-
ListViewDuplicateFile.ItemsSource = DuplicateFiles;
89+
ListBoxSearchFolders.ItemsSource = _myModel.SearchFolders;
90+
ListViewDuplicateFile.ItemsSource = _myModel.DuplicateFiles;
10191

10292
CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(ListViewDuplicateFile.ItemsSource);
10393
PropertyGroupDescription groupDescription = new PropertyGroupDescription("Key");
@@ -106,6 +96,7 @@ private void BindingItemsSource()
10696
RdoOnlyImageFile.ToolTip = $"仅支持:{GlobalArgs.AppConfig.ImageExtension}文件";
10797
RdoOnlyMediaFile.ToolTip = $"仅支持:{GlobalArgs.AppConfig.MediaExtension}文件";
10898
RdoOnlyDocumentFile.ToolTip = $"仅支持:{GlobalArgs.AppConfig.DocumentExtension}文件";
99+
DataContext = _myModel;
109100
}
110101

111102
/// <summary>
@@ -136,7 +127,7 @@ private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
136127
if (GridImage.Visibility == Visibility)
137128
{
138129
ImgSelected.Source = null;
139-
GridImage.Visibility = Visibility.Hidden;
130+
GridImage.Visibility = Visibility.Collapsed;
140131
}
141132
}
142133

@@ -174,12 +165,11 @@ private void BtnAddSearchFolder_Click(object sender, RoutedEventArgs e)
174165
return;
175166
}
176167

177-
if (SearchFolders.Contains(path))
168+
if (_myModel.SearchFolders.Contains(path))
178169
{
179170
return;
180171
}
181-
182-
SearchFolders.Add(path);
172+
_myModel.SearchFolders.Add(path);
183173
}
184174

185175
private void BtnRemoveSearchFolder_Click(object sender, RoutedEventArgs e)
@@ -190,7 +180,7 @@ private void BtnRemoveSearchFolder_Click(object sender, RoutedEventArgs e)
190180
return;
191181
}
192182

193-
SearchFolders.Remove(tag.ToString());
183+
_myModel.SearchFolders.Remove(tag.ToString());
194184
}
195185

196186
private void BtnSearch_Click(object sender, RoutedEventArgs e)
@@ -211,7 +201,7 @@ private void BtnSearch_Click(object sender, RoutedEventArgs e)
211201

212202
private void BeginSearch()
213203
{
214-
if (SearchFolders.Count == 0)
204+
if (_myModel.SearchFolders.Count == 0)
215205
{
216206
MessageBox.Show("请选择要查找的文件夹", "重复文件查找", MessageBoxButton.OK, MessageBoxImage.Stop);
217207
return;
@@ -265,10 +255,10 @@ private void BeginSearch()
265255
}
266256

267257
SetBeginSearchStyle();
268-
DuplicateFiles.Clear();
258+
_myModel.DuplicateFiles.Clear();
269259
var config = new SearchConfigs()
270260
{
271-
Folders = new List<string>(SearchFolders.ToList()),
261+
Folders = new List<string>(_myModel.SearchFolders.ToList()),
272262
SearchMatch = searchMatch,
273263
SearchOption = searchOption
274264
};
@@ -280,11 +270,10 @@ private void BeginSearch()
280270
/// </summary>
281271
private void SetBeginSearchStyle()
282272
{
283-
PanelLoading.Height = 25;
284-
PanelLoading.Margin = new Thickness(5, 8, 5, 8);
285273
TxtSearch.Text = "停止";
286274
ImgSearch.Source = new BitmapImage(new Uri("pack://application:,,,/Images/stop.png"));
287275
_isSearching = true;
276+
_myModel.IsShowLoading = _isSearching;
288277
}
289278

290279
private void EndSearch()
@@ -298,11 +287,10 @@ private void EndSearch()
298287
/// </summary>
299288
private void SetEndSearchStyle()
300289
{
301-
PanelLoading.Height = 0;
302-
PanelLoading.Margin = new Thickness(0, 0, 0, 0);
303290
TxtSearch.Text = "开始";
304291
ImgSearch.Source = new BitmapImage(new Uri("pack://application:,,,/Images/search.png"));
305292
_isSearching = false;
293+
_myModel.IsShowLoading = _isSearching;
306294
}
307295

308296
private void BtnReset_Click(object sender, RoutedEventArgs e)
@@ -354,7 +342,7 @@ private void DuplicateFilesFound(string key, SimpleFileInfo simpleFile)
354342
{
355343
Application.Current.Dispatcher.Invoke(() =>
356344
{
357-
DuplicateFiles.Add(new DuplicateFileInfo()
345+
_myModel.DuplicateFiles.Add(new DuplicateFileInfo()
358346
{
359347
Key = key,
360348
Name = simpleFile.Name,
@@ -397,5 +385,15 @@ private void BtnAbout_Click(object sender, RoutedEventArgs e)
397385
};
398386
about.Show();
399387
}
388+
389+
private void BtnChooseFile_Click(object sender, RoutedEventArgs e)
390+
{
391+
MessageBox.Show("抱歉,该功能暂不可用", "重复文件查找", MessageBoxButton.OK, MessageBoxImage.Information);
392+
}
393+
394+
private void BtnDeleteFile_Click(object sender, RoutedEventArgs e)
395+
{
396+
MessageBox.Show("抱歉,该功能暂不可用", "重复文件查找", MessageBoxButton.OK, MessageBoxImage.Information);
397+
}
400398
}
401399
}

src/FindDuplicateFiles/Common/AppConfigInfo.cs renamed to src/FindDuplicateFiles/Model/AppConfigInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace FindDuplicateFiles.Common
1+
namespace FindDuplicateFiles.Model
22
{
33
public class AppConfigInfo
44
{

src/FindDuplicateFiles/SearchFile/DuplicateFileInfo.cs renamed to src/FindDuplicateFiles/Model/DuplicateFileInfo.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace FindDuplicateFiles.SearchFile
1+
using FindDuplicateFiles.SearchFile;
2+
3+
namespace FindDuplicateFiles.Model
24
{
35
public class DuplicateFileInfo : SimpleFileInfo
46
{

src/FindDuplicateFiles/Common/SearchConfigs.cs renamed to src/FindDuplicateFiles/Model/SearchConfigs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33

4-
namespace FindDuplicateFiles.Common
4+
namespace FindDuplicateFiles.Model
55
{
66
public class SearchConfigs
77
{

0 commit comments

Comments
 (0)