C# 中的超链接按钮 XAMARIN.FORMS

HyperlinkButton in C# XAMARIN.FORMS

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

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

有可能在Xamarin.Forms做吗?

我找到了这个但是不一样:

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

我建议使用 GestureRecognizers 并向标签添加 Tap GestureRef: here

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

GestureRecognizerLabel 继承的 View class 上的 public 属性。参见 here

我会采用更标准的方法并使用 Button。只需将背景设置为与您的应用背景相匹配并移除边框即可。那么就不需要额外的 TapGestureRecongniser 代码了。 (伪代码如下:)

Xaml:

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

Code-Behind:

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

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

这是一个 class 我用标签充当 link:

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)) });
    }
}

有关 的回答可能会有用。

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");
}

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

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

然后在任何需要创建 link 按钮的地方使用:

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

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

将此添加到 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>