简体   繁体   English

HTML 内的可点击超链接 Label Xamarin.Forms

[英]Clickable HTML hyperlink within Label Xamarin.Forms

I'm trying to make HTML hyperlinks within a xaml label clickable.我试图在 xaml label 中创建 HTML 超链接可点击。 I have created classes which take a string containing HTML from an API, and renderer it correctly as HTML inside of a custom label renderer in both Android and iOS. To understand my dilemma better, its necessary to explain the extent of my use.我创建的类从 API 中获取包含 HTML 的字符串,并在 Android 和 iOS 中的自定义 label 渲染器中将其正确渲染为 HTML。为了更好地理解我的困境,我有必要更好地解释它的使用。

Inside my app, I have a messaging component that simply uses an API to post and get conversations from a website which is used primarily for better helping our users since our office is closed due to the coronavirus.在我的应用程序中,我有一个消息传递组件,它仅使用 API 来发布和从网站获取对话,该网站主要用于更好地帮助我们的用户,因为我们的办公室因冠状病毒而关闭。 The messaging center on the site allows for complex text formatting like a web forums post may, such as bold, italics, hyperlinks, etc.站点上的消息中心允许复杂的文本格式,如 web 论坛帖子可能,例如粗体、斜体、超链接等。

In order to display this correctly on mobile, I had to create a custom label renderer that will take the string with HTML tags and display it correctly as the body of the message.为了在移动设备上正确显示它,我必须创建一个自定义 label 渲染器,它将使用带有 HTML 标签的字符串并将其正确显示为消息正文。 This was a simple enough task.这是一项非常简单的任务。

The issue becomes that the messages may sometimes have hyperlinks, which currently display correct as links, but when a user tries to click on a link the list item is what registers the tap, not the label, nor the html link within the label.问题是消息有时可能有超链接,目前显示正确的链接,但当用户试图点击链接时,列表项是注册点击的内容,而不是 label,也不是 label 中的 html 链接。

The expected result is that when a user clicks on the hyperlink within the label, within the list, it will open that hyperlink in the devices default browser.预期结果是,当用户单击列表中 label 内的超链接时,它将在设备默认浏览器中打开该超链接。

I understand that one solution may be to parse the body of the message for any links, create a list of links, and then dynamically add spans to a label that each have their own gesture recognizers, but this won't be possible for me due to the hyperlinks being at times midway through a conversation.我知道一种解决方案可能是解析任何链接的消息正文,创建链接列表,然后将跨度动态添加到每个都有自己的手势识别器的 label,但这对我来说是不可能的,因为超链接有时会在对话中途出现。 An example of a messages body may be:消息正文的示例可能是:

'Hello user , please go to this link to login to your account.' '你好用户,请 go到此链接登录到您的帐户。

I understand there are better ways to create a messaging app than this, but the primary purpose of the mobile side is for users to get easier access to responses from staff, and for simple chat responses to staff messages.我知道有比这更好的方法来创建消息传递应用程序,但移动端的主要目的是让用户更轻松地访问员工的回复,以及对员工消息的简单聊天回复。 Staff will use the site exclusively to respond to users, and may utilize the complex editor to better assist users.工作人员将专门使用该站点来响应用户,并可能利用复杂的编辑器更好地帮助用户。

Thank you.谢谢你。 EDIT:编辑:对话范例

In the above example, the link at the end is not clickable.在上面的例子中,末尾的链接是不可点击的。 When i click on the link, the whole list view item highlights as if that ui element is higher priority than the label within.当我单击链接时,整个列表视图项目突出显示,就好像该 ui 元素的优先级高于其中的 label。

Code associated:相关代码:

<ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Frame
                        Margin="5"
                        Padding="0"
                        BorderColor="LightGray"
                        CornerRadius="15"
                        HasShadow="False">
                        <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
                            <StackLayout
                                Padding="5"
                                BackgroundColor="#f5f5f5"
                                Orientation="Horizontal">
                                <Label
                                    Margin="5,2.5,5,2.5"
                                    BackgroundColor="Transparent"
                                    FontAttributes="Bold"
                                    FontSize="Small"
                                    HorizontalOptions="FillAndExpand"
                                    HorizontalTextAlignment="Start"
                                    Text="{Binding SentBy}"
                                    TextColor="Black"
                                    VerticalOptions="FillAndExpand" />
                                <Label
                                    Margin="5,2.5,5,2.5"
                                    BackgroundColor="Transparent"
                                    FontAttributes="Bold"
                                    FontSize="Small"
                                    HorizontalOptions="FillAndExpand"
                                    HorizontalTextAlignment="End"
                                    Text="{Binding Sent}"
                                    TextColor="Black"
                                    VerticalOptions="FillAndExpand" />
                            </StackLayout>
                            <StackLayout Padding="5" Orientation="Horizontal">
                                <Label
                                    Margin="2.5"
                                    BackgroundColor="Transparent"
                                    FontSize="Small"
                                    HorizontalOptions="FillAndExpand"
                                    HorizontalTextAlignment="Start"
                                    Text="{Binding Body}"
                                    TextColor="Black"
                                    TextType="Html"
                                    VerticalOptions="FillAndExpand" />
                            </StackLayout>
                        </StackLayout>
                    </Frame>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>

Reposting/edit my previous answer as it got removed for incompleteness.重新发布/编辑我之前的答案,因为它因不完整而被删除。

This should be of some help:这应该有一些帮助:

Showing part of a label as a clickable link, has been a long desired feature of Xamarin.Forms.将 label 的一部分显示为可点击链接,一直是 Xamarin.Forms 的长期期望功能。 With the release of 3.2.0, this is now possible.随着 3.2.0 的发布,这成为可能。

Tappable Span可点击范围

Create a Label, with a FormattedText property and spans as shown below.创建一个 Label,具有 FormattedText 属性和跨度,如下所示。 For those not familar, labels, have a property called FormattedText.对于那些不熟悉的人,标签有一个名为 FormattedText 的属性。 It allows you to split text into separate sections, so you can format each of them differently.它允许您将文本拆分为单独的部分,因此您可以对每个部分进行不同的格式化。

<Label HorizontalOptions="Center"
       VerticalOptions="CenterAndExpand">
    <Label.FormattedText>
        <FormattedString>
            <Span Text="Hello " />
            <Span Text="Click Me!"
                  TextColor="Blue"
                  TextDecorations="Underline">
                <Span.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding ClickCommand}"
                                          CommandParameter="https://xamarin.com" />
                </Span.GestureRecognizers>
            </Span>
            <Span Text=" Some more text." />
        </FormattedString>
    </Label.FormattedText>
</Label>

In your ViewModel, add the following Command.在您的 ViewModel 中,添加以下命令。

public ICommand ClickCommand => new Command<string>((url) =>
{
    Device.OpenUri(new System.Uri(url));
});

This will result in a label, with Click Me.这将产生 label,点击我。 text highlighted, Tapping the text.文本突出显示,点击文本。 will result in the browser opening and going to xamarin.com.将导致浏览器打开并转到 xamarin.com。

Reference: https://xamarinhelp.com/hyperlink-in-xamarin.forms-label/参考: https://xamarinhelp.com/hyperlink-in-xamarin.forms-label/

I'm trying to make HTML hyperlinks within a xaml label clickable.我正在尝试使 xaml label 中的 HTML 超链接可点击。 I have created classes which take a string containing HTML from an API, and renderer it correctly as HTML inside of a custom label renderer in both Android and iOS. I have created classes which take a string containing HTML from an API, and renderer it correctly as HTML inside of a custom label renderer in both Android and iOS. To understand my dilemma better, its necessary to explain the extent of my use.为了更好地理解我的困境,有必要解释我的使用程度。

Inside my app, I have a messaging component that simply uses an API to post and get conversations from a website which is used primarily for better helping our users since our office is closed due to the coronavirus.在我的应用程序中,我有一个消息传递组件,它仅使用 API 从网站发布和获取对话,该网站主要用于更好地帮助我们的用户,因为我们的办公室因冠状病毒而关闭。 The messaging center on the site allows for complex text formatting like a web forums post may, such as bold, italics, hyperlinks, etc.网站上的消息中心允许复杂的文本格式,如 web 论坛帖子可能,如粗体、斜体、超链接等。

In order to display this correctly on mobile, I had to create a custom label renderer that will take the string with HTML tags and display it correctly as the body of the message.为了在移动设备上正确显示此内容,我必须创建一个自定义 label 渲染器,该渲染器将获取带有 HTML 标签的字符串并将其正确显示为消息正文。 This was a simple enough task.这是一个足够简单的任务。

The issue becomes that the messages may sometimes have hyperlinks, which currently display correct as links, but when a user tries to click on a link the list item is what registers the tap, not the label, nor the html link within the label.问题是消息有时可能具有超链接,当前显示为正确的链接,但当用户尝试单击链接时,列表项是注册点击的内容,而不是 label,也不是 ZD304BA20E96D80EEEABAC581 中的 html 链接。

The expected result is that when a user clicks on the hyperlink within the label, within the list, it will open that hyperlink in the devices default browser.预期结果是,当用户单击列表中 label 内的超链接时,它将在设备默认浏览器中打开该超链接。

I understand that one solution may be to parse the body of the message for any links, create a list of links, and then dynamically add spans to a label that each have their own gesture recognizers, but this won't be possible for me due to the hyperlinks being at times midway through a conversation.我知道一种解决方案可能是解析任何链接的消息正文,创建链接列表,然后动态地将跨度添加到 label 中,每个都有自己的手势识别器,但这对我来说是不可能的有时超链接会出现在对话的中间。 An example of a messages body may be:消息正文的示例可能是:

'Hello user , please go to this link to login to your account.' '您好用户,请 go到此链接登录您的帐户。

I understand there are better ways to create a messaging app than this, but the primary purpose of the mobile side is for users to get easier access to responses from staff, and for simple chat responses to staff messages.我知道创建消息传递应用程序有比这更好的方法,但移动端的主要目的是让用户更容易地访问员工的回复,以及对员工消息的简单聊天回复。 Staff will use the site exclusively to respond to users, and may utilize the complex editor to better assist users.工作人员将专门使用该网站来回应用户,并可能利用复杂的编辑器来更好地帮助用户。

Thank you.谢谢你。 EDIT:编辑:对话示例

In the above example, the link at the end is not clickable.在上面的例子中,最后的链接是不可点击的。 When i click on the link, the whole list view item highlights as if that ui element is higher priority than the label within.当我单击链接时,整个列表视图项突出显示,好像该 ui 元素的优先级高于 label 内的优先级。

Code associated:相关代码:

<ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Frame
                        Margin="5"
                        Padding="0"
                        BorderColor="LightGray"
                        CornerRadius="15"
                        HasShadow="False">
                        <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
                            <StackLayout
                                Padding="5"
                                BackgroundColor="#f5f5f5"
                                Orientation="Horizontal">
                                <Label
                                    Margin="5,2.5,5,2.5"
                                    BackgroundColor="Transparent"
                                    FontAttributes="Bold"
                                    FontSize="Small"
                                    HorizontalOptions="FillAndExpand"
                                    HorizontalTextAlignment="Start"
                                    Text="{Binding SentBy}"
                                    TextColor="Black"
                                    VerticalOptions="FillAndExpand" />
                                <Label
                                    Margin="5,2.5,5,2.5"
                                    BackgroundColor="Transparent"
                                    FontAttributes="Bold"
                                    FontSize="Small"
                                    HorizontalOptions="FillAndExpand"
                                    HorizontalTextAlignment="End"
                                    Text="{Binding Sent}"
                                    TextColor="Black"
                                    VerticalOptions="FillAndExpand" />
                            </StackLayout>
                            <StackLayout Padding="5" Orientation="Horizontal">
                                <Label
                                    Margin="2.5"
                                    BackgroundColor="Transparent"
                                    FontSize="Small"
                                    HorizontalOptions="FillAndExpand"
                                    HorizontalTextAlignment="Start"
                                    Text="{Binding Body}"
                                    TextColor="Black"
                                    TextType="Html"
                                    VerticalOptions="FillAndExpand" />
                            </StackLayout>
                        </StackLayout>
                    </Frame>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM