Bobisback

Badges

5 LikesFirst Anniversary10 CommentsName DropperPhotogenicFirst Comment

About

Username
Bobisback
Location
US
Joined
Visits
262
Last Active
Roles
University
Points
30
Badges
6

Bobisback ✭✭

About

Username
Bobisback
Location
US
Joined
Visits
262
Last Active
Roles
University
Points
30
Badges
6
  • Re: Need to put HTML into a Label

    @MaximeLefebvre said:
    Thank you ! Here is my implementation if someone has the same problem

    For completeness here is the iOS renderer for the HtmlLabel posted by MaximeLefebvre

    using System.ComponentModel;
    using Foundation;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.iOS;
    
    [assembly: ExportRenderer(typeof(HtmlLabel), typeof(HtmlLabelRenderer))]
    
    namespace YourNamespace.iOS
    {
        public class HtmlLabelRenderer : LabelRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
            {
                base.OnElementChanged(e);
    
                if (Control != null && Element != null && !string.IsNullOrWhiteSpace(Element.Text))
                {
                    var attr = new NSAttributedStringDocumentAttributes();
                    var nsError = new NSError();
                    attr.DocumentType = NSDocumentType.HTML;
    
                    var myHtmlData = NSData.FromString(Element.Text, NSStringEncoding.Unicode);
                    Control.Lines = 0;
                    Control.AttributedText = new NSAttributedString(myHtmlData, attr, ref nsError);
                }
            }
    
            protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                base.OnElementPropertyChanged(sender, e);
    
                if (e.PropertyName == Label.TextProperty.PropertyName)
                {
                    if (Control != null && Element != null && !string.IsNullOrWhiteSpace(Element.Text))
                    {
                        var attr = new NSAttributedStringDocumentAttributes();
                        var nsError = new NSError();
                        attr.DocumentType = NSDocumentType.HTML;
    
                        var myHtmlData = NSData.FromString(Element.Text, NSStringEncoding.Unicode);
                        Control.Lines = 0;
                        Control.AttributedText = new NSAttributedString(myHtmlData, attr, ref nsError);
                    }
                }
            }
        }
    }
    

    Hope this helps someone.

    Thanks,
    Bob

  • Re: TableView - custom ViewCell - Command property

    So I just got this to work. As a refreance for everyone else searching the web below is the code to create a command bindable property in xamarin forms in a ViewCell. In other words I have a custom Cell that I wanted to have a command property on.

    `class MainMenuItemCell : ViewCell
    {
    public static readonly BindableProperty CommandProperty =
    BindableProperty.Create<MainMenuItemCell, ICommand>(x => x.Command, null);

    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }
    
    public MainMenuItemCell()
    {
        //put all your super cool layout stuff here
        var viewLayout = new StackLayout();
    
        var gestureRecognizer = new TapGestureRecognizer();
    
        gestureRecognizer.Tapped += (s, e) => {
            if (Command != null && Command.CanExecute(null)) {
                Command.Execute(null);
            }
        };
    
        //You can either set the binding here or you can set the binding in the xaml. You only need one or the other.
        //Xaml for this is below
        SetBinding(CommandProperty, new Binding("ItemSelectedCommand"));
    
        viewLayout.GestureRecognizers.Add(gestureRecognizer);
    
        View = viewLayout;
    }
    

    }`

    Here is the Xaml I used

    <ListView x:Name="MainMenuListView" StyleId="MainMenuListView" ItemsSource="{x:Static local:MainMenuViewModel.MainMenuItems}" BackgroundColor="Transparent" SeparatorVisibility="None"> <ListView.ItemTemplate> <DataTemplate> <views:MainMenuItemCell Command="{Binding ItemSelectedCommand}"/> </DataTemplate> </ListView.ItemTemplate> </ListView>

    Hope this helps someone in there search for this!!

    See ya all,
    Sean