Pass ICommand Action through MVXValueConverter in MVVVMCross with Binding in iOS-Android

135 Views Asked by At

I am creating a Cross Platform application in Xamarin MVVMCross in which I am using an Attributed Text for showing a clickable link on some part of a text.

I have created my custom class with MvxValueConverter and applied a Binding in my iOS class.

Below is my code.

public class FreeShippingConverter : MvxValueConverter<string, NSAttributedString>
    {
        protected override NSAttributedString Convert(string value, Type targetType, object parameter, CultureInfo culture)
        {
            var result = new NSMutableAttributedString(value);
            var startIndex = value.IndexOf(Strings.Test, StringComparison.InvariantCultureIgnoreCase);
            if (startIndex < 0)
            {
                return result;
            }

            var range = new NSRange(startIndex, Strings.Test.Length);
            result.AddAttribute(UIStringAttributeKey.Font, TextStyle.B2Bold.Font(), range);
            result.AddAttribute(UIStringAttributeKey.ForegroundColor, Colors.Bluescale4.ToNativeColor(), range);
            return result;
        }
    }

Below is the Binding code from TableViewCell.

this.CreateBinding(FreeShippingText).For(v => v.AttributedText).To((FulfilmentOptionsCellViewModel vm) => vm.FreeShippingText).WithConversion<FreeShippingConverter>().Apply();

Output

Now I want to create an Action when user clicks on "Perks Members".

How to bind that in iOS and Android?

Please help me.

1

There are 1 best solutions below

2
Jianwei Sun - MSFT On

Can this achieve your need?

Create a VM class:

namespace Forms1.ViewModels
{
    public class VM
    {
        public ICommand SomeActionsCommand { get; set; }
        void SomeActions()
        {
            Console.WriteLine("some actions");
        }
        public VM()
        {
            SomeActionsCommand = new Command(SomeActions);
        }
    }
}

In your MainPage.xaml:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:vm="clr-namespace:Forms1.ViewModels"
             x:Class="Forms1.MainPage">
    <ContentPage.BindingContext>
        <vm:VM/>
    </ContentPage.BindingContext>


   <StackLayout Padding="10,10">
        <Label LineBreakMode="WordWrap" FontSize="Large" VerticalOptions="CenterAndExpand">
            <Label.FormattedText>
                <FormattedString>
                    <Span Text="FREE Shipping For " TextColor="#465978" />
                    <Span Text="Perks Members" TextColor="#2d3f62" FontAttributes="Bold"  FontSize="Large">
                        <Span.GestureRecognizers>
                            <TapGestureRecognizer Command="{Binding SomeActionsCommand}" />
                        </Span.GestureRecognizers>
                    </Span>
                </FormattedString>
            </Label.FormattedText>
        </Label>
    </StackLayout>


</ContentPage>

And here is the effect.