Advertisement
eduardogr

Untitled

Nov 5th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.79 KB | None | 0 0
  1. Code-Behind
  2.  
  3. using Microsoft.Graphics.Canvas.Text;
  4. using System;
  5. using System.Collections;
  6. using System.Diagnostics;
  7. using Windows.Media.SpeechRecognition;
  8. using Windows.UI.Popups;
  9. using Windows.UI.Text;
  10. using Windows.UI.Xaml;
  11. using Windows.UI.Xaml.Controls;
  12. using Windows.UI.Xaml.Media;
  13.  
  14. // The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238
  15.  
  16. namespace uwpEvernote.View {
  17.     /// <summary>
  18.     /// An empty page that can be used on its own or navigated to within a Frame.
  19.     /// </summary>
  20.     public sealed partial class NotesPage: Page {
  21.         public NotesPage() {
  22.             this.InitializeComponent();
  23.  
  24.             var fonts = CanvasTextFormat.GetSystemFontFamilies();
  25.             fontBox.ItemsSource = fonts;
  26.             var arrList = new ArrayList();
  27.             for (int i = 0; i < 73; ++i) {
  28.                 arrList.Add(i);
  29.             }
  30.             fontSizeBox.ItemsSource = arrList;
  31.         }
  32.  
  33.         private void Cotent_TextChanged(object sender, RoutedEventArgs e) {
  34.  
  35.             richEbitBox.Document.GetText(TextGetOptions.None, out string value);
  36.             charactersCount.Text = (value.Length - 1).ToString();
  37.         }
  38.  
  39.         private async void Actions_Click(object sender, RoutedEventArgs e) {
  40.  
  41.             var id = sender as Button;
  42.  
  43.             switch (id.Tag) {
  44.  
  45.                 case "0":
  46.                     using (SpeechRecognizer recognizer = new SpeechRecognizer()) {
  47.                         await recognizer.CompileConstraintsAsync();
  48.                         var result = await recognizer.RecognizeWithUIAsync();
  49.                         var dialog = new MessageDialog(result.Text, "Text spoken");
  50.                         await dialog.ShowAsync();
  51.  
  52.                         richEbitBox.Document.GetText(TextGetOptions.None, out string value);
  53.                         richEbitBox.Document.SetText(TextSetOptions.None, value += result.Text);
  54.                     }
  55.                     break;
  56.                 case "1":
  57.                     if (richEbitBox.Document.Selection.CharacterFormat.Bold == FormatEffect.On) {
  58.                         richEbitBox.Document.Selection.CharacterFormat.Bold = FormatEffect.Off;
  59.                         FormatBoltText.Background = (SolidColorBrush)Resources["disabled"];
  60.                     } else {
  61.                         richEbitBox.Document.Selection.CharacterFormat.Bold = FormatEffect.On;
  62.                         FormatBoltText.Background = (SolidColorBrush)Resources["enabled"];
  63.                     }
  64.                     break;
  65.                 case "2":
  66.                     if (richEbitBox.Document.Selection.CharacterFormat.Italic == FormatEffect.On) {
  67.                         richEbitBox.Document.Selection.CharacterFormat.Italic = FormatEffect.Off;
  68.                         formatItalicText.Background = (SolidColorBrush)Resources["disabled"];
  69.                     } else {
  70.                         richEbitBox.Document.Selection.CharacterFormat.Italic = FormatEffect.On;
  71.                         formatItalicText.Background = (SolidColorBrush)Resources["enabled"];
  72.                     }
  73.                     break;
  74.                 case "3":
  75.                     if (richEbitBox.Document.Selection.CharacterFormat.Underline == UnderlineType.Single) {
  76.                         richEbitBox.Document.Selection.CharacterFormat.Underline = UnderlineType.None;
  77.                         formatUnderlineText.Background = (SolidColorBrush)Resources["disabled"];
  78.                     } else {
  79.                         richEbitBox.Document.Selection.CharacterFormat.Underline = UnderlineType.Single;
  80.                         formatUnderlineText.Background = (SolidColorBrush)Resources["enabled"];
  81.                     }
  82.                     break;
  83.                 default:
  84.                     break;
  85.             }
  86.         }
  87.  
  88.         private void ComboChanged(object sender, SelectionChangedEventArgs e) {
  89.  
  90.             var id = sender as ComboBox;
  91.  
  92.             switch (id.Tag) {
  93.  
  94.                 case "1":
  95.                     //Todo implement new font
  96.                     string fontName = id.SelectedItem.ToString();
  97.                     richEbitBox.Focus(FocusState.Pointer);
  98.                     richEbitBox.Document.Selection.SetRange(0, richEbitBox.Document.Selection.EndPosition);
  99.                     richEbitBox.Document.Selection.CharacterFormat.Name = fontName;
  100.                     break;
  101.                 case "2":
  102.                     var size = (float)id.SelectedItem;
  103.                     richEbitBox.Focus(FocusState.Pointer);
  104.                     richEbitBox.Document.Selection.SetRange(0, richEbitBox.Document.Selection.EndPosition);
  105.                     richEbitBox.Document.Selection.CharacterFormat.Size = size;
  106.                     break;
  107.                 default:
  108.                     break;
  109.             }
  110.         }
  111.  
  112.         private void Container_Loaded(object sender, RoutedEventArgs e) {
  113.             fontBox.Text = richEbitBox.Document.GetDefaultCharacterFormat().Name;
  114.             fontSizeBox.Text = richEbitBox.Document.GetDefaultCharacterFormat().Size.ToString();
  115.         }
  116.     }
  117. }
  118.  
  119. XAML
  120.  
  121. <Page x:Name="Main"
  122.       x:Class="uwpEvernote.View.NotesPage"
  123.       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  124.       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  125.       xmlns:local="using:uwpEvernote.View"
  126.       xmlns:vm="using:uwpEvernote.ViewModel"
  127.       xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  128.       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  129.       mc:Ignorable="d"
  130.       Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
  131.  
  132.     <Page.Resources>
  133.         <vm:NotesVM x:Key="vm" />
  134.         <SolidColorBrush x:Key="enabled"
  135.                          Color="#0078D4" />
  136.         <SolidColorBrush x:Key="disabled"
  137.                          Color="Transparent" />
  138.     </Page.Resources>
  139.  
  140.     <RelativePanel x:Name="Container"
  141.                    DataContext="{StaticResource vm}"
  142.                    Background="{ThemeResource SystemChromeLowColor}"
  143.                    Loaded="Container_Loaded">
  144.         <MenuBar x:Name="menuBar">
  145.             <MenuBarItem Title="File">
  146.                 <MenuFlyoutItem Text="New notebook"
  147.                                 Command="{Binding NewNotebookCommand}">
  148.                     <MenuFlyoutItem.Icon>
  149.                         <FontIcon Glyph="&#xE82D;" />
  150.                     </MenuFlyoutItem.Icon>
  151.                 </MenuFlyoutItem>
  152.                 <MenuFlyoutItem Text="New Note"
  153.                                 Command="{Binding NewNoteCommand}"
  154.                                 CommandParameter="{Binding SelectedNotebook}">
  155.                     <MenuFlyoutItem.Icon>
  156.                         <FontIcon Glyph="&#xE70B;" />
  157.                     </MenuFlyoutItem.Icon>
  158.                 </MenuFlyoutItem>
  159.                 <MenuFlyoutSeparator />
  160.                 <MenuFlyoutItem Text="Exit"
  161.                                 Command="{Binding ExitCommand}">
  162.                     <MenuFlyoutItem.Icon>
  163.                         <FontIcon Glyph="&#xE106;" />
  164.                     </MenuFlyoutItem.Icon>
  165.                 </MenuFlyoutItem>
  166.             </MenuBarItem>
  167.  
  168.         </MenuBar>
  169.         <ListView x:Name="Notebook"
  170.                   RelativePanel.Below="menuBar"
  171.                   Width="140"
  172.                   SelectedItem="{Binding SelectedNotebook, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
  173.                   ItemsSource="{Binding NoteBooks}"
  174.                   RelativePanel.AlignBottomWithPanel="True">
  175.             <ListView.ItemTemplate>
  176.                 <DataTemplate>
  177.                     <TextBlock Text="{Binding Name}" />
  178.                 </DataTemplate>
  179.             </ListView.ItemTemplate>
  180.         </ListView>
  181.         <ListView x:Name="Notes"
  182.                   Width="140"
  183.                   ItemsSource="{Binding Notes}"
  184.                   RelativePanel.Below="menuBar"
  185.                   RelativePanel.RightOf="Notebook"
  186.                   RelativePanel.AlignBottomWithPanel="True">
  187.             <ListView.ItemTemplate>
  188.                 <DataTemplate>
  189.                     <TextBlock Text="{Binding Title}" />
  190.                 </DataTemplate>
  191.             </ListView.ItemTemplate>
  192.         </ListView>
  193.         <CommandBar x:Name="commandBar"
  194.                     RelativePanel.Below="menuBar"
  195.                     RelativePanel.RightOf="Notes"
  196.                     RelativePanel.AlignRightWithPanel="True"
  197.                     OverflowButtonVisibility="Collapsed"
  198.                     VerticalAlignment="Center"
  199.                     Margin="0,10,20,10"
  200.                     Background="{ThemeResource  SystemChromeLowColor}">
  201.             <CommandBar.Content>
  202.                 <StackPanel Orientation="Horizontal">
  203.                     <AppBarButton x:Name="textToSpeech"
  204.                                   Icon="Microphone"
  205.                                   Click="Actions_Click"
  206.                                   Tag="0"
  207.                                   ToolTipService.ToolTip="Text to speech" />
  208.                     <AppBarButton x:Name="FormatBoltText"
  209.                                   ToolTipService.ToolTip="Bold"
  210.                                   Icon="Bold"
  211.                                   Tag="1"
  212.                                   Click="Actions_Click" />
  213.                     <AppBarButton x:Name="formatItalicText"
  214.                                   ToolTipService.ToolTip="Italic"
  215.                                   Icon="Italic"
  216.                                   Tag="2"
  217.                                   Click="Actions_Click" />
  218.                     <AppBarButton x:Name="formatUnderlineText"
  219.                                   ToolTipService.ToolTip="Underline"
  220.                                   Icon="Underline"
  221.                                   Tag="3"
  222.                                   Click="Actions_Click" />
  223.                     <ComboBox IsEditable="True"
  224.                               Tag="1"
  225.                               x:Name="fontBox"
  226.                               SelectionChanged="ComboChanged"
  227.                               Width="150" />
  228.                     <ComboBox x:Name="fontSizeBox"
  229.                               Tag="2"
  230.                               SelectionChanged="ComboChanged"
  231.                               IsEditable="True"
  232.                               Margin="10,0,0,0"
  233.                                />
  234.                 </StackPanel>
  235.  
  236.             </CommandBar.Content>
  237.  
  238.         </CommandBar>
  239.         <RichEditBox x:Name="richEbitBox"
  240.                      TextChanged="Cotent_TextChanged"
  241.                      RelativePanel.RightOf="Notes"
  242.                      RelativePanel.Below="commandBar"
  243.                      RelativePanel.AlignRightWithPanel="True"
  244.                      RelativePanel.AlignBottomWith="Notebook"
  245.                      Margin="0,0,10,40" />
  246.         <CommandBar Background="{ThemeResource  SystemChromeLowColor}"
  247.                     RelativePanel.RightOf="Notes"
  248.                     RelativePanel.AlignBottomWith="richEbitBox"
  249.                     RelativePanel.AlignRightWithPanel="True"
  250.                     Margin="0,0,10,0"
  251.                     HorizontalAlignment="Left"
  252.                     VerticalAlignment="Center">
  253.             <CommandBar.Content>
  254.                 <StackPanel Orientation="Horizontal"
  255.                             HorizontalAlignment="Center"
  256.                             VerticalAlignment="Center"
  257.                             Margin="0,10,0,0">
  258.                     <TextBlock Text="Count"
  259.                                VerticalAlignment="Center"
  260.                                HorizontalAlignment="Center" />
  261.                     <TextBlock Text="|"
  262.                                Margin="10,0,0,0" />
  263.                     <TextBlock x:Name="charactersCount"
  264.                                Margin="10,0,0,0" />
  265.                 </StackPanel>
  266.             </CommandBar.Content>
  267.         </CommandBar>
  268.     </RelativePanel>
  269. </Page>
  270.  
  271. NotesVM
  272.  
  273. using SQLite;
  274. using System;
  275. using System.Collections.ObjectModel;
  276. using System.ComponentModel;
  277. using System.Data.SqlClient;
  278.  
  279. using uwpEvernote.Model;
  280. using uwpEvernote.ViewModel.Commands;
  281.  
  282. namespace uwpEvernote.ViewModel {
  283.     public class NotesVM : INotifyPropertyChanged {
  284.  
  285.         public ObservableCollection<NoteBook> NoteBooks { get; set; }
  286.  
  287.         private NoteBook _SelectedNotebook;
  288.  
  289.         public NoteBook SelectedNotebook {
  290.             get { return _SelectedNotebook; }
  291.             set {
  292.                 if (value != _SelectedNotebook) {
  293.                     _SelectedNotebook = value;
  294.                     //OnPropertyChanged("SelectedNotebook");
  295.                 }
  296.             }
  297.         }
  298.  
  299.         public ObservableCollection<Note> Notes { get; set; }
  300.         public NewNoteCommand NewNoteCommand { get; set; }
  301.         public NewNotebookCommand NewNotebookCommand { get; set; }
  302.         public ExitCommand ExitCommand { get; set; }
  303.  
  304.  
  305.         public NotesVM() {
  306.  
  307.             NewNoteCommand = new NewNoteCommand(this);
  308.             NewNotebookCommand = new NewNotebookCommand(this);
  309.             ExitCommand = new ExitCommand(this);
  310.             NoteBooks = new ObservableCollection<NoteBook>();
  311.             Notes = new ObservableCollection<Note>();
  312.  
  313.             ReadNotebooks();
  314.         }
  315.  
  316.         public void CreateNotebook() {
  317.  
  318.             var newNotebook = new NoteBook() {
  319.                 Name = "New notebook"
  320.             };
  321.  
  322.             DatabaseHelper.Insert(newNotebook);
  323.         }
  324.  
  325.         public void CreateNote(int id) {
  326.  
  327.             var newNote = new Note() {
  328.                 NotebookId = id,
  329.                 CratedTime = DateTime.Now,
  330.                 UpdatedTime = DateTime.Now,
  331.                 Title = "New note"
  332.             };
  333.  
  334.             DatabaseHelper.Insert(newNote);
  335.         }
  336.  
  337.         public void ReadNotebooks() {
  338.  
  339.             using (var conn = new SQLiteConnection(DatabaseHelper.dbFile)) {
  340.  
  341.                 var notebooks = conn.Table<NoteBook>().ToList();
  342.  
  343.                 NoteBooks.Clear();
  344.                 foreach (var item in notebooks) {
  345.                     NoteBooks.Add(item);
  346.                 }
  347.             }
  348.         }
  349.  
  350.         public void ReadNoote() {
  351.  
  352.             using (var conn = new SQLiteConnection(DatabaseHelper.dbFile)) {
  353.  
  354.                 if (SelectedNotebook != null) {
  355.                     var notes = conn.Table<Note>().Where(n => n.NotebookId == SelectedNotebook.Id).ToList();
  356.  
  357.                     Notes.Clear();
  358.                     foreach (var item in notes) {
  359.                         Notes.Add(item);
  360.                     }
  361.                 }
  362.             }
  363.         }
  364.  
  365.         public event PropertyChangedEventHandler PropertyChanged;
  366.  
  367.  
  368.         private void OnPropertyChanged(string property) {
  369.             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
  370.         }
  371.     }
  372. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement