Uwp RichEditBox和MVVM

Uwp RichEditBox和MVVM,uwp,Uwp,我正在想办法用我的三个按钮影响我的“EditBox” 当我更改组合框变量时,我想更改字体名称和大小 我有一个switch语句和标记来定义每个按钮 我试图通过调用CharacterFormat 我已经为此设置了VM,但我现在不知道如何传递每个按钮的标签 <Page x:Name="Main" x:Class="uwpEvernote.View.NotesPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presenta

我正在想办法用我的三个按钮影响我的
“EditBox”

  • 当我更改
    组合框
    变量时,我想更改字体名称和大小

  • 我有一个switch语句和标记来定义每个按钮 我试图通过调用
    CharacterFormat
    我已经为此设置了VM,但我现在不知道如何传递每个按钮的标签

    <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>
    
    
    

    publicobservableCollection笔记本{get;set;}
    私人笔记本(可选笔记本);;
    公共笔记本选择笔记本{
    获取{return\u SelectedNotebook;}
    设置{
    如果(值!=\u选择的笔记本){
    _SelectedNotebook=值;
    //OnPropertyChanged(“SelectedNotebook”);
    }
    }
    }
    公共可观测集合注释{get;set;}
    public NewNoteCommand NewNoteCommand{get;set;}
    public NewNotebookCommand NewNotebookCommand{get;set;}
    公共ExitCommand ExitCommand{get;set;}
    公共票据(){
    NewNoteCommand=新的NewNoteCommand(此);
    NewNotebookCommand=新建NewNotebookCommand(此命令);
    ExitCommand=新ExitCommand(此命令);
    笔记本=新的ObservableCollection();
    注释=新的ObservableCollection();
    阅读笔记本();
    }
    公共笔记本(){
    var newNotebook=新笔记本(){
    Name=“新笔记本”
    };
    DatabaseHelper.Insert(newNotebook);
    }
    public void CreateNote(int-id){
    var newNote=newNote(){
    NotebookId=id,
    CratedTime=日期时间。现在,
    UpdateTime=DateTime。现在,
    Title=“新注释”
    };
    DatabaseHelper.Insert(newNote);
    }
    公共图书馆{
    使用(var conn=newsqliteconnection(DatabaseHelper.dbFile)){
    var笔记本=连接表().ToList();
    笔记本。清除();
    foreach(笔记本中的var项目){
    笔记本。添加(项目);
    }
    }
    }
    公共无效ReadNoote(){
    使用(var conn=newsqliteconnection(DatabaseHelper.dbFile)){
    如果(SelectedNotebook!=null){
    var notes=conn.Table().Where(n=>n.NotebookId==SelectedNotebook.Id).ToList();
    Notes.Clear();
    foreach(注释中的var项目){
    注:增加(项目);
    } 
    }
    }
    }
    公共事件属性更改事件处理程序属性更改;
    私有void OnPropertyChanged(字符串属性){
    PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(property));
    }
    }
    
    }

    public NotesPage(){
    this.InitializeComponent();
    var fonts=CanvasTextFormat.GetSystemFontFamilies();
    fontBox.ItemsSource=字体;
    var arrList=new ArrayList();
    对于(int i=0;i<73;++i){
    增列(i);
    }
    fontSizeBox.ItemsSource=arrList;
    }
    私有无效内容_text已更改(对象发送方,路由目标e){
    richEbitBox.Document.GetText(TextGetOptions.None,输出字符串值);
    characterScont.Text=(value.Length-1).ToString();
    }
    私有异步无效操作\u单击(对象发送方,路由目标){
    var id=发送者作为按钮;
    开关(id.Tag){
    案例“0”:
    使用(SpeechRecognizer recognizer=新SpeechRecognizer()){
    等待识别器。CompileConstraintsAsync();
    var result=await recognizer.RecognizeWithUIAsync();
    var dialog=新消息对话框(result.Text,“文本对话”);
    wait dialog.ShowAsync();
    richEbitBox.Document.GetText(TextGetOptions.None,输出字符串值);
    richEbitBox.Document.SetText(TextSetOptions.None,value+=result.Text);
    }
    打破
    案例“1”:
    if(richEbitBox.Document.Selection.CharacterFormat.Bold==FormatEffect.On){
    richEbitBox.Document.Selection.CharacterFormat.Bold=FormatEffect.Off;
    FormatBoltText.Background=(SolidColorBrush)资源[“已禁用”];
    }否则{
    richEbitBox.Document.Selection.CharacterFormat.Bold=FormatEffect.On;
    FormatBoltText.Background=(SolidColorBrush)资源[“已启用”];
    }
    打破
    案例“2”:
    if(richEbitBox.Document.Selection.CharacterFormat.Italic==FormatEffect.On){
    richEbitBox.Document.Selection.CharacterFormat.Italic=FormatEffect.Off;
    formatItalicText.Background=(SolidColorBrush)资源[“已禁用”];
    }否则{
    richEbitBox.Document.Selection.CharacterFormat.Italic=FormatEffect.On;
    formatItalicText.Background=(SolidColorBrush)资源[“已启用”];
    }
    打破
    案例“3”:
    if(richEbitBox.Document.Selection.CharacterFormat.Underline==UnderlineType.Single){
    richEbitBox.Document.Sel
    
      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();
        }
    }
    
    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);
    
    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;
            ......
        }
    }