HyperlinkButton in C# XAMARIN.FORMS

后端 未结 7 1482
南方客
南方客 2021-02-19 02:36

I want to create Label with click possibility like in WIN phone xaml


Is there a possibili

相关标签:
7条回答
  • 2021-02-19 02:56

    I do this --> create the following class:

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

    Then everywhere you need to create a link button use this:

    SendButton = new ButtonAsLink()
            {
                Text = "Send Logs...",
                VerticalOptions = LayoutOptions.Center
            };
    
    0 讨论(0)
  • 2021-02-19 02:57

    If you want stylish the button (make it like hyperlink - with underline) you would like to implement the custom renderer:

    Add this to xamarin.project

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

    And implementation the renderer to IOS project:

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

    I added renderer for iOS only. If you want to support another versions - you have to add renderers to this platforms.

    And use it at xcml like this:

    <ContentPage ...
    xmlns:control="clr-namespace:xamarin.Mobile.Controls">
    
    <control:HyperlinkButton Text="Cancel" Clicked="CancelClicked"/>
    </ContentPage>
    
    0 讨论(0)
  • 2021-02-19 03:05

    This is a class I use to have a label act as a 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)) });
        }
    }
    

    For more options, this answer about creating a hyperlink in Xamarin Forms might be useful.

    0 讨论(0)
  • 2021-02-19 03:10

    I would suggest using GestureRecognizers and adding a Tap Gesture to a label. Ref: here

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

    GestureRecognizer is a public property on the View class which Label inherits from. See here

    0 讨论(0)
  • 2021-02-19 03:11

    Yes, you can either use Button Clicked or TapGestureRecognizer. If you want to redirect to a site you can use WebView. If you want to direct to your own Native page: If you are using NavigationPage, you can use Navigation.PushAsync(new Page()); If your not using NavigationPage and would like to change MainPage: App.Current.MainPage = new MyContentPage();

    0 讨论(0)
  • 2021-02-19 03:14

    I'd take a much more standard approach and use a Button. Just set the background to match your app background and remove the border. Then there's no need for extra TapGestureRecongniser code. (Pseudo Code below:)

    Xaml:

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

    Code-Behind:

    void OnButtonClicked(object sender, EventArgs args)
    {
        //Open your link in here
    }
    
    0 讨论(0)
提交回复
热议问题