黑马程序员学习日记(2)——文件批量重命名程序:One Click实现BLL层与UI层各个部分的交互

---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------


1.这里解释一下为什么需要定义抽象基类FileOperator,因为软件以后还会增加更多对文件进行操作的功能,而这些操作都有共同的特性:都需要打开文件目录、需要保存目录路径、需要显示帮助消息。因此定义一个提供实现了这些特性的基类就实现了代码的重用。


2.介绍数据的存储类型:嵌套类型ColorString,它在FileOperator中定义。

        /// <summary>
        /// 这个类用于创建带有颜色的文本
        /// </summary>
        public class ColorString : INotifyPropertyChanged
        {
            public enum TextColor
            {
                Red, Black, Orange
            }

            private string text;
            private TextColor color;
            private bool visibility;

            public delegate void TextHelper();
            public event TextHelper TextChanged;
            public event PropertyChangedEventHandler PropertyChanged;

            public TextColor Color //颜色属性
            { 
                get
                {
                    return color;
                }
                set
                {
                    color = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Color"));
                    }
                }
            } 
            /// <summary>
            /// 每次对Text修改都会触发事件,检查输入
            /// </summary>
            public string Text // 文本属性
            { 
                get { return text; } 
                set 
                {
                    text = value;
                    if (TextChanged != null)
                    {
                        TextChanged(); 
                    }
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Text"));
                    }
                }
            }     
            public bool Visibility //可见性    
            { 
                get
                {
                    return visibility;
                }
                set
                {
                    visibility = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Visibility"));
                    }
                }
            }         
        }

为了提供良好的用户体验,我们需要在用户输入错误时将输入变为红色,并在合适的时候设置帮助消息的可见性。


为此我们需要用比string更能适应软件需求的类型来保存数据,这就是ColorString。

所有的用户输入和帮助消息都将使用ColorString来保存

        private ColorString message;        // 保存帮助消息
        private ColorString directoryPath;  // 保存目录路径

        private ColorString customName;                  // 保存用户输入的自定义文件名
        private ColorString specifiedType;               // 保存用户输入的文件类型


3.使用单例模式创建Rename的实例,禁止外部定义Rename实例,确保安全(毕竟我们没有必要有1个以上的Rename实例)

    /// <summary>
    /// 这个类实现了批量重命名文件的功能
    /// </summary>
    public sealed class Rename : FileOperator
    {
        private static Rename rename = new Rename();

        public static Rename Default
        {
            get { return Rename.rename; }
            set { Rename.rename = value; }
        }

        private Rename()
        {
            customName = new ColorString() { Color = ColorString.TextColor.Black, Text = null, Visibility = true };
            specifiedType = new ColorString() { Color = ColorString.TextColor.Black, Text = null, Visibility = true };
        }
    }


4.现在开始介绍每个与UI层各个部分交互的功能的实现。首先是读取目录按钮的实现:这里使用的是CommonOpenFileDialog类,需要引用相关的程序集。*注意:这里请先关注 读取目录这个功能的实现,其他的细节(像如何检查文件夹是否为空和数据绑定)我会稍后叙述。

        private CommonOpenFileDialog cofd;
        public CommonOpenFileDialog Cofd
        {
            get
            {
                if (cofd == null)
                {
                    throw new ApplicationException("引用了值为null的CommonOpenFileDialog类型的属性。");
                }
                return cofd;
            }
            set { cofd = value; }
        }

        /// <summary>
        /// 使用CommonOpenFileDialog类打开包含要重命名文件的文件夹
        /// </summary>
        public void GetTheDirectory()
        {            
            // Add reference from Windows API code pack : 
            //     Microsoft.WindowsAPICodePack
            //     Microsoft.WindowsAPICodePack.Shell
            //
            // using Microsoft.WindowsAPICodePack.Dialogs;
            // Down Link : http://archive.msdn.microsoft.com/WindowsAPICodePack/Release/ProjectReleases.aspx?ReleaseId=4906#VoteBreakdown
            // 这些程序集的安装文件已下载到解决方案中,解压即可用

            using (Cofd = new CommonOpenFileDialog())
            {
                Cofd.IsFolderPicker = true;	// true: 文件夹, false: 文件
                Cofd.Title = "选择一个文件夹";                                                             // 标题
                Cofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); // 初始路径

                // 如果成功打开目录
                if (Cofd.ShowDialog() == CommonFileDialogResult.Ok) 
                {
                    // 为路径赋值
                    DirectoryPath.Text = Cofd.FileName;

                    // 检查目录是否为空
                    CheckTheDirectory();
                }  
            }
        }

        private void Btn_CommonOpenFileDialog1_Click(object sender, RoutedEventArgs e)
        {
            // 调用方法打开目录            
            Rename.Default.GetTheDirectory();                    
        }

5.存储用户输入的文件类型和文件名

        /// <summary>
        /// 用户输入的自定义文件名
        /// </summary>
        public ColorString CustomName
        {
            get { return customName; }
            set { customName = value; }
        }
        /// <summary>
        /// 用户指定的类型,每次赋值都会自动转换为小写,并进行拼写检查
        /// </summary>
        public ColorString SpecifiedType
        {
            get { return specifiedType; }
            set { specifiedType = value; }
        }


6.按下“一键完成”按钮,将调用Run()方法,开始执行任务。

        private void Btn_Run_Click(object sender, RoutedEventArgs e)
        {
            // 执行任务
            switch (TabControl_FileOperator.SelectedIndex)
            {
                case 0:
                    Rename.Default.Run();
                    break;
                default:
                    break;
            }
        }

下一篇文章将开始讲解数据的绑定。


---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------

详细请查看:<a href="http://www.itheima.com" target="blank">www.itheima.com</a>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值