RichTextBox for UWP
手順4:BottomAppBar 用コードの追加
チュートリアル > AppBar アプリケーションの作成 > 手順4:BottomAppBar 用コードの追加

この手順では、BottomAppBar 項目のフライアウトイベントとクリックイベントを処理するコードを追加します。

  1. 最初にページに追加するコードセクションには、クリップボード、元に戻す/やり直す、リスト、書式のクリア、下付き/上付き文字に対応する5つの領域があります。

    C# コードの書き方

    C#
    コードのコピー
    #region Clipboard
            private void btnCopy_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
            {
                rtb.ClipboardCopy();
            }
     
            private void btnCut_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
            {
                if (rtb.IsReadOnly)
                    rtb.ClipboardCopy();
                else
                    rtb.ClipboardCut();
            }
     
            private void btnPaste_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
            {
                if (!rtb.IsReadOnly)
                {
                    rtb.ClipboardPaste();
                }
            }
            #endregion
     
            #region Undo/Redo
            void btnRedo_Click(object sender, RoutedEventArgs e)
            {
                if (rtb.DocumentHistory.CanRedo)
                {
                    rtb.DocumentHistory.Redo();
                }
                morePopUp.Hide();
            }
     
            void btnUndo_Click(object sender, RoutedEventArgs e)
            {
                if (rtb.DocumentHistory.CanUndo)
                {
                    rtb.DocumentHistory.Undo();
                }
                morePopUp.Hide();
            }
            #endregion
     
            #region Lists
            void btnBulletedList_Click(object sender, RoutedEventArgs e)
            {
                // 選択範囲が既にリストかどうかをチェックします
                if (rtb.Selection.Lists.Count<C1.Xaml.RichTextBox.Documents.C1List>() > 0)
                {
                    // リストを解除します
                    rtb.Selection.UndoList();
                }
                else
                {
                    // 箇条書きリストを作成します
                    rtb.Selection.MakeList(C1.Xaml.RichTextBox.Documents.TextMarkerStyle.Disc);
                }
                morePopUp.Hide();
            }
     
            void btnNumberedList_Click(object sender, RoutedEventArgs e)
            {
                // 選択範囲が既にリストかどうかをチェックします
                if (rtb.Selection.Lists.Count<C1.Xaml.RichTextBox.Documents.C1List>() > 0)
                {
                    // リストを解除します
                    rtb.Selection.UndoList();
                }
                else
                {
                    // 番号付きリストを作成します
                    rtb.Selection.MakeList(C1.Xaml.RichTextBox.Documents.TextMarkerStyle.Decimal);
                }
                morePopUp.Hide();
            }
            #endregion
     
            #region clear formatting
            void btnClear_Click(object sender, RoutedEventArgs e)
            {
                // 前景色と背景色をクリアします
                rtb.Selection.InlineBackground = null;
                rtb.Selection.Foreground = rtb.Foreground;
     
                // フォントをクリアします
                rtb.Selection.FontWeight = FontWeights.Normal;
                rtb.Selection.FontStyle = FontStyle.Normal;
                rtb.Selection.TextDecorations = null;
            }
            #endregion
     
            #region sub/super script
            void btnSubscript_Click(object sender, RoutedEventArgs e)
            {
                // 下付き文字
                if (rtb.Selection.InlineAlignment != C1VerticalAlignment.Sub && rtb.Selection.InlineAlignment != null)
                {
                    rtb.Selection.InlineAlignment = C1VerticalAlignment.Sub;
                    ShrinkFont(4);
                }
                else
                {
                    rtb.Selection.InlineAlignment = C1VerticalAlignment.Baseline;
                    GrowFont(4);
                }
                morePopUp.Hide();
            }
     
            void btnSuperscript_Click(object sender, RoutedEventArgs e)
            {
                // 上付き文字
                if (rtb.Selection.InlineAlignment != C1VerticalAlignment.Super && rtb.Selection.InlineAlignment != null)
                {
                    rtb.Selection.InlineAlignment = C1VerticalAlignment.Super;
                    ShrinkFont(4);
                }
                else
                {
                    rtb.Selection.InlineAlignment = C1VerticalAlignment.Baseline;
                    GrowFont(4);
                }
                morePopUp.Hide();
            }
     
            private void GrowFont(int size)
            {
                // フォント拡大
                rtb.Selection.TrimRuns();
                foreach (var run in rtb.Selection.Runs)
                {
                    run.FontSize += size;
                }
            }
     
            private void ShrinkFont(int size)
            {
                // フォント縮小
                rtb.Selection.TrimRuns();
                foreach (var run in rtb.Selection.Runs)
                {
                    run.FontSize -= size;
                }
            }
            #endregion
    
  2. 次に追加するコード領域には、[その他]ボタンとメニューのイベントがあります。

    C# コードの書き方

    C#
    コードのコピー
    #region More
            Flyout morePopUp = new Flyout();
            private void btnMore_Click(object sender, RoutedEventArgs e)
            {
                morePopUp.Placement = FlyoutPlacementMode.Top;
                morePopUp.ShowAt(sender as FrameworkElement);
            }
     
            void InitMorePopup()
            {
                Border menuBorder = new Border();
                menuBorder.Height = 260;
                menuBorder.Width = 150;
                StackPanel panel = new StackPanel();
                panel.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Center;
     
                // 箇条書きリスト
                Button btnBulletedList = new Button();
                btnBulletedList.Content = "Bulleted List";
                btnBulletedList.Style = Application.Current.Resources["MenuTextButtonStyle"] as Style;
                btnBulletedList.Margin = new Thickness(20, 5, 20, 5);
                btnBulletedList.Click += btnBulletedList_Click;
     
                // 番号付きリスト
                Button btnNumberedList = new Button();
                btnNumberedList.Content = "Numbered List";
                btnNumberedList.Style = Application.Current.Resources["MenuTextButtonStyle"] as Style;
                btnNumberedList.Margin = new Thickness(20, 5, 20, 5);
                btnNumberedList.Click += btnNumberedList_Click;
     
                // 元に戻す
                Button btnUndo = new Button();
                btnUndo.Content = "Undo";
                btnUndo.Style = Application.Current.Resources["MenuTextButtonStyle"] as Style;
                btnUndo.Margin = new Thickness(20, 5, 20, 5);
                btnUndo.Click += btnUndo_Click;
     
                // 元に戻す
                Button btnRedo = new Button();
                btnRedo.Content = "Redo";
                btnRedo.Style = Application.Current.Resources["MenuTextButtonStyle"] as Style;
                btnRedo.Margin = new Thickness(20, 5, 20, 5);
                btnRedo.Click += btnRedo_Click;
     
                // 書式のクリア
                Button btnClear = new Button();
                btnClear.Content = "Clear Formatting";
                btnClear.Style = Application.Current.Resources["MenuTextButtonStyle"] as Style;
                btnClear.Margin = new Thickness(20, 5, 20, 5);
                btnClear.Click += btnClear_Click;
     
                // 上付き文字
                Button btnSuperscript = new Button();
                btnSuperscript.Content = "Superscript";
                btnSuperscript.Style = Application.Current.Resources["MenuTextButtonStyle"] as Style;
                btnSuperscript.Margin = new Thickness(20, 5, 20, 5);
                btnSuperscript.Click += btnSuperscript_Click;
     
                // 下付き文字
                Button btnSubscript = new Button();
                btnSubscript.Content = "Subscript";
                btnSubscript.Style = Application.Current.Resources["MenuTextButtonStyle"] as Style;
                btnSubscript.Margin = new Thickness(20, 5, 20, 5);
                btnSubscript.Click += btnSubscript_Click;
     
                // 取り消し線
                Button btnStrikethrough = new Button();
                btnStrikethrough.Content = "Strikethrough";
                btnStrikethrough.Style = Application.Current.Resources["MenuTextButtonStyle"] as Style;
                btnStrikethrough.Margin = new Thickness(20, 5, 20, 5);
                btnStrikethrough.Click += btnStrikethrough_Click;
     
                panel.Children.Add(btnBulletedList);
                panel.Children.Add(btnNumberedList);
                panel.Children.Add(btnSubscript);
                panel.Children.Add(btnSuperscript);
                panel.Children.Add(btnStrikethrough);
                panel.Children.Add(btnUndo);
                panel.Children.Add(btnRedo);
                panel.Children.Add(btnClear);
                menuBorder.Child = panel;
                morePopUp.Content = menuBorder;
     
            }
     
            void btnStrikethrough_Click(object sender, RoutedEventArgs e)
            {
                // 取り消し線
                var range = rtb.Selection;
                var collection = new C1TextDecorationCollection();
                if (range.TextDecorations == null)
                {
                    collection.Add(C1TextDecorations.Strikethrough[0]);
                }
                else if (!range.TextDecorations.Contains(C1TextDecorations.Strikethrough[0]))
                {
                    foreach (var decoration in range.TextDecorations)
                        collection.Add(decoration);
     
                    collection.Add(C1TextDecorations.Strikethrough[0]);
                }
                else
                {
                    foreach (var decoration in range.TextDecorations)
                        collection.Add(decoration);
     
                    collection.Remove(C1TextDecorations.Strikethrough[0]);
                    if (collection.Count == 0)
                        collection = null;
                }
                range.TextDecorations = collection;
                morePopUp.Hide();
            }
            #endregion
    
  3. 最後のコードセクションでは、PointerPressed イベントを処理します。

    C# コードの書き方

    C#
    コードのコピー
    private void rtb_PointerPressed(object sender, PointerRoutedEventArgs e)
            {
                bottomAppBar.IsOpen = true;
                topAppBar.IsOpen = true;
            }
        }
    }
    

この手順では、下部の AppBar のイベントコードを追加しました。次の手順では、このアプリケーションを実行します。