我正在 Xamarin MVVMCross 中创建一个跨平台应用程序,其中我使用属性文本在文本的某些部分显示可单击的链接。
我已经使用 MvxValueConverter 创建了自定义类,并在我的 iOS 类中应用了 Binding。
下面是我的代码。
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;
}
}
下面是 TableViewCell 的绑定代码。
this.CreateBinding(FreeShippingText).For(v => v.AttributedText).To((FulfilmentOptionsCellViewModel vm) => vm.FreeShippingText).WithConversion<FreeShippingConverter>().Apply();
现在我想在用户单击“Perks Members”时创建一个操作。
iOS和Android如何绑定?
请帮助我。
这能达到你的需求吗?
创建VM类:
namespace Forms1.ViewModels
{
public class VM
{
public ICommand SomeActionsCommand { get; set; }
void SomeActions()
{
Console.WriteLine("some actions");
}
public VM()
{
SomeActionsCommand = new Command(SomeActions);
}
}
}
在您的 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>
这是效果。