【问题标题】:HyperlinkButton in C# XAMARIN.FORMSC# XAMARIN.FORMS 中的超链接按钮
【发布时间】:2016-04-16 19:59:56
【问题描述】:

我想像在 WIN 手机 xaml 中一样创建具有点击可能性的标签

<HyperlinkButton Content="My Text to click"/>

是否有可能在 Xamarin.Forms 中执行此操作?

我找到了这个但不一样:

https://github.com/XLabs/Xamarin-Forms-Labs/wiki/HyperLinkLabel

【问题讨论】:

    标签: c# xaml xamarin.forms


    【解决方案1】:

    我会采用更标准的方法并使用Button。只需设置背景以匹配您的应用背景并删除边框。那么就不需要额外的TapGestureRecongniser 代码了。 (下面是伪代码:)

    Xaml:

    <Button Text="Click Me!" Background= "YourAppBackground" BorderWidth="0" Clicked="OnButtonClicked" />
    

    代码隐藏:

    void OnButtonClicked(object sender, EventArgs args)
    {
        //Open your link in here
    }
    

    【讨论】:

    • 这种方法如何重定向到页面?
    • 我的意思是,创建事件处理程序是微不足道的,但你如何真正打开一个链接?
    【解决方案2】:

    我建议使用GestureRecognizers 并将Tap Gesture 添加到标签中。 Ref: here

    var label = new Label()
    {
      Text="My Hyperlink"
    };
    var tapGestureRecognizer = new TapGestureRecognizer();
    tapGestureRecognizer.Tapped += (s, e) => {
        // handle the tap
    };
    label.GestureRecognizers.Add(tapGestureRecognizer);
    

    GestureRecognizerView 类的公共属性,Label 继承自该类。见here

    【讨论】:

    • 这对于 Voiceover 等用户可能无法访问和显示。
    【解决方案3】:

    XAML 代码如下:

    <Label
        Text="My Text to click"
        HorizontalOptions="Center" >
    
        <Label.GestureRecognizers>
            <TapGestureRecognizer
                Tapped="OnLabelTapped"
                NumberOfTapsRequired="2" />
        </Label.GestureRecognizers>
    </Label>
    

    注意:默认情况下,NumberOfTapsRequired 为 1。

    然后在您的.cs 文件中,添加方法OnLabelTapped

    public void OnLabelTapped(object sender, EventArgs args)
    {
        // Your code here
        // Example:
        // DisplayAlert("Message", "You clicked on the label", "OK");
    }
    

    【讨论】:

      【解决方案4】:

      是的,您可以使用 Button Clicked 或 TapGestureRecognizer。 如果你想重定向到一个站点,你可以使用 WebView。 如果您想定向到您自己的 Native 页面: 如果您使用的是 NavigationPage,则可以使用 Navigation.PushAsync(new Page()); 如果您不使用 NavigationPage 并想更改 MainPage: App.Current.MainPage = new MyContentPage();

      【讨论】:

        【解决方案5】:

        这是我用来让标签充当链接的类:

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

        有关更多选项,关于creating a hyperlink in Xamarin Forms 的这个答案可能有用。

        【讨论】:

          【解决方案6】:

          我这样做 --> 创建以下类:

          public class ButtonAsLink : Button
          {
              public ButtonAsLink()
              {
                  this.TextColor = Color.Blue;
                  this.BackgroundColor = Color.Transparent;
                  this.BorderWidth = 0;
              }
          }
          

          然后在任何需要创建链接按钮的地方使用这个:

          SendButton = new ButtonAsLink()
                  {
                      Text = "Send Logs...",
                      VerticalOptions = LayoutOptions.Center
                  };
          

          【讨论】:

          • 使用这个,默认按钮样式是否仍然适用于 Windows 设备,例如悬停时的背景和边框变化?可以用简单的方式覆盖它们吗?
          【解决方案7】:

          如果你想要时尚的按钮(让它像超链接 - 带下划线)你想实现自定义渲染器:

          将此添加到 xamarin.project

          using Xamarin.Forms;
          
          namespace xamarin.Mobile.Controls
          {
              public class HyperlinkButton : Button
              {
                  public HyperlinkButton()
                  {
                  }
              }
          }
          

          并将渲染器实现到IOS项目:

          using System.ComponentModel;
          using xamarin.Mobile.IOS;
          using Foundation;
          using UIKit;
          using Xamarin.Forms;
          using Xamarin.Forms.Platform.iOS;
          
          [assembly: ExportRenderer(typeof(xamarin.Mobile.Controls.HyperlinkButton), typeof(HyperlinkButtonRenderer))]
          namespace xamarin.Mobile.IOS
          {
              public class HyperlinkButtonRenderer : ButtonRenderer
              {
                  protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
                  {
                      base.OnElementPropertyChanged(sender, e);
          
                      if (Control != null)
                      {
                          //Set attribute for underlineing information links
                          var underlineAttr = new UIStringAttributes { UnderlineStyle = NSUnderlineStyle.Single };
                          Control.SetAttributedTitle(new NSAttributedString(Control.CurrentTitle, underlineAttr), UIControlState.Normal);
                      }
                  }
              }
          }
          

          我只为 iOS 添加了渲染器。如果您想支持其他版本 - 您必须向此平台添加渲染器。

          并像这样在 xcml 中使用它:

          <ContentPage ...
          xmlns:control="clr-namespace:xamarin.Mobile.Controls">
          
          <control:HyperlinkButton Text="Cancel" Clicked="CancelClicked"/>
          </ContentPage>
          

          【讨论】:

            猜你喜欢
            • 2017-03-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-10-13
            • 2020-05-16
            • 1970-01-01
            相关资源
            最近更新 更多