RichEditBox和MVVM

时间:2019-11-06 23:23:25

标签: uwp

  1. 我正在尝试一种方法,通过三个按钮来影响我的"EditBox"

  2. 当我更改comboboxes变量时,我想更改字体名称和大小。

我有一个switch语句和标签,用于定义每个按钮 我试图通过调用CharacterFormat来更改字体的大小 我已经为此设置了虚拟机,但是现在我不知道如何传递每个按钮的标签

<Page x:Name="Main"
  x:Class="uwpEvernote.View.NotesPage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="using:uwpEvernote.View"
  xmlns:vm="using:uwpEvernote.ViewModel"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  mc:Ignorable="d"
  Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Page.Resources>
    <vm:NotesVM x:Key="VM" />
    <SolidColorBrush x:Key="enabled"
                     Color="#0078D4" />
    <SolidColorBrush x:Key="disabled"
                     Color="Transparent" />
</Page.Resources>

<RelativePanel x:Name="Container"
               DataContext="{StaticResource VM}"
               Background="{ThemeResource SystemChromeLowColor}"
               Loaded="Container_Loaded">
    <MenuBar x:Name="menuBar">
        <MenuBarItem Title="File">
            <MenuFlyoutItem Text="New notebook"
                            Command="{Binding NewNotebookCommand}">
                <MenuFlyoutItem.Icon>
                    <FontIcon Glyph="&#xE82D;" />
                </MenuFlyoutItem.Icon>
            </MenuFlyoutItem>
            <MenuFlyoutItem Text="New Note"
                            Command="{Binding NewNoteCommand}"
                            CommandParameter="{Binding SelectedNotebook}">
                <MenuFlyoutItem.Icon>
                    <FontIcon Glyph="&#xE70B;" />
                </MenuFlyoutItem.Icon>
            </MenuFlyoutItem>
            <MenuFlyoutSeparator />
            <MenuFlyoutItem Text="Exit"
                            Command="{Binding ExitCommand}">
                <MenuFlyoutItem.Icon>
                    <FontIcon Glyph="&#xE106;" />
                </MenuFlyoutItem.Icon>
            </MenuFlyoutItem>
        </MenuBarItem>

    </MenuBar>
    <ListView x:Name="Notebook"
              RelativePanel.Below="menuBar"
              Width="140"
              SelectedItem="{Binding SelectedNotebook, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
              ItemsSource="{Binding NoteBooks}"
              RelativePanel.AlignBottomWithPanel="True">
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}" />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    <ListView x:Name="Notes"
              Width="140"
              ItemsSource="{Binding Notes}"
              RelativePanel.Below="menuBar"
              RelativePanel.RightOf="Notebook"
              RelativePanel.AlignBottomWithPanel="True">
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Title}" />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    <CommandBar x:Name="commandBar"
                RelativePanel.Below="menuBar"
                RelativePanel.RightOf="Notes"
                RelativePanel.AlignRightWithPanel="True"
                OverflowButtonVisibility="Collapsed"
                VerticalAlignment="Center"
                Margin="0,10,20,10"
                Background="{ThemeResource  SystemChromeLowColor}">
        <CommandBar.Content>
            <StackPanel Orientation="Horizontal">
                <AppBarButton x:Name="textToSpeech"
                              Icon="Microphone"
                              Click="Actions_Click"
                              Tag="0"
                              ToolTipService.ToolTip="Text to speech" />
                <AppBarButton x:Name="FormatBoltText"
                              ToolTipService.ToolTip="Bold"
                              Icon="Bold"
                              Tag="1"
                              Click="Actions_Click" />
                <AppBarButton x:Name="formatItalicText"
                              ToolTipService.ToolTip="Italic"
                              Icon="Italic"
                              Tag="2"
                              Click="Actions_Click" />
                <AppBarButton x:Name="formatUnderlineText"
                              ToolTipService.ToolTip="Underline"
                              Icon="Underline"
                              Tag="3"
                              Click="Actions_Click" />
                <ComboBox IsEditable="True"
                          Tag="1"
                          x:Name="fontBox"
                          SelectionChanged="ComboChanged"
                          Width="150" />
                <ComboBox x:Name="fontSizeBox"
                          Tag="2"
                          SelectionChanged="ComboChanged"
                          IsEditable="True"
                          Margin="10,0,0,0"
                           />
            </StackPanel>

        </CommandBar.Content>

    </CommandBar>
    <RichEditBox x:Name="richEbitBox"
                 TextChanged="Cotent_TextChanged"
                 RelativePanel.RightOf="Notes"
                 RelativePanel.Below="commandBar"
                 RelativePanel.AlignRightWithPanel="True"
                 RelativePanel.AlignBottomWith="Notebook"
                 Margin="0,0,10,40" />
    <CommandBar Background="{ThemeResource  SystemChromeLowColor}"
                RelativePanel.RightOf="Notes"
                RelativePanel.AlignBottomWith="richEbitBox"
                RelativePanel.AlignRightWithPanel="True"
                Margin="0,0,10,0"
                HorizontalAlignment="Left"
                VerticalAlignment="Center">
        <CommandBar.Content>
            <StackPanel Orientation="Horizontal"
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center"
                        Margin="0,10,0,0">
                <TextBlock Text="Count"
                           VerticalAlignment="Center"
                           HorizontalAlignment="Center" />
                <TextBlock Text="|"
                           Margin="10,0,0,0" />
                <TextBlock x:Name="charactersCount"
                           Margin="10,0,0,0" />
            </StackPanel>
        </CommandBar.Content>
    </CommandBar>
</RelativePanel>

  public ObservableCollection<NoteBook> NoteBooks { get; set; }

    private NoteBook _SelectedNotebook;

    public NoteBook SelectedNotebook {
        get { return _SelectedNotebook; }
        set {
            if (value != _SelectedNotebook) {
                _SelectedNotebook = value;
                //OnPropertyChanged("SelectedNotebook");
            }
        }
    }

    public ObservableCollection<Note> Notes { get; set; }
    public NewNoteCommand NewNoteCommand { get; set; }
    public NewNotebookCommand NewNotebookCommand { get; set; }
    public ExitCommand ExitCommand { get; set; }


    public NotesVM() {

        NewNoteCommand = new NewNoteCommand(this);
        NewNotebookCommand = new NewNotebookCommand(this);
        ExitCommand = new ExitCommand(this);
        NoteBooks = new ObservableCollection<NoteBook>();
        Notes = new ObservableCollection<Note>();

        ReadNotebooks();
    }

    public void CreateNotebook() {

        var newNotebook = new NoteBook() {
            Name = "New notebook"
        };

        DatabaseHelper.Insert(newNotebook);
    }

    public void CreateNote(int id) {

        var newNote = new Note() {
            NotebookId = id,
            CratedTime = DateTime.Now,
            UpdatedTime = DateTime.Now,
            Title = "New note"
        };

        DatabaseHelper.Insert(newNote);
    }

    public void ReadNotebooks() {

        using (var conn = new SQLiteConnection(DatabaseHelper.dbFile)) {

            var notebooks = conn.Table<NoteBook>().ToList();

            NoteBooks.Clear();
            foreach (var item in notebooks) {
                NoteBooks.Add(item);
            }
        }
    }

    public void ReadNoote() {

        using (var conn = new SQLiteConnection(DatabaseHelper.dbFile)) {

            if (SelectedNotebook != null) {
                var notes = conn.Table<Note>().Where(n => n.NotebookId == SelectedNotebook.Id).ToList();

                Notes.Clear();
                foreach (var item in notes) {
                    Notes.Add(item);
                } 
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;


    private void OnPropertyChanged(string property) {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
    }
}

}

        public NotesPage() {
        this.InitializeComponent();

        var fonts = CanvasTextFormat.GetSystemFontFamilies();
        fontBox.ItemsSource = fonts;
        var arrList = new ArrayList();
        for (int i = 0; i < 73; ++i) {
            arrList.Add(i);
        }
        fontSizeBox.ItemsSource = arrList;
    }

    private void Cotent_TextChanged(object sender, RoutedEventArgs e) {

        richEbitBox.Document.GetText(TextGetOptions.None, out string value);
        charactersCount.Text = (value.Length - 1).ToString();
    }

    private async void Actions_Click(object sender, RoutedEventArgs e) {

        var id = sender as Button;

        switch (id.Tag) {

            case "0":
                using (SpeechRecognizer recognizer = new SpeechRecognizer()) {
                    await recognizer.CompileConstraintsAsync();
                    var result = await recognizer.RecognizeWithUIAsync();
                    var dialog = new MessageDialog(result.Text, "Text spoken");
                    await dialog.ShowAsync();

                    richEbitBox.Document.GetText(TextGetOptions.None, out string value);
                    richEbitBox.Document.SetText(TextSetOptions.None, value += result.Text);
                }
                break;
            case "1":
                if (richEbitBox.Document.Selection.CharacterFormat.Bold == FormatEffect.On) {
                    richEbitBox.Document.Selection.CharacterFormat.Bold = FormatEffect.Off;
                    FormatBoltText.Background = (SolidColorBrush)Resources["disabled"];
                } else {
                    richEbitBox.Document.Selection.CharacterFormat.Bold = FormatEffect.On;
                    FormatBoltText.Background = (SolidColorBrush)Resources["enabled"];
                }
                break;
            case "2":
                if (richEbitBox.Document.Selection.CharacterFormat.Italic == FormatEffect.On) {
                    richEbitBox.Document.Selection.CharacterFormat.Italic = FormatEffect.Off;
                    formatItalicText.Background = (SolidColorBrush)Resources["disabled"];
                } else {
                    richEbitBox.Document.Selection.CharacterFormat.Italic = FormatEffect.On;
                    formatItalicText.Background = (SolidColorBrush)Resources["enabled"];
                }
                break;
            case "3":
                if (richEbitBox.Document.Selection.CharacterFormat.Underline == UnderlineType.Single) {
                    richEbitBox.Document.Selection.CharacterFormat.Underline = UnderlineType.None;
                    formatUnderlineText.Background = (SolidColorBrush)Resources["disabled"];
                } else {
                    richEbitBox.Document.Selection.CharacterFormat.Underline = UnderlineType.Single;
                    formatUnderlineText.Background = (SolidColorBrush)Resources["enabled"];
                }
                break;
            default:
                break;
        }
    }

    private void ComboChanged(object sender, SelectionChangedEventArgs e) {

        var id = sender as ComboBox;

        switch (id.Tag) {

            case "1":
                //Todo implement new font
                string fontName = id.SelectedItem.ToString();
                richEbitBox.Document.Selection.CharacterFormat.Name = fontName;
                break;
            case "2":
                var size = (float)id.SelectedItem;
                break;
            default:
                break;
        }
    }

    private void Container_Loaded(object sender, RoutedEventArgs e) {
        fontBox.Text = richEbitBox.Document.GetDefaultCharacterFormat().Name;
        fontSizeBox.Text = richEbitBox.Document.GetDefaultCharacterFormat().Size.ToString();
    }
}

}

  1. 更改字体大小,当我下拉并选择一个数字和字体名称时
  2. 从背后的代码中消除所有内容(我不知道使用它是否不好)

1 个答案:

答案 0 :(得分:1)

  
    

我不知道如何传递每个按钮的标签。

  

在哪种情况下您要传递每个按钮的标签?具体行为是什么?

  
    

每次运行识别器时,它都会创建一个新行,而我不希望这样。

  

这是因为当您使用此代码richEbitBox.Document.GetText(TextGetOptions.None, out string value);获取文本时,它将自动在文本末尾添加'\ r'。通过测试,您可以使用TextGetOptions.AdjustCrlf设置TextGetOptions,它将不会创建新行。

richEbitBox.Document.GetText(TextGetOptions.AdjustCrlf, out string value);
richEbitBox.Document.SetText(TextSetOptions.None, newText += result.Text);

或者在获取文本后,首先删除最后一个字符,然后设置新值。

richEbitBox.Document.GetText(TextGetOptions.None, out string value);
string newText = value.Remove(value.Length - 1);
richEbitBox.Document.SetText(TextSetOptions.None, newText += result.Text);

更新

  
    

如何更改字体大小。

  

您可以尝试以下代码来更改richEditBox的大小。

private void ComboChanged(object sender, SelectionChangedEventArgs e)
{
    var id = sender as ComboBox;
    switch (id.Tag)
    {
        case "2":
        richEbitBox.Document.Selection.CharacterFormat.Size = Convert.ToInt64(id.SelectedItem);
        break;
        ......
    }
}