From cb7608ed7be9acbc2dfed7f7589d53cbb07bc943 Mon Sep 17 00:00:00 2001 From: xiang Date: Sat, 11 Jan 2025 21:43:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0mvvm?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.xaml.cs | 2 +- Common/Tools/LogUtils.cs | 188 ++++++++++++++++++++++++++++++ Models/MainWindowModel.cs | 14 --- Models/VideoModel.cs | 112 ++++++++++++++++++ VideoConcat.csproj | 1 + VideoConcat.sln | 4 - ViewModels/MainWindowViewModel.cs | 21 ---- ViewModels/VideoViewModel.cs | 110 +++++++++++++++++ Views/MainWindow.xaml | 10 +- Views/MainWindow.xaml.cs | 8 +- Views/Video.xaml | 55 +++++++++ Views/Video.xaml.cs | 30 +++++ log4net.config | 12 +- 13 files changed, 518 insertions(+), 49 deletions(-) create mode 100644 Common/Tools/LogUtils.cs delete mode 100644 Models/MainWindowModel.cs create mode 100644 Models/VideoModel.cs delete mode 100644 ViewModels/MainWindowViewModel.cs create mode 100644 ViewModels/VideoViewModel.cs create mode 100644 Views/Video.xaml create mode 100644 Views/Video.xaml.cs diff --git a/App.xaml.cs b/App.xaml.cs index 3e80403..42f83eb 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -7,7 +7,7 @@ namespace VideoConcat /// /// Interaction logic for App.xaml /// - public partial class App : Application + public partial class App : System.Windows.Application { } diff --git a/Common/Tools/LogUtils.cs b/Common/Tools/LogUtils.cs new file mode 100644 index 0000000..54dbbe1 --- /dev/null +++ b/Common/Tools/LogUtils.cs @@ -0,0 +1,188 @@ +using log4net; +using log4net.Config; +using System.IO; + +namespace VideoConcat.Common.Tools +{ + class LogUtils + { + private static readonly ILog loginfo = LogManager.GetLogger("loginfo"); + + /// + /// 从缺省配置文件获取日志配置 + /// + public static void SetConfig() + { + XmlConfigurator.Configure(); + } + + /// + /// 从指定配置文件获取日志配置 + /// + /// 指定的配置文件 + public static void SetConfig(FileInfo configFile) + { + XmlConfigurator.Configure(configFile); + } + + /// + /// 生成分类日志 + /// + /// 日志信息 + /// 保存目录名,形如d:\log\aaa + private static void WriteSortLog(string info, string dirName) + { + try + { + if (false == Directory.Exists(dirName)) + { + Directory.CreateDirectory(dirName); + } + string path = dirName + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".log"; + StreamWriter sw = new(path, true, System.Text.Encoding.Default); + sw.WriteLine(DateTime.Now.ToString("HH:mm:ss: ") + info); + sw.Close(); + } + catch (Exception ex) + { + string expMsg = "WriteSortLog异常:" + ex.Message + Environment.NewLine + ex.StackTrace; + + if (ex.InnerException != null) + expMsg += Environment.NewLine + "InnerException:" + ex.InnerException.Message; + + Error(expMsg, ex); + } + } + + /// + /// Info级 常规日志 + /// + /// 日志信息 + public static void Info(string info) + { + if (loginfo.IsInfoEnabled) + { + loginfo.Info(info); + } + } + + /// + /// Info 先生成常规日志,然后在指定目录另外创建一份日志 + /// 主要用来需要对日志进行分类时使用 + /// + /// + /// + public static void Info(string info, string dirName) + { + if (loginfo.IsInfoEnabled) + { + //生成常规日志 + loginfo.Info(info); + + //生成分类日志 + WriteSortLog(info, dirName); + } + } + + /// + /// Debug级 常规日志 + /// + /// 日志信息 + public static void Debug(string info) + { + if (loginfo.IsDebugEnabled) + { + loginfo.Debug(info); + } + } + + /// + /// Debug级 异常日志 + /// + /// 日志信息 + /// 异常信息 + public static void Debug(string info, Exception exp) + { + if (loginfo.IsDebugEnabled) + { + loginfo.Debug(info, exp); + } + } + + /// + /// Error级 常规的日志 + /// + /// 日志信息 + public static void Error(string info) + { + if (loginfo.IsErrorEnabled) + { + loginfo.Error(info); + } + } + + /// + /// Error 异常日志 + /// + /// 日志信息 + /// 异常信息 + public static void Error(string info, Exception exp) + { + if (loginfo.IsErrorEnabled) + { + loginfo.Error(info, exp); + } + } + + /// + /// Fatal级 常规日志 + /// + /// 日志信息 + public static void Fatal(string info) + { + if (loginfo.IsFatalEnabled) + { + loginfo.Fatal(info); + } + } + + /// + /// Fatal级 异常日志 + /// + /// 日志信息 + /// 异常信息 + public static void Fatal(string info, Exception exp) + { + if (loginfo.IsFatalEnabled) + { + loginfo.Fatal(info, exp); + } + } + + /// + /// Warn级 常规日志 + /// + /// 日志信息 + public static void Warn(string info) + { + if (loginfo.IsWarnEnabled) + { + loginfo.Warn(info); + } + } + + /// + /// Warn级 异常日志 + /// + /// 日志 + /// 异常信息 + public static void Warn(string info, Exception exp) + { + if (loginfo.IsWarnEnabled) + { + loginfo.Warn(info, exp); + } + } + } + +} diff --git a/Models/MainWindowModel.cs b/Models/MainWindowModel.cs deleted file mode 100644 index b3a10d6..0000000 --- a/Models/MainWindowModel.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace VideoConcat.Models -{ - internal class MainWindowModel - { - public string? UserName { get; set; } - public string? PassWord { get; set; } - } -} diff --git a/Models/VideoModel.cs b/Models/VideoModel.cs new file mode 100644 index 0000000..f1caaf2 --- /dev/null +++ b/Models/VideoModel.cs @@ -0,0 +1,112 @@ +using Standard; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Collections.Specialized; +using System.ComponentModel; +using System.IO; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Input; +using System.Xml.Linq; +using static System.Windows.Forms.VisualStyles.VisualStyleElement.Header; + +namespace VideoConcat.Models +{ + public class VideoModel : INotifyPropertyChanged + { + private int _num; + private string _folderPath = ""; + private bool _canStart = false; + private ObservableCollection _FolderInfos = []; + + public int Num + { + get { return _num; } + set + { + _num = value; + OnPropertyChanged(); + } + } + + public struct FolderInfo + { + public DirectoryInfo DirectoryInfo{set;get;} + public int Num { set; get; } + } + + public ObservableCollection FolderInfos + { + get { return _FolderInfos; } + set + { + if (_FolderInfos != null) + { + _FolderInfos.CollectionChanged -= ItemList_CollectionChanged; + } + _FolderInfos = value; + if (_FolderInfos != null) + { + _FolderInfos.CollectionChanged += ItemList_CollectionChanged; + } + OnPropertyChanged(); + UpdateSum(); + } + } + + public string FolderPath + { + get { return _folderPath; } + set + { + _folderPath = value; + OnPropertyChanged(nameof(FolderPath)); + } + } + + public bool CanStart + { + get { return _canStart; } + set + { + _canStart = value; + OnPropertyChanged(nameof(CanStart)); + } + } + + public ICommand? BtnOpenFolderCommand { get; set; } + public ICommand? BtnStartVideoConcatCommand { get; set; } + + public event PropertyChangedEventHandler? PropertyChanged; + + + protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + + private void ItemList_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) + { + UpdateSum(); + } + + public void UpdateSum() + { + int _temp = 1; + foreach (var item in FolderInfos) + { + if (item.Num > 0) + { + _temp *= item.Num; + } + } + Num = _temp; + if(Num > 0){ + CanStart = true; + } + } + } +} diff --git a/VideoConcat.csproj b/VideoConcat.csproj index 0bfa5dd..a35f01c 100644 --- a/VideoConcat.csproj +++ b/VideoConcat.csproj @@ -7,6 +7,7 @@ enable true true + true diff --git a/VideoConcat.sln b/VideoConcat.sln index 6f6e4ad..5093c67 100644 --- a/VideoConcat.sln +++ b/VideoConcat.sln @@ -5,8 +5,6 @@ VisualStudioVersion = 17.11.35327.3 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VideoConcat", "VideoConcat.csproj", "{2FF5691C-3184-4B68-944B-C704E64C4E4E}" EndProject -Project("{54435603-DBB4-11D2-8724-00A0C9A8B90C}") = "Setup", "..\Setup\Setup.vdproj", "{7EE4FDA6-076F-494D-89C5-B3735A89875F}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -17,8 +15,6 @@ Global {2FF5691C-3184-4B68-944B-C704E64C4E4E}.Debug|Any CPU.Build.0 = Debug|Any CPU {2FF5691C-3184-4B68-944B-C704E64C4E4E}.Release|Any CPU.ActiveCfg = Release|Any CPU {2FF5691C-3184-4B68-944B-C704E64C4E4E}.Release|Any CPU.Build.0 = Release|Any CPU - {7EE4FDA6-076F-494D-89C5-B3735A89875F}.Debug|Any CPU.ActiveCfg = Debug - {7EE4FDA6-076F-494D-89C5-B3735A89875F}.Release|Any CPU.ActiveCfg = Release EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ViewModels/MainWindowViewModel.cs b/ViewModels/MainWindowViewModel.cs deleted file mode 100644 index 3dacd12..0000000 --- a/ViewModels/MainWindowViewModel.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using VideoConcat.Models; - -namespace VideoConcat.ViewModels -{ - internal class MainWindowViewModel - { - - public MainWindowModel MainWindowsModel { get; set; } = new MainWindowModel(); - - public void Login() - { - string userName = MainWindowsModel.UserName; - string passWord = MainWindowsModel.PassWord; - } - } -} diff --git a/ViewModels/VideoViewModel.cs b/ViewModels/VideoViewModel.cs new file mode 100644 index 0000000..68c9434 --- /dev/null +++ b/ViewModels/VideoViewModel.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Input; +using VideoConcat.Models; +using System.Windows; +using System.Windows.Forms; +using MessageBox = System.Windows.MessageBox; +using System.ComponentModel; +using VideoConcat.Common.Tools; +using System.IO; +using static VideoConcat.Models.VideoModel; + +namespace VideoConcat.ViewModels +{ + public class VideoViewModel + { + private VideoModel _videoModel; + public VideoModel VideoModel + { + get { return _videoModel; } + set + { + _videoModel = value; + } + } + + + + public VideoViewModel() + { + VideoModel = new VideoModel(); + VideoModel.BtnOpenFolderCommand = new BtnOpenFolderCommand() + { + DoExcue = obj => + { + FolderBrowserDialog folderBrowserDialog = new(); + if (folderBrowserDialog.ShowDialog() == DialogResult.OK) + { + VideoModel.FolderPath = folderBrowserDialog.SelectedPath; + LogUtils.Info($"获取视频文件夹,视频路径:{VideoModel.FolderPath}"); + ListFolder(VideoModel.FolderPath); + } + } + }; + + VideoModel.BtnStartVideoConcatCommand = new BtnStartVideoConcatCommand() + { + DoExcue = obj => + { + LogUtils.Info("开始合并视频"); + } + }; + } + + private void ListFolder(string path) + { + DirectoryInfo dir = new(path); + try + { + DirectoryInfo dirD = dir as DirectoryInfo; + DirectoryInfo[] folders = dirD.GetDirectories(); + VideoModel.FolderInfos.Clear(); + //获取文件夹下所有视频文件 + foreach (DirectoryInfo Folder in folders) + { + string[] files = Directory.GetFiles(Folder.FullName, "*.mp4"); + LogUtils.Info($"{Folder.Name}下有{files.Length}个视频文件"); + + VideoModel.FolderInfos.Add(new FolderInfo { DirectoryInfo = Folder, Num = files.Length }); + } + VideoModel.UpdateSum(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message); + return; + } + } + } + + class BtnOpenFolderCommand : ICommand + { + public event EventHandler? CanExecuteChanged; + + public bool CanExecute(object? parameter) => true; + + public void Execute(object? parameter) + { + DoExcue?.Invoke(parameter); + } + + public Action? DoExcue { get; set; } + } + class BtnStartVideoConcatCommand : ICommand + { + public event EventHandler? CanExecuteChanged; + + public bool CanExecute(object? parameter) => true; + + public void Execute(object? parameter) + { + DoExcue?.Invoke(parameter); + } + + public Action? DoExcue { get; set; } + } +} diff --git a/Views/MainWindow.xaml b/Views/MainWindow.xaml index 2d89ce7..fde2c10 100644 --- a/Views/MainWindow.xaml +++ b/Views/MainWindow.xaml @@ -33,7 +33,7 @@ - - + x:Name="Password" + /> +