[实例]UWP之正则获取文本中的超链接到剪切板并粘贴

前端之家收集整理的这篇文章主要介绍了[实例]UWP之正则获取文本中的超链接到剪切板并粘贴前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

MainPage.xaml中

<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <TextBlock  Height="200" TextWrapping="Wrap"  Loaded="TextBlock_Loaded" Tapped="TextBlock_Tapped">
            dsdsdsdsadsadddsdsdsdhttp://www.baidu.com dsadsadsadsahttps://www.baidu.com1 daewqewqeqwewqdaewq
            dsdsdsdsadsadddsdsdsdhttp://www.baidu.com6 dsadsadsadsahttps://www.baidu.com2 daewqewqeqwewqdaewq
            dsdsdsdsadsadddsdsdsdhttp://www.baidu.com7 dsadsadsadsahttps://www.baidu.com3 daewqewqeqwewqdaewq
            dsdsdsdsadsadddsdsdsdhttp://www.baidu.com8 dsadsadsadsahttps://www.baidu.com4 daewqewqeqwewqdaewq
            dsdsdsdsadsadddsdsdsdhttp://www.baidu.com9 dsadsadsadsahttps://www.baidu.com5 daewqewqeqwewqdaewq
            <FlyoutBase.AttachedFlyout>
                <Flyout x:Name="HyperText" Placement="Full">
                    <StackPanel>
                        <ListView Name="ttt" SelectionChanged="ttt_SelectionChanged"/>
                    </StackPanel>
                </Flyout>
            </FlyoutBase.AttachedFlyout>
        </TextBlock>

        <StackPanel Orientation="Horizontal">
            <Button Content="粘贴超链接" Click="Button_Click" />
            <TextBox  Name="hhh"/>
        </StackPanel>
        

    </StackPanel>

MainPage.cs中

using System.Text.RegularExpressions;
using Windows.ApplicationModel.DataTransfer;        
   private void TextBlock_Loaded(object sender,RoutedEventArgs e)
        {
            string str = ((TextBlock)sender).Text.ToString().ToLower();
            string pattern = @"https?://[^\s]+\s";
            MatchCollection mc = Regex.Matches(str,pattern);

            if(mc.Count > 0)
            {
                ttt.Items.Clear();
                foreach (Match t in mc)
                {
                    ttt.Items.Add(t.Value.ToString().Trim());
                }
            }

        }



        private void ttt_SelectionChanged(object sender,SelectionChangedEventArgs e)
        {
            var ddd = (ListView)sender;
            DataPackage dp = new DataPackage();
            dp.SetText(ddd.SelectedItem.ToString());
            Clipboard.SetContent(dp);
            HyperText.Hide();
        }

        private void TextBlock_Tapped(object sender,TappedRoutedEventArgs e)
        {
            var fff = (TextBlock)sender;
            FlyoutBase.ShowAttachedFlyout(fff);
        }

        private async void Button_Click(object sender,RoutedEventArgs e)
        {
            DataPackageView con = Clipboard.GetContent();
            
            if (con.Contains(StandardDataFormats.Text))
            {
                hhh.Text = await con.GetTextAsync();
            }
        }



实现的效果如下:


知识链接win10 UWP 剪贴板 Clipboard

原文链接:https://www.f2er.com/regex/358753.html

猜你在找的正则表达式相关文章