写一个去除AI2XAML注释及多余数字位数的WPF窗体程序

简介: 原文:写一个去除AI2XAML注释及多余数字位数的WPF窗体程序 使用正则表达式去除多余注释及冗余数字位,关键代码:            string pattern = @"/b(/d+)/.
原文: 写一个去除AI2XAML注释及多余数字位数的WPF窗体程序

使用正则表达式去除多余注释及冗余数字位,关键代码:

            string pattern = @"/b(/d+)/.(/d{1})/d+([/,/s]?)/b";
            string temp = Regex.Replace(oldContent, pattern, @"$1.$2$3");
            temp = Regex.Replace(temp, @"<!--.*-->", "");
            temp = Regex.Replace(temp, @"[/s| ]*/r","");

运行效果截图:
XamlExportWPFWindow

// ReplaceWindow.xaml

<Window x:Class="BrawDraw.Com.AIXamlConverter.ReplaceWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="600" Width="800">
    <Grid>
        <Label Height="28" Margin="45,17,219,0" Name="label1" VerticalAlignment="Top" Foreground="Red">将XAML内容复制到下面文本框内,然后点替换,在“结果”文本框中得到新内容。</Label>
        <Button Height="23" HorizontalAlignment="Right" Margin="0,17,119,0" Name="btnReplace" VerticalAlignment="Top" Width="75" Click="btnReplace_Click">替换</Button>
        <TextBox Margin="124,45,26,0" Name="txtBoxOldContent" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Visible" Height="151" VerticalAlignment="Top" />
        <TextBox Margin="124,200,26,201" Name="txtBoxResultContent" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Visible" />
        <Label Height="28" HorizontalAlignment="Left" Margin="45,45,0,0" Name="label2" VerticalAlignment="Top" Width="91">替换前内容:</Label>
        <Label Height="28" HorizontalAlignment="Left" Margin="45,200,0,0" Name="label3" VerticalAlignment="Top" Width="91">结果:</Label>
        <StackPanel Margin="123,0,27,12" Height="180" VerticalAlignment="Bottom">
        <Border BorderBrush="Black" BorderThickness="1" Height="180" Name="borderResult">
            <StackPanel Name="spResult" />      
        </Border>
        </StackPanel>
        <Label Height="28" HorizontalAlignment="Left" Margin="45,0,0,164" Name="label4" VerticalAlignment="Bottom" Width="91">效果预览:</Label>
    </Grid>
</Window>

// ReplaceWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Text.RegularExpressions;

namespace BrawDraw.Com.AIXamlConverter
{
    /// <summary>
    /// Interaction logic for ReplaceWindow.xaml
    /// </summary>
    public partial class ReplaceWindow : Window
    {
        public ReplaceWindow()
        {
            InitializeComponent();
        }

        private void btnReplace_Click(object sender, RoutedEventArgs e)
        {
            string oldContent = txtBoxOldContent.Text;
            //string pattern = @"(/d+)/.(/d)/d{5,}([/,/s]?)";
            string pattern = @"/b(/d+)/.(/d{1})/d+([/,/s]?)/b";
            string temp = Regex.Replace(oldContent, pattern, @"$1.$2$3");
            temp = Regex.Replace(temp, @"<!--.*-->", "");
            temp = Regex.Replace(temp, @"[/s| ]*/r","");
            txtBoxResultContent.Text = temp;
            try
            {
                //下面的代码用于预览效果显示:
                System.IO.StringReader stringReader = new System.IO.StringReader(temp);
                System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(stringReader);
                UIElement uie = (UIElement)System.Windows.Markup.XamlReader.Load(xmlReader);
                spResult.Children.Add(uie);

                borderResult.BorderBrush = Brushes.Transparent;
            }
            catch (Exception exc)
            {
                MessageBox.Show(string.Format("出错了,错误是:/r/n{0}", exc.Message));
            }
        }
    }
}

 

目录
相关文章
|
2月前
|
人工智能 安全 IDE
不会写代码同学的福音——AI 代码生成器Amazon CodeWhisperer(通过注释写代码)
不会写代码同学的福音——AI 代码生成器Amazon CodeWhisperer(通过注释写代码)
59 0
|
9月前
|
C#
WPF控件和窗体一起放大一起缩小
WPF控件和窗体一起放大一起缩小
182 0
|
9月前
|
C# 容器
WPF框架下,窗体的嵌套显示
WPF框架下,窗体的嵌套显示
172 0
|
11月前
|
C#
WPF防止程序多次运行
WPF防止程序多次运行
135 0
WPF界面无法正常显示(资源引用,如转换器),但程序正常运行
WPF界面无法正常显示(资源引用,如转换器),但程序正常运行
WPF界面无法正常显示(资源引用,如转换器),但程序正常运行
|
C# 索引
WPF实用指南二:移除窗体的图标
原文:WPF实用指南二:移除窗体的图标 WPF没有提供任何功能来移除窗体上的icon图标。一般的做法是设置一个空白的图标,如下图1: 这种做法在窗体边框与标题之间仍然会保留一片空白。
1130 0
|
Java C# 程序员
WPF程序中的弱事件模式
原文:WPF程序中的弱事件模式 在C#中,得益于强大的GC机制,使得我们开发程序变得非常简单,很多时候我们只需要管使用,而并不需要关心什么时候释放资源。但是,GC有的时并不是按照我们所期望的方式工作。 例如,我想实现一个在窗口的标题栏中实时显示当前的时间,一个比较常规的做法如下:     var...
1086 0
|
人工智能 自然语言处理 前端开发
VS Code能自己编程了,GitHub推出“AI程序员”插件,根据注释自动补全代码
VS Code能自己编程了,GitHub推出“AI程序员”插件,根据注释自动补全代码
706 0
VS Code能自己编程了,GitHub推出“AI程序员”插件,根据注释自动补全代码
|
人工智能 算法 JavaScript
AI自动生成的代码还自带原来的「WTF」注释?程序员:我笑了,抄的太明显
直接复制粘贴训练集里的代码,原来的注释也一字不差。AI 的「抄能力」获得了认可。
255 0
AI自动生成的代码还自带原来的「WTF」注释?程序员:我笑了,抄的太明显
|
C# Windows
WPF 设置类库项目为启动项,设置窗体跟随。
原文:WPF 设置类库项目为启动项,设置窗体跟随。 1、添加用于启动的类Program.cs,需要一个静态的Main函数入口。
983 0