[英].Net MAUI how to include a link in a Label
Using Visual Studio Community edition 2022.使用 Visual Studio 社区版 2022。
New to.Net MAUI and am trying to follow the examples provided in the documentation found at https://docs.microsoft.com/en-us/dotnet/maui/user-interface/controls/label .Net MAUI 的新手,我正在尝试遵循https://docs.microsoft.com/en-us/dotnet/maui/user-interface/controls/label中的文档中提供的示例
The XAML code below (from the docs) is used to create a "link" using a tap recognizer:下面的 XAML 代码(来自文档)用于使用点击识别器创建“链接”:
<Label>
<Label.FormattedText>
<FormattedString>
<Span
Text="Link: "
/>
<Span
Text="click here to open the docs"
TextColor="Blue"
TextDecorations="Underline">
<Span.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding OpenUrlCommand}"
CommandParameter="https://docs.microsoft.com/dotnet/maui/"
/>
</Span.GestureRecognizers>
</Span>
</FormattedString>
</Label.FormattedText>
</Label>
However, the command (OpenUrlCommand) is never called - tested using the Windows and Andriod emulators.但是,从未调用命令 (OpenUrlCommand) - 使用 Windows 和 Andriod 仿真器进行了测试。
In my ViewModel, I define the OpenUrlCommand as follows:在我的 ViewModel 中,我将 OpenUrlCommand 定义如下:
public ICommand OpenUrlCommand => new Command<string>( async ( url ) => await Launcher.OpenAsync( url ) );
... but nothing works, I also tried the following -- no go... ...但是没有任何效果,我还尝试了以下方法-没有 go ...
public ICommand OpenUrlCommand => new Command<string>( async ( url ) => await Browser.OpenAsync( url ) );
... and then tried the following -- again, no luck... ......然后尝试了以下 - 再次,没有运气......
public IRelayCommand OpenUrlCommand => new RelayCommand<string>( async ( url ) => await Launcher.OpenAsync( url ) );
I then replaced the call to open the url with a Debug.WriteLine( url ), and it seems that the OpenUrlCommand is never being called.然后我用 Debug.WriteLine( url ) 替换了打开 url 的调用,似乎从未调用过 OpenUrlCommand。 Also, one would expect the cursor to change when hovering over the "link" text, but it never does -- it is as though the TapGesterRecognizer is not being created or registered.
此外,人们会期望 cursor 在将鼠标悬停在“链接”文本上时会发生变化,但它从来没有发生过——就好像没有创建或注册 TapGesterRecognizer。
As a last ditch effort, I also tried the following (again, copying from the docs):作为最后的努力,我还尝试了以下方法(再次,从文档中复制):
<Label TextType="Html">
<![CDATA[
This is <a href="https://docs.microsoft.com/dotnet/maui/" target="_blank">a link to another site</a>.
]]>
</Label>
... no luck -- the link appears underlined, but when clicked on, nothing happens (cursor does not change, browser does not open). ... 不走运——该链接带有下划线,但是当点击时,什么也没有发生(光标没有改变,浏览器没有打开)。
Any advice, or even better, a working example would be appreciated.任何建议,甚至更好的工作示例将不胜感激。
UPDATE: Found this post as well: https://github.com/dotnet/maui/issues/4734 - which looks like this was a known bug back in February!更新:也找到了这篇文章: https://github.com/dotnet/maui/issues/4734 - 看起来这是二月份的一个已知错误!
As per @Jessie Zhang -MSFT, I ended up doing the following - which underlines the link, but with the caveat that the WHOLE label is tapable...根据@Jessie Zhang -MSFT,我最终做了以下事情 - 强调了链接,但需要注意的是整个 label 是可录音的......
<!--
This works - HOWEVER, the WHOLE label is tappable. We cannot use
Span.GestureRecognizers because of the following issue:
https://github.com/dotnet/maui/issues/4734
-->
<Label>
<Label.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding OpenUrlCommand}"
CommandParameter="https://docs.microsoft.com/en-us/dotnet/maui/"
/>
</Label.GestureRecognizers>
<Label.FormattedText>
<FormattedString>
<Span
Text="To learn more, "
/>
<Span
Text="check the documentation"
TextColor="Blue"
TextDecorations="Underline">
</Span>
<Span
Text="."
/>
</FormattedString>
</Label.FormattedText>
</Label>
... and in my ViewModel: ...在我的 ViewModel 中:
public IRelayCommand OpenUrlCommand => new RelayCommand<String>( launch_browser );
private async void launch_browser( String url )
{
Debug.WriteLine( $"*** Tap: {url}" );
await Browser.OpenAsync( url );
}
... the above works (for me) in the Windows and Andriod simulators. ...上述工作(对我来说)在 Windows 和 Andriod 模拟器中。
Yes, it is a known issue about this problem.是的,这是关于此问题的已知问题。
You can follow it here: https://github.com/dotnet/maui/issues/4734 .你可以在这里关注它: https://github.com/dotnet/maui/issues/4734 。
But as a workaround, you can use Label.GestureRecognizers
instead of Span.GestureRecognizers
.但作为一种解决方法,您可以使用
Label.GestureRecognizers
而不是Span.GestureRecognizers
。
For example:例如:
<Label
Text="click here"
VerticalOptions="Center"
HorizontalOptions="Center" >
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding TapCommand}"
CommandParameter="https://docs.microsoft.com/dotnet/maui/" />
</Label.GestureRecognizers>
</Label>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.