首頁 > 軟體

WPF+DiffPlex實現文字比對工具

2022-11-21 14:01:25

背景

現行的文字編輯器大多都具備文字查詢的能力,但是並不能直觀的告訴使用者兩段文字的細微差異,所以對比工具在某種情況下,就起到了很便捷的效率。

關於 DiffPlex

DiffPlex 是用於生成文字差異的 C# 庫

準備

NuGet 包

DiffPlex.Wpf 主要包

MaterialDesignThemes 主題包

程式碼實現

MainWindow.xaml

<Window
    x:Class="TextComparisonTool.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:diffplex="clr-namespace:DiffPlex.Wpf.Controls;assembly=DiffPlex.Wpf"
    xmlns:local="clr-namespace:TextComparisonTool"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="文字比對工具"
    Width="800"
    Height="450"
    Icon="DiffPlex.ico"
    WindowState="Maximized"
    mc:Ignorable="d">

    <Grid Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition Height="40" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <WrapPanel>
            <Button
                x:Name="BtnInput"
                Click="BtnInput_Click"
                Content="輸入文字"
                Style="{DynamicResource MaterialDesignFlatAccentBgButton}" />
        </WrapPanel>
        <diffplex:DiffViewer x:Name="DiffView" Grid.Row="1" />
    </Grid>

</Window>

MainWindow.xaml.cs

using System.Windows;

namespace TextComparisonTool
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();           
        }
         

        private void BtnInput_Click(object sender, RoutedEventArgs e)
        {
            InputOldeTextAndNewText input = new();

            input.ShowDialog();

            if (input.DialogResult is true)
            {
                DiffView.OldText = input.txtOldText.Text;
                DiffView.NewText = input.txtNewText.Text;
            }
        }
    }
}

InputOldeTextAndNewText.xaml

<Window
    x:Class="TextComparisonTool.InputOldeTextAndNewText"
    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"
    Title="輸入新舊文字"
    Width="850"
    Height="500"
    Icon="DiffPlex.ico"
    ResizeMode="CanMinimize"
    WindowStartupLocation="CenterScreen"
    mc:Ignorable="d">
    <Border Margin="5" CornerRadius="11">
        <StackPanel>
            <TextBlock Style="{DynamicResource MaterialDesignBody1TextBlock}" Text="源文字" />
            <TextBox
                x:Name="txtOldText"
                AcceptsReturn="True"
                MaxLines="10"
                MinLines="10"
                TextWrapping="Wrap" />
            <TextBlock
                VerticalAlignment="Center"
                Style="{DynamicResource MaterialDesignBody1TextBlock}"
                Text="新文字" />
            <TextBox
                x:Name="txtNewText"
                AcceptsReturn="True"
                MaxLines="10"
                MinLines="10"
                TextWrapping="Wrap" />
            <Button
                x:Name="BtnText"
                Margin="10"
                Click="BtnText_Click"
                Content="確認"
                Style="{DynamicResource MaterialDesignFlatButton}" />
        </StackPanel>
    </Border>
</Window>

InputOldeTextAndNewText.xaml.cs

using System.Windows;

namespace TextComparisonTool
{
    /// <summary>
    /// InputOldeTextAndNewText.xaml 的互動邏輯
    /// </summary>
    public partial class InputOldeTextAndNewText : Window
    {
        public InputOldeTextAndNewText()
        {
            InitializeComponent();
        }

        private void BtnText_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
        }
    }
}

效果圖

到此這篇關於WPF+DiffPlex實現文字比對工具的文章就介紹到這了,更多相關WPF DiffPlex文字比對工具內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com