【问题标题】:Create a hyperlink using Xamarin.Forms (xaml and c#)使用 Xamarin.Forms(xaml 和 c#)创建超链接
【发布时间】:2016-10-02 13:10:56
【问题描述】:

我基本上想使用标签类在 Xamarin.Forms 中创建一个超链接。基本上,我想通过以下链接在网络浏览器中将用户带到 google.com:

<Label Text="http://www.google.com/" />

我在 Xamarin Forms API 中找不到任何关于此的内容,并且 Internet 在 Xamarin.Forms 中有关此主题的信息模糊且有限。

这可能吗?如果是这样,有人可以指出我正确的方向吗?提前感谢任何回答的人。

【问题讨论】:

标签: c# xaml xamarin xamarin.ios xamarin.forms


【解决方案1】:

您不能真正做到这一点,因为默认情况下标签不会响应用户输入,但您可以通过手势实现类似的功能

using Xamarin.Forms;
using Xamarin.Essentials;

Label label = new Label();
label.Text = "http://www.google.com/";

var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += async (s, e) => {
    // Depreciated - Device.OpenUri( new Uri((Label)s).Text); 
    await Launcher.OpenAsync(new Uri(((Label)s).Text));
};
label.GestureRecognizers.Add(tapGestureRecognizer);

【讨论】:

  • 非常感谢!我需要做一个小的更正:实际上,Device.OpenUri(new Uri(((Label)s).Text)); new Uri(uriString) 构造函数对于将字符串初始化为 Uri 是必需的。谢谢你的改变,它起作用了!
  • 由于 OpenUri 已被弃用,您现在应该写 Launcher.OpenAsync(new Uri((s as Label).Text));
  • @Adriano 谢谢 - 我已经提交了反映这一点的编辑
【解决方案2】:

我做了这个小类来处理它:

public class SimpleLinkLabel : Label
{
    public SimpleLinkLabel(Uri uri, string labelText = null)
    {
        Text = labelText ?? uri.ToString();
        TextColor = Color.Blue;
        GestureRecognizers.Add(new TapGestureRecognizer { Command = new Command(() => Device.OpenUri(uri)) });
    }
}

如果你也想强调它,还需要更多的参与:

public class LinkLabel : StackLayout
{
    private SimpleLinkLabel label;

    public LinkLabel(Uri uri, string labelText = null, bool underlined = true)
    {
        // Remove bottom padding
        Padding = new Thickness(Padding.Left, Padding.Top, Padding.Right, 0);
        VerticalOptions = LayoutOptions.Center;

        Children.Add(label = new SimpleLinkLabel(uri, labelText));

        if (underlined)
            Children.Add(new BoxView { BackgroundColor = Color.Blue, HeightRequest = 1, Margin = new Thickness(0, -8, 0, 0) });
    }

    public TextAlignment HorizontalTextAlignment { get { return label.HorizontalTextAlignment; } set { label.HorizontalTextAlignment = value; } }
}

受这篇文章启发的后一课:how to underline in xamarin forms


编辑:XLabs 也有 HyperLinkLabel

【讨论】:

    【解决方案3】:

    使用按钮和 xamarin.forms.theme nuget

    <Button StyleClass = "Link"/>
    

    https://developer.xamarin.com/guides/xamarin-forms/user-interface/themes/light/

    【讨论】:

      【解决方案4】:

      如果您想使用 MVVM 执行此操作,您可以创建一个带有蓝色文本颜色的标签和一个 GestureRecognizers 以将其连接到命令:

      <Label TextColor="Blue" Text="{Binding Model.LinkDescription}">
          <Label.GestureRecognizers>
              <TapGestureRecognizer Command="{Binding ClickCommand}" CommandParameter="{Binding Model.LinkURL}"/>
          </Label.GestureRecognizers>
      </Label>
      

      在您的 ViewModel 中,您可以使用 Xamarin Essentials Nuget 包启动默认浏览器:

      private Boolean IsValidUri(String uri)
      {
          try
          {
              new Uri(uri);
              return true;
          }
          catch
          {
              return false;
          }
      }
      
      public ICommand ClickCommand => new Command<string>(async (url) =>
      {
          try
          {
              if (!string.IsNullOrEmpty(url))
              {
                  if (!url.Trim().StartsWith("http", StringComparison.OrdinalIgnoreCase))
                  {
                      url = "http://" + url;
                  }
                  if (IsValidUri(url))
                  {
                      await Browser.OpenAsync(new Uri(url), BrowserLaunchMode.SystemPreferred);
                  }
              }
          }
          catch(Exception ex)
          {
              Debug.WriteLine(ex.Message);
          }
      
      });
      

      【讨论】:

        【解决方案5】:
        <Label LineBreakMode="WordWrap">
            <Label.FormattedText>
                <FormattedString>
                    <Span Text="Google">
                        <Span.GestureRecognizers>
                            <TapGestureRecognizer Tapped="Handle_Tapped" />
                        </Span.GestureRecognizers>
                    </Span>
                </FormattedString>
            </Label.FormattedText>
        </Label>
        
        public async void Handle_Tapped(object sender, EventArgs e)
        {
            await Browser.OpenAsync(new Uri(url), BrowserLaunchMode.SystemPreferred);
        }
        

        【讨论】:

          猜你喜欢
          • 2016-04-16
          • 2011-10-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-04-21
          • 2013-10-27
          • 1970-01-01
          • 2010-10-06
          相关资源
          最近更新 更多