HyperlinkButton in C# XAMARIN.FORMS

后端 未结 7 1506
南方客
南方客 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: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:

    
    
    
    
    

提交回复
热议问题