c# - Xamarin.Forms dynamic button styling -
i'd implement own custom button style. style has change, whenever button changes state (from enabled disabled i.e.).
my current solution define style in android , custom buttonrenderer applies style button.
button_style.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" > <shape> <solid android:color="#d7d7d7" /> <stroke android:width="1dp" android:color="#d7d7d7" /> <corners android:radius="6dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> <item android:state_enabled="false"> <shape> <solid android:color="#efefef" /> <stroke android:width="1dp" android:color="#efefef" /> <corners android:radius="6dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> <item> <shape> <gradient android:startcolor="#d7d7d7" android:endcolor="#d7d7d7" android:angle="270" /> <stroke android:width="1dp" android:color="#d7d7d7" /> <corners android:radius="6dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> </selector>
custom renderer:
using frameworkforms.usercontrol; using xamarin.forms; using xamarin.forms.platform.android; using color = android.graphics.color; [assembly: exportrenderer(typeof(custombutton), typeof(frameworkforms.droid.renderer.customizedbuttonrenderer))] namespace frameworkforms.droid.renderer { public class customizedbuttonrenderer : buttonrenderer { protected override void onelementchanged(elementchangedeventargs<button> e) { base.onelementchanged(e); if (control != null) { control.setbackgroundresource(resource.drawable.button_style); control.setallcaps(false); if (!control.enabled) { control.settextcolor(color.parsecolor("#858585")); } } } } }
there binding on buttons enabled property. problem pretty obvious, once property changes, button doesn't renderered again , keeps textcolor of previous state.
i've done little research styles in xamarin.forms. seems dynamic styling should me, i'd have buttons (like global style). can somehow combine them? or better solution? worth mentioning, don't mind implementing whole thing ios again. it's gonna different anyways. android xml-styles too.
thanks.
if want change when isenabled
property changes, need override onelementpropertychanged
:
protected override void onelementpropertychanged(object sender, propertychangedeventargs e) { base.onelementpropertychanged(sender, e); if(e.propertyname == "isenabled") { if(element.isenabled) { control.settextcolor(color.parsecolor("#858585")); control.setbackgroundresource(resource.drawable.yourenabledresource); } else { control.settextcolor(element.textcolor.toandroid()); control.setbackgroundresource(resource.drawable.yourdisabledresource); } } }
then can create 2 drawable xml files define different resources enabled , disabled. titled them yourenabledresource
, yourdisabledresource
accordingly.
Comments
Post a Comment