私はxamarinフォームでスイッチ要素をカスタマイズするためのエフェクトを作成しますが、15行目でエラー(指定されたキャストは無効です)が見つかりました。私のXAMLファイルがこれです 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ファイルに追加します。
<?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ファイルで編集しました。 –