我创建了一个效果来自定义xamarin窗体中的开关元素,但我在第15行发现了错误(指定的转换无效)。有什么想法来改变它?我班波纹管Xamarin窗体效果
using Android.Graphics;
using Android.Widget;
using Puuber.Droid.Platform;
using Xamarin.Forms.Platform.Android;
[assembly: Xamarin.Forms.ExportEffect(typeof(PinkSwitchEffect), "PinkSwitchEffect")]
namespace Puuber.Droid.Platform
{
class PinkSwitchEffect: PlatformEffect
{
protected override void OnAttached()
{
var toggle = (Android.Widget.Switch)Control;
Xamarin.Forms.Color gold = Xamarin.Forms.Color.FromHex("#fcb741");
toggle.TrackDrawable.SetColorFilter(new PorterDuffColorFilter(gold.ToAndroid(), PorterDuff.Mode.SrcIn));
}
protected override void OnDetached()
{
// Use this method if you wish to reset the control to original state
}
}
我的XAML文件是这样的,效果添加在XAML文件:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Puuber;assembly=Puuber"
x:Class="Puuber.Welcome">
<AbsoluteLayout BackgroundColor="#161616">
<Image Source="logo.png" AbsoluteLayout.LayoutBounds="0.5 ,0, 1, 0.4" AbsoluteLayout.LayoutFlags="XProportional,YProportional,WidthProportional, HeightProportional"/>
<Label x:Name="rangeText" TextColor="#ffffff" FontSize = "24" Text="5 Km" HorizontalTextAlignment="End" AbsoluteLayout.LayoutBounds="0.8 ,0.5, 0.25, 0.1" AbsoluteLayout.LayoutFlags="XProportional,YProportional,WidthProportional, HeightProportional" local:CustomFontEffect.FontFileName="OpenSans-Regular">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<OnPlatform.Android></OnPlatform.Android>
</OnPlatform>
</Label.FontFamily>
</Label>
<Slider x:Name="rangeSlider" Maximum="100" Minimum="5" ValueChanged="rangeSlider_ValueChanged" AbsoluteLayout.LayoutBounds="0.5 ,0.55, 0.8, 0.1" AbsoluteLayout.LayoutFlags="XProportional,YProportional,WidthProportional, HeightProportional">
<Slider.Effects>
<local:GoldSliderEffect/>
</Slider.Effects>
</Slider>
<Label Text="Mulheres" TextColor="#ffffff" FontSize = "24" HorizontalTextAlignment="Center" AbsoluteLayout.LayoutBounds="0.5 ,0.7, 0.5, 0.1" AbsoluteLayout.LayoutFlags="XProportional,YProportional,WidthProportional, HeightProportional" local:CustomFontEffect.FontFileName="OpenSans-Regular">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<OnPlatform.Android></OnPlatform.Android>
</OnPlatform>
</Label.FontFamily>
</Label>
<Switch IsToggled="{Binding isOn}" AbsoluteLayout.LayoutBounds="0.8 ,0.667, 0.15, 0.05" AbsoluteLayout.LayoutFlags="XProportional, YProportional, WidthProportional, HeightProportional">
<Switch.Effects>
<local:PinkSwitchEffect/>
</Switch.Effects>
</Switch>
<Label Text="Homens" TextColor="#ffffff" FontSize = "24" HorizontalTextAlignment="Center" AbsoluteLayout.LayoutBounds="0.5 ,0.80, 0.5, 0.1" AbsoluteLayout.LayoutFlags="XProportional,YProportional,WidthProportional, HeightProportional" local:CustomFontEffect.FontFileName="OpenSans-Regular">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<OnPlatform.Android></OnPlatform.Android>
</OnPlatform>
</Label.FontFamily>
</Label>
<Switch IsToggled="{Binding isOn}" AbsoluteLayout.LayoutBounds="0.8 ,0.767, 0.15, 0.05" AbsoluteLayout.LayoutFlags="XProportional, YProportional, WidthProportional, HeightProportional"/>
<Label Text="Travestis" TextColor="#ffffff" FontSize = "24" HorizontalTextAlignment="Center" AbsoluteLayout.LayoutBounds="0.5 ,0.9, 0.5, 0.1" AbsoluteLayout.LayoutFlags="XProportional,YProportional,WidthProportional, HeightProportional" local:CustomFontEffect.FontFileName="OpenSans-Regular">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<OnPlatform.Android></OnPlatform.Android>
</OnPlatform>
</Label.FontFamily>
</Label>
<Switch IsToggled="{Binding isOn}" AbsoluteLayout.LayoutBounds="0.8 ,0.867, 0.15, 0.05" AbsoluteLayout.LayoutFlags="XProportional, YProportional, WidthProportional, HeightProportional"/>
</AbsoluteLayout>
</ContentPage>
你能否把包含效果的代码放在控件中? – matthewrdev
立即使用xaml文件编辑。 –