private void BtnImportOnClick(object sender, RoutedEventArgs e)
        {
            var btn      = sender as Button;
            var bookInfo = btn.DataContext as BookInfo;

            ConfigSevice.SaveBook(ref bookInfo);
            btn.IsEnabled = false;
        }
示例#2
0
        private void GetChapters()
        {
            _chapters.Clear();
            if (!File.Exists(_file))
            {
                _autoLoad = false;
                return;
            }
            var name = FileHelper.GetFileNameNoneExt(_file);

            if (!_file.EndsWith(ConfigSevice.BookExt))
            {
                var tmpFile = ConfigSevice.BookDir + name + ConfigSevice.BookExt;
                FileHelper.FileToUTF8(_file, tmpFile);
                _file = tmpFile;
            }
            var rgx = new Regex(_txtRegex);

            using (StreamReader sr = File.OpenText(_file))
            {
                string s;
                int    lineNum = 1;
                while ((s = sr.ReadLine()) != null)
                {
                    if (rgx.IsMatch(s))
                    {
                        var count = _chapters.Count;
                        if (count <= 0 || (lineNum - _chapters[count - 1].LineNum) > 5)
                        {
                            if (count <= 0 && lineNum > 5)
                            {
                                _chapters.Add(new ChaptersInfo
                                {
                                    Content = "序",
                                    LineNum = 0
                                });
                            }
                            _chapters.Add(new ChaptersInfo
                            {
                                Content = s,
                                LineNum = lineNum
                            });
                        }
                    }
                    lineNum++;
                }
            }
            if (_chapters.Count <= 0 && !_regexChanged)
            {
                ReloadBook();
                return;
            }
            Dispatcher.Invoke(() =>
            {
                LsCatalog.ItemsSource = _chapters;
                if (_chapters.Count > 0)
                {
                    if (_autoLoad)
                    {
                        LsCatalog.SelectedIndex = _bookInfo.ChapterIndex < 0 ? 0 : _bookInfo.ChapterIndex;
                    }
                    else
                    {
                        _bookInfo = new BookInfo
                        {
                            BookName = name,
                            FilePath = _file
                        };
                        ConfigSevice.SaveBook(ref _bookInfo);
                        LsCatalog.SelectedIndex = 0;
                    }
                    TbBookName.Text = string.Format("《{0}》", _bookInfo.BookName);
                }
            });
        }