我正在从一个API中获取一些文本,该API包含嵌入在字符串响应中的超链接.例如:

This text is coming from an API. Click http://www.example.com to visit us.

文本可以包含一个或多个超链接在此传入文本的任何位置.例如,另一个API调用可以返回如下内容:

Call from http://www.example.com. Please visit us at http://www.test.org to see the possibilities.

我把它放在Label米里面.在这种情况下,是否可以使指向动态文本的内联链接可点击,并在WebView中打开相应的链接?

如果不是label,那么span可以用来解析和构造带有GestureRecognizers的传入文本吗?

推荐答案

在这种情况下,是否有可能使链接可点击,然后打开 在WebView中的链接?

是的,你可以参考官方文档:Create a reusable hyperlink class

在项目的根目录中创建一个HyperlinkSpan类:

public class HyperlinkSpan : Span
{
    public static readonly BindableProperty UrlProperty =
        BindableProperty.Create(nameof(Url), typeof(string), typeof(HyperlinkSpan), null);

    public string Url
    {
        get { return (string)GetValue(UrlProperty); }
        set { SetValue(UrlProperty, value); }
    }

    public HyperlinkSpan()
    {
        TextDecorations = TextDecorations.Underline;
        TextColor = Colors.Blue;
        GestureRecognizers.Add(new TapGestureRecognizer
        {
            // Launcher.OpenAsync is provided by Essentials.
            Command = new Command(async () => await Launcher.OpenAsync(Url))
        });
    }
}

将其添加到MainPage. xaml:

<ContentPage ...
             xmlns:local="clr-namespace:MauiApp4"
             x:Class="MauiApp4.MainPage">
    <StackLayout>
        ...
        <Label>
            <Label.FormattedText>
                <FormattedString>
                    <Span Text="Alternatively, click " />
                    <local:HyperlinkSpan Text="here"
                                         Url="https://learn.microsoft.com/dotnet/" />
                    <Span Text=" to view .NET documentation." />
                </FormattedString>
            </Label.FormattedText>
        </Label>
    </StackLayout>
</ContentPage>

这是effect.

Csharp相关问答推荐

我如何才能获得被嘲笑班级的私有成员?

自定义JsonEditor,用于将SON序列化为抽象类

EF Core判断是否应用了AsSplitQuery()

MongoDB将JS查询转换为C#的问题

如果存在对CodeAnalysis.CSharp的引用,则不能引用netStandard2.0库

Blazor. NET 8—阶段启动配置文件不启动网站VS2022

可为空的泛型属性

实体框架核心中的ComplexType和OwnsOne有什么不同?

Blazor Web App WASM的两个独立项目令人困惑

如何让两个.NET版本不兼容的项目对话?

如何避免在.NET中将日志(log)写入相对路径

未在Windows上运行的Maui项目

从Base64转换为不同的字符串返回相同的结果

将J数组转换为列表,只保留一个嵌套的JToken

如何在C#.NET桌面应用程序中动态更改焦点工具上的后退 colored颜色

ASP.NET核心8:app.UseStaticFiles()管道执行顺序

使用免费的DotNet库从Azure函数向Azure文件共享上的现有Excel文件追加行

使用SQL Server 2022+时,我是否必须将代码从SqlConnection类对象中迁移出来?

Excel将';@';添加到具有范围的公式中

如何根据分割文本的块数来计算文本的大小?