private async void LoadFile(string path, bool initialLoad = false)
        {
            try
            {
                var json = await File.ReadAllTextAsync(path);

                var deserializedSnippets = JsonConvert.DeserializeObject <List <Snippet> >(json);
                SnippetsList.Clear();
                if (deserializedSnippets?.Any() ?? false)
                {
                    deserializedSnippets.ForEach(snippet => SnippetsList.Add(snippet));
                }
                AppSettings.CurrentFilePath = path;

                if (!initialLoad)
                {
                    AppSettings.AutoSaveEnabled = false;
                }
            }
            catch (Exception)
            {
                if (!initialLoad)
                {
                    await this.ShowChildWindowAsync(new MessageDialog { MessageBoxText = "Couldn't read file.", Caption = "Error" });
                }

                AppSettings.CurrentFilePath = null;
            }
        }
        private void AddNewClick(object sender, RoutedEventArgs e)
        {
            var editorWindow = new EditorWindow(new Snippet(), false);

            if (editorWindow.ShowDialog() == true)
            {
                SnippetsList.Add(editorWindow.Snippet);
            }
        }
예제 #3
0
 private void saveButton_Click(object sender, EventArgs e)
 {
     if (Snippets.SnippetExists(snippetList.Text))
     {
         var snip = Snippets.FirstOrDefault(s => s.Name.Equals(snippetList.Text, StringComparison.CurrentCultureIgnoreCase));
         snip.Content     = snippetContent.Text;
         snip.Description = snippetDescription.Text;
     }
     else
     {
         Snippets.Add(new Snippet(snippetList.Text, snippetDescription.Text, snippetContent.Text));
     }
     Snippets.Save();
     this.Close();
 }
        private void EditSnippet(object sender, MouseButtonEventArgs e)
        {
            var rowIndex = SnippetsDataGrid.SelectedIndex;

            if (rowIndex < 0)
            {
                return;
            }

            var snippet = rowIndex < SnippetsList.Count ? SnippetsList[rowIndex] : new Snippet();
            var isEdit  = rowIndex < SnippetsList.Count;

            var editorWindow = new EditorWindow(snippet, isEdit);

            if (editorWindow.ShowDialog() != true)
            {
                return;
            }

            if (editorWindow.IsSetToDelete)
            {
                SnippetsList.RemoveAt(rowIndex);
                Clipboard.Clear();
                return;
            }

            if (rowIndex < SnippetsList.Count)
            {
                SnippetsList[rowIndex].Name  = editorWindow.Snippet.Name;
                SnippetsList[rowIndex].Value = editorWindow.Snippet.Value;
            }
            else
            {
                SnippetsList.Add(editorWindow.Snippet);
            }

            Clipboard.SetDataObject($"{editorWindow.Snippet.Value}");
        }