예제 #1
0
        /// <summary>
        /// 改库
        /// </summary>
        private void ExecuteAddTabModifyLibraryCommand()
        {
            OpenFileDialog newDatabase = new OpenFileDialog();
            newDatabase.Title = "打开库文件";
            newDatabase.Filter = "库文件(*.lib)|*.lib|所有文件(*.*)|*.*";
            //初始化打开文件目录,但会造成以后每次打开的都是这个目录
            //newDatabase.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            //wpf不实现此属性
            //newDatabase.RestoreDirectory = true;
            if ((bool)newDatabase.ShowDialog())
            {
                try
                {
                    Config.DataBaseFile = newDatabase.FileName;

                    if (ValidateDataBase.Validate(Config.DataSource))
                    {
                        string tabName = string.Format("改库-{0}", Path.GetFileNameWithoutExtension(newDatabase.FileName));

                        if (IsTabExist(tabName))
                        {
                            this.SelectedTab = ItemCollection.FirstOrDefault(x => x.TabName == tabName);
                        }
                        else
                        {
                            var tab = new BuildLibraryViewModel(Config.DataBaseFile, tabName);

                            if (tab.FirstRecordCommand.CanExecute())
                            {
                                tab.FirstRecordCommand.Execute();
                            }
                            else
                            {
                                tab.QuestionInfo.QuestionNumber = "";
                                tab.InitialQuestionInfo();
                            }
                            _status.ShowStatus(string.Format("正在加载{0}", Config.DataBaseFile));

                            using (new AutoWaitCursor())
                            {
                                this.ItemCollection.Add(tab);
                                tab.IsChanged = false;
                            }
                            this.SelectedTab = tab;
                            _status.ShowStatus("就绪");
                        }
                    }
                    else
                    {
                        MessageBoxPlus.Show(App.Current.MainWindow, string.Format("文件“{0}”已加密或者它不是一个有效的库文件", Config.DataBaseFile), "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBoxPlus.Show(App.Current.MainWindow, ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    LogWritter.Log(ex, "改库错误");
                    _status.ShowStatus("文件解析失败");
                    return;
                }
            }
        }
예제 #2
0
        /// <summary>
        /// 建库
        /// </summary>
        private void ExecuteAddTabBulidLibraryCommand()
        {
            if (!IsFirstAdd)
            {
                IsBusy = true;
                DispatcherHelper.DoEvents();
            }
            try
            {
                string tabName = "建库";
                if (IsTabExist(tabName))
                {
                    this.SelectedTab = ItemCollection.FirstOrDefault(x => x.TabName == tabName);
                }
                else
                {
                    //Config.DataBaseFile = Path.Combine(System.Windows.Forms.Application.StartupPath, "MyData.db");
                    string startupPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
                    string dbFilePathWithName = Path.Combine(startupPath, "MyData.db");

                    //判断MyData.db是否存在
                    if (File.Exists(dbFilePathWithName))
                    {
                        Config.DataBaseFile = dbFilePathWithName;
                        if (ValidateDataBase.Validate(Config.DataSource))
                        {
                            var tab = new BuildLibraryViewModel(Config.DataBaseFile, tabName);
                            if (tab.LastRecordCommand.CanExecute())
                            {
                                IsBusy = false;
                                MessageBoxPlus.Show(App.Current.MainWindow, "检测到上一次程序没有正确关闭,保存的建库数据部分已恢复", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                                IsBusy = true;
                                tab.LastRecordCommand.Execute();
                            }
                            else
                            {
                                tab.QuestionInfo.QuestionNumber = "";
                                tab.InitialQuestionInfo();
                            }
                            BuildLibraryViewModel.isFinishButtonBeClicked = false;
                            _status.ShowStatus("正在加载...");
                            using (new AutoWaitCursor())
                            {
                                if (!IsFirstAdd)
                                {
                                    this.ItemCollection.Add(tab);
                                    this.ItemCollection.Remove(tab);
                                    this.ItemCollection.Add(tab);
                                }
                                else
                                {
                                    this.ItemCollection.Add(tab);
                                }
                            }
                            this.SelectedTab = tab;
                            _status.ShowStatus("就绪");
                            IsFirstAdd = true;
                            IsBusy = false;
                        }
                        else
                        {
                            IsFirstAdd = false;
                            IsBusy = false;
                            MessageBoxPlus.Show(App.Current.MainWindow, string.Format("文件“{0}”已加密或者它不是一个有效的库文件", Config.DataBaseFile), "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                            return;
                        }
                    }
                    else
                    {
                        IsFirstAdd = false;
                        IsBusy = false;
                        MessageBoxPlus.Show(App.Current.MainWindow, "应用程序文件“MyData.db”丢失", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                }
            }
            catch (Exception e)
            {
                IsFirstAdd = false;
                IsBusy = false;
                MessageBoxPlus.Show(App.Current.MainWindow, e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                LogWritter.Log(e, "建库错误");
                return;
            }
        }