「東雲 忠太郎」の平凡な日常のできごと

「東雲 忠太郎」の平凡な日常のできごと

2024.06.09
XML
カテゴリ: WPFC#.NET


以下は、C#のWPFでHTMLをXAML形式に変換して画面に表示するサンプルコードです。このサンプルコードでは、HtmlToXamlConverterクラスを使用してHTMLをXAMLに変換し、そのXAMLをRichTextBoxコントロールに表示します。


まず、HtmlToXamlConverterクラスを作成します。



using System.IO;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Markup;


namespace WpfApp

{

    public static class HtmlToXamlConverter

    {

        public static string ConvertHtmlToXaml(string html)

        {

            var richTextBox = new RichTextBox();

            var textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);


            using (var memoryStream = new MemoryStream())

            {

                using (var streamWriter = new StreamWriter(memoryStream))

                {

                    streamWriter.Write(html);

                    streamWriter.Flush();

                    memoryStream.Seek(0, SeekOrigin.Begin);


                    textRange.Load(memoryStream, DataFormats.Html);

                }

            }


            using (var memoryStream = new MemoryStream())

            {

                textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);

                textRange.Save(memoryStream, DataFormats.Xaml);

                return System.Text.Encoding.Default.GetString(memoryStream.ToArray());

            }

        }

    }

}

```


次に、メインウィンドウのXAMLファイルにRichTextBoxコントロールを配置します。


```xml

<Window x:Class="WpfApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:WpfApp"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800">

    <Grid>

        <RichTextBox x:Name="richTextBox" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"/>

    </Grid>

</Window>

```


最後に、メインウィンドウのコードビハインドでHTMLをXAMLに変換してRichTextBoxに表示します。


```csharp

using Microsoft.Win32;

using System;

using System.IO;

using System.Windows;


namespace WpfApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

            LoadHtmlFile();

        }


        private void LoadHtmlFile()

        {

            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "HTML files (*.html;*.htm)|*.html;*.htm|All files (*.*)|*.*";


            if (openFileDialog.ShowDialog() == true)

            {

                string html = File.ReadAllText(openFileDialog.FileName);

                string xaml = HtmlToXamlConverter.ConvertHtmlToXaml(html);

                richTextBox.Document = (FlowDocument)XamlReader.Parse(xaml);

            }

        }

    }

}

```


このコードでは、メインウィンドウが起動されると、OpenFileDialogを使用してHTMLファイルを選択し、そのHTMLをXAMLに変換してRichTextBoxに表示します。






お気に入りの記事を「いいね!」で応援しよう

Last updated  2024.06.09 11:23:38


【毎日開催】
15記事にいいね!で1ポイント
10秒滞在
いいね! -- / --
おめでとうございます!
ミッションを達成しました。
※「ポイントを獲得する」ボタンを押すと広告が表示されます。
x
X

© Rakuten Group, Inc.
X
Design a Mobile Site
スマートフォン版を閲覧 | PC版を閲覧
Share by: