c# - Xamarin Forms Effect -


i create effect customize switch element in xamarin forms found error (specified cast not valid) in line 15. idea change this? class bellow

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 method if wish reset control original state     } } 

my xaml file this, effect add in xaml file:

<?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> 

probably control instance of android.support.v7.widget.switchcompat instead of android.widget.switch. default android mainactivity inherits global::xamarin.forms.platform.android.formsappcompatactivity when start new xamarin forms project. in case, xaml switch become switchcompat instead of android.widget.switch.

you can find out type of control looking @ inspector in visual studio or xamarin studio. change line 15 var toggle = (android.support.v7.widget.switchcompat)control; if control has switchcompat type.

since got error, expect did following, complete:

keep in mind need resolutiongroupname assembly attribute in android project, , routingeffect in pcl/shared project in order reference effect correctly. see https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/effects/creating/ example of this.

for reference, android renderers create native control, based on mainactivity base class:


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -