private NotebookList.Notebook GetSelectedNotebook()
        {
            NotebookList.Notebook notebook = null;

            this.list.Dispatcher.Invoke(() =>
            {
                notebook = this.list.SelectedItem as NotebookList.Notebook;
            });

            return(notebook);
        }
        /// <summary>
        /// 列表选中项改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ListSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (prevSelectedItems != null)
            {
                SaveNotebookContent(prevSelectedItems, this.text.Text, false);
            }

            NotebookList.Notebook notebook = GetSelectedNotebook();

            if (notebook != null)
            {
                LoadNotebookContent(notebook.id);
            }

            prevSelectedItems = notebook;
        }
        /// <summary>
        /// 自动保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void AutoSave(object sender, System.Timers.ElapsedEventArgs e)
        {
            NotebookList.Notebook notebook = GetSelectedNotebook();
            string text = "";

            this.text.Dispatcher.Invoke(() =>
            {
                text = this.text.Text;
            });

            if (notebook != null && text != "")
            {
                SaveNotebookContent(notebook, text);
            }

            // 自动保存,消息提示
            SetTips("自动保存成功", 3000);
        }
        private void Text_KeyDown(object sender, KeyEventArgs e)
        {
            // ctrl+s
            if (Keyboard.IsKeyDown(Key.LeftCtrl) && Keyboard.IsKeyDown(Key.S))
            {
                NotebookList.Notebook notebook = GetSelectedNotebook();
                string text = "";

                this.text.Dispatcher.Invoke(() =>
                {
                    text = this.text.Text;
                });

                if (notebook != null && text != "")
                {
                    SaveNotebookContent(notebook, text);
                }
            }
        }
        /// <summary>
        /// 删除日记本
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteClick(object sender, RoutedEventArgs e)
        {
            NotebookList.Notebook notebook = GetSelectedNotebook();

            IDictionary <string, string> parameters = Request.getParameters();

            parameters.Add("id", notebook.id.ToString());

            Result result = JsonConvert.DeserializeObject <Result>(Http.Send(Request.baseUrl + Request.DeleteNotebook, "POST", parameters));

            if (result.code == 1000)
            {
                prevSelectedItems = null;
                this.list.Items.Remove(notebook);

                // 减去1的原因是为了方便后续的分页加载,防止加载断层
                start--;
            }
        }
        /// <summary>
        /// 修改标题
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ModifyClick(object sender, RoutedEventArgs e)
        {
            NotebookList.Notebook notebook = GetSelectedNotebook();

            Input  input   = new Input();
            string content = "";

            input.input.Text = notebook.title;

            input.confirm.Click += (object _sender, RoutedEventArgs _e) =>
            {
                content = input.input.Text;

                if (content.Length <= 0)
                {
                    MessageBox.Show("请输入标题");
                }
                else
                {
                    IDictionary <string, string> parameters = Request.getParameters();
                    parameters.Add("id", notebook.id.ToString());
                    parameters.Add("title", notebook.title);

                    Result result = JsonConvert.DeserializeObject <Result>(Http.Send(Request.baseUrl + Request.ModifyTitleNotebook, "POST", parameters));

                    if (result.code == 1000)
                    {
                        // 更新名字
                        notebook.title = content;
                        this.list.Items.Refresh();
                        input.Close();
                    }
                }
            };

            input.ShowDialog();
        }
        /// <summary>
        /// 保存日记本内容
        /// </summary>
        /// <param name="notebook"></param>
        /// <param name="content"></param>
        private void SaveNotebookContent(NotebookList.Notebook notebook, string content, bool async = true)
        {
            long updated_at = Int64.Parse(Helper.GetTimeStamp());

            if (notebook is null || content == "" || !saveLock)
            {
                return;
            }

            saveLock = true;

            IDictionary <string, string> parameters = Request.getParameters();

            parameters.Add("id", notebook.id.ToString());
            parameters.Add("title", notebook.title);
            parameters.Add("content", content);
            parameters.Add("updated_at", (updated_at / 1000).ToString());

            void callback(string responseText)
            {
                saveLock = false;

                if (responseText == "")
                {
                    return;
                }

                UpdateResult result = JsonConvert.DeserializeObject <UpdateResult>(responseText);

                if (result.code == 1000)
                {
                    this.list.Dispatcher.Invoke(() =>
                    {
                        notebook.updated_at = result.data.updated_at;
                    });

                    this.SetTips("保存成功", 3000);
                }
                else if (result.code == 1003)   // 文件有冲突
                {
                    this.list.Dispatcher.Invoke(() =>
                    {
                        isEnd = false;
                        RewindLoadNotebookPage();
                    });
                }
            }

            string tempResult = "";

            if (async)
#pragma warning disable CS4014 // 由于此调用不会等待,因此在调用完成前将继续执行当前方法
            {
                Http.SendAsync(Request.baseUrl + Request.SaveNotebook, "POST", parameters, callback);
            }
            else
            {
                tempResult = Http.Send(Request.baseUrl + Request.SaveNotebook, "POST", parameters);
                callback(tempResult);
            }
        }