用WinUI3开发一个正则表达式工具

WinUI3推出一段时间了 ,但目前这个版本的vs2022 17.1 还不能可视化也不能看属性,只能纯代码开发,而且有一些组件属性跟wpf和uwp还有点区别 昨天稍微试着写了一下,现在把过程贴出来给新手指指路

首先下载winui3模板

然后创建新项目的时候我选的这个 空白应用已打包不带wap那个也可以

直接运行就是一个按钮程序

然后先写布局 Grid 三行两列

<Window
    x:Class="RegexZoo.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:local="using:RegexZoo"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition MinHeight="400" />

        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <TextBlock
            Grid.Row="1"
            Grid.Column="0"
            Margin="5"
            Text="原文本" />
        <TextBox
            Name="tbSource"
            Grid.Row="2"
            Grid.Column="0"
            MinHeight="400"
            Margin="5"
            HorizontalAlignment="Stretch"
            BorderBrush="#69c0ff"
            BorderThickness="1"
            ScrollViewer.VerticalScrollBarVisibility="Auto"
            TextWrapping="Wrap" />
        <TextBox
            Name="tbPattern"
            Grid.Row="0"
            Grid.Column="0"
            Width="400"
            Margin="5"
            HorizontalAlignment="Left"
            TextWrapping="Wrap" />
        <Button
            x:Name="myButton"
            Grid.Row="0"
            Grid.Column="0"
            Width="100"
            Margin="5"
            HorizontalAlignment="Right"
            Background="#69c0ff"
            Click="myButton_Click"
            Foreground="#1890ff">
            匹配
        </Button>
        <TextBlock
            Grid.Row="1"
            Grid.Column="1"
            Margin="5"
            Text="匹配结果" />
        <TextBox
            Name="tbDestination"
            Grid.Row="2"
            Grid.Column="1"
            MinHeight="400"
            Margin="5"
            HorizontalAlignment="Stretch"
            BorderBrush="#69c0ff"
            BorderThickness="1"
            ScrollViewer.VerticalScrollBarVisibility="Auto"
            TextWrapping="Wrap" />
        <ComboBox
            Name="cbHistory"
            Grid.Row="0"
            Grid.Column="1"
            Width="400"
            Margin="5"
            SelectionChanged="cbHistory_SelectionChanged" />
        <TextBlock
            Grid.Row="0"
            Grid.Column="1"
            Width="100"
            Margin="5"
            HorizontalAlignment="Right"
            VerticalAlignment="Center"
            Text="历史Pattern记录" />
    </Grid>
</Window>

两个个文本框第一个输入pattern 下面输入原文本 右边 一个combobox作为历史记录 再来一个输出文本框

然后给按钮写事件实现

正则很简单,一个函数就搞定

private void myButton_Click(object sender, RoutedEventArgs e)
        {
            string input = tbSource.Text;
            string pattern = tbPattern.Text;
            string result = "";

            hislist.Add(pattern);
            if (hislist.Count > 10)
                hislist.RemoveAt(0);

            foreach (Match match in Regex.Matches(input, pattern))
                result += match.Value + "\n\n";
            tbDestination.Text = result;
        }

历史记录那 跟combobox做个绑定

public MainWindow()
        {
            this.InitializeComponent();
            Title = "Regex Zoo";
            cbHistory.ItemsSource = hislist;
        }
private void cbHistory_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            tbPattern.Text = (sender as ComboBox).SelectedValue.ToString();
        }

历史的选择事件 实现可以把搜过的赋值回到pattern框里 

大功告成

现在还不太成熟 xaml熟的人可以先开发着 稍微难受一点

效果图

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

充值内卷

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值