xaml - StaticResource into MarkupExtension -
i'm trying use static resource extension entry's fontsize
property. have code piece of code:
<?xml version="1.0" encoding="utf-8" ?> <contentpage x:class="project.sources.pages.extras.editprofilepage" xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:control="clr-namespace:project.sources.controls;assembly=project" xmlns:extension="clr-namespace:project.sources.extensions;assembly=project" xmlns:sys="clr-namespace:system;assembly=mscorlib"> <contentpage.resources> <resourcedictionary> <color x:key="nl_bluenight">#0e1728</color> <color x:key="nl_orangebeer">#e87e07</color> <color x:key="nl_orangesky">#bd4327</color> <color x:key="nl_white">#ececec</color> <sys:double x:key="entryfontsize">20</sys:double> </resourcedictionary> </contentpage.resources> <contentpage.content> <absolutelayout backgroundcolor="{x:staticresource nl_bluenight}"> <absolutelayout margin="{binding layoutthicknessadapter}" absolutelayout.layoutbounds="0.5, 0.5, 1, 1" absolutelayout.layoutflags="all" backgroundcolor="black"> <control:gif absolutelayout.layoutbounds="0.5, 0, 1, 0.9" absolutelayout.layoutflags="all" gifsource="gifs/loginbackground.gif" /> <boxview absolutelayout.layoutbounds="0.5, 0.5, 1, 1" absolutelayout.layoutflags="all" backgroundcolor="transparent" /> </absolutelayout> <absolutelayout margin="{binding layoutthicknessadapter}" absolutelayout.layoutbounds="0.5, 0.5, 1, 1" absolutelayout.layoutflags="all" backgroundcolor="{staticresource nl_bluenight}" opacity="0.8"> <scrollview absolutelayout.layoutbounds="0.5, 0, 1, 0.9" absolutelayout.layoutflags="all"> <stacklayout horizontaloptions="fill" orientation="vertical" spacing="15" verticaloptions="centerandexpand"> <boxview backgroundcolor="transparent" heightrequest="{binding separatorheight}" /> <control:customimagecircle x:name="userprofileimagebutton" heightrequest="{binding pictureheightwidth}" horizontaloptions="center" source="{binding currentuser.imageprofile}" verticaloptions="center" widthrequest="{binding pictureheightwidth}" /> <image heightrequest="{binding separatorheight}" horizontaloptions="center" source="{extension:imagesource logoproject.png}" /> <absolutelayout heightrequest="{binding entryheight}" widthrequest="{binding entrywidth}"> <control:customentry absolutelayout.layoutbounds="0.5, 0.5, 1, 1" absolutelayout.layoutflags="all" backgroundcolor="transparent" fontfamily="{extension:fontfamily roboto_light}" fontsize="{extention:fontsize staticresourcekey=entryfontsize}}" hasborder="false" placeholder="pseudo" placeholdercolor="gray" text="{binding currentuser.pseudo}" textcolor="white" xalign="center" /> <!--<boxview absolutelayout.layoutbounds="0.5, 0.7, 0.8, 1" absolutelayout.layoutflags="xproportional, yproportional, widthproportional" backgroundcolor="{staticresource nl_orangebeer}" />--> </absolutelayout> <absolutelayout heightrequest="{binding entryheight}" widthrequest="{binding entrywidth}"> <control:customentry absolutelayout.layoutbounds="0.5, 0.5, 1, 1" absolutelayout.layoutflags="all" backgroundcolor="transparent" fontfamily="{extension:fontfamily roboto_light}" fontsize="{extention:fontsize staticresourcekey=entryfontsize}}" hasborder="false" placeholder="email" placeholdercolor="gray" text="{binding currentuser.email}" textcolor="white" xalign="center" /> <!--<boxview absolutelayout.layoutbounds="0.5, 0.7, 0.8, 1" absolutelayout.layoutflags="xproportional, yproportional, widthproportional" backgroundcolor="{staticresource nl_orangebeer}" />--> </absolutelayout> <absolutelayout heightrequest="{binding entryheight}" widthrequest="{binding entrywidth}"> <control:customentry absolutelayout.layoutbounds="0.5, 0.5, 1, 1" absolutelayout.layoutflags="all" backgroundcolor="transparent" fontfamily="{extension:fontfamily roboto_light}" fontsize="{extention:fontsize staticresourcekey=entryfontsize}}" hasborder="false" placeholder="firstname" placeholdercolor="gray" text="{binding currentuser.firstname}" textcolor="white" xalign="center" /> <!--<boxview absolutelayout.layoutbounds="0.5, 0.7, 0.8, 1" absolutelayout.layoutflags="xproportional, yproportional, widthproportional" backgroundcolor="{staticresource nl_orangebeer}" />--> </absolutelayout> <absolutelayout heightrequest="{binding entryheight}" widthrequest="{binding entrywidth}"> <control:customentry absolutelayout.layoutbounds="0.5, 0.5, 1, 1" absolutelayout.layoutflags="all" backgroundcolor="transparent" fontfamily="{extension:fontfamily roboto_light}" fontsize="{extention:fontsize staticresourcekey=entryfontsize}}" hasborder="false" placeholder="lastname" placeholdercolor="gray" text="{binding currentuser.lastname}" textcolor="white" xalign="center" /> <!--<boxview absolutelayout.layoutbounds="0.5, 0.7, 0.8, 1" absolutelayout.layoutflags="xproportional, yproportional, widthproportional" backgroundcolor="{staticresource nl_orangebeer}" />--> </absolutelayout> <absolutelayout heightrequest="{binding entryheight}" widthrequest="{binding entrywidth}"> <control:customentry absolutelayout.layoutbounds="0.5, 0.5, 1, 1" absolutelayout.layoutflags="all" backgroundcolor="transparent" fontfamily="{extension:fontfamily roboto_light}" fontsize="{extention:fontsize staticresourcekey=entryfontsize}}" hasborder="false" keyboard="telephone" placeholder="phone number" placeholdercolor="gray" text="{binding currentuser.number}" textcolor="white" xalign="center" /> <!--<boxview absolutelayout.layoutbounds="0.5, 0.7, 0.8, 1" absolutelayout.layoutflags="xproportional, yproportional, widthproportional" backgroundcolor="{staticresource nl_orangebeer}" />--> </absolutelayout> <absolutelayout heightrequest="{binding entryheight}" widthrequest="{binding entrywidth}"> <control:customentry absolutelayout.layoutbounds="0.5, 0.5, 1, 1" absolutelayout.layoutflags="all" backgroundcolor="transparent" fontfamily="{extension:fontfamily roboto_light}" fontsize="{extention:fontsize staticresourcekey=entryfontsize}}" hasborder="false" ispassword="true" placeholder="password" placeholdercolor="gray" text="{binding passwordone}" textcolor="white" xalign="center" /> <!--<boxview absolutelayout.layoutbounds="0.5, 0.7, 0.8, 1" absolutelayout.layoutflags="xproportional, yproportional, widthproportional" backgroundcolor="{staticresource nl_orangebeer}" />--> </absolutelayout> <absolutelayout heightrequest="{binding entryheight}" widthrequest="{binding entrywidth}"> <control:customentry absolutelayout.layoutbounds="0.5, 0.5, 1, 1" absolutelayout.layoutflags="all" backgroundcolor="transparent" fontfamily="{extension:fontfamily roboto_light}" fontsize="{extention:fontsize staticresourcekey=entryfontsize}}" hasborder="false" ispassword="true" placeholder="password (retype)" placeholdercolor="gray" text="{binding passwordtwo}" textcolor="white" xalign="center" /> <!--<boxview absolutelayout.layoutbounds="0.5, 0.7, 0.8, 1" absolutelayout.layoutflags="xproportional, yproportional, widthproportional" backgroundcolor="{staticresource nl_orangebeer}" />--> </absolutelayout> </stacklayout> </scrollview> <absolutelayout absolutelayout.layoutbounds="0.5,1,1,0.1" absolutelayout.layoutflags="all" backgroundcolor="{staticresource nl_orangebeer}"> <control:customlabel absolutelayout.layoutbounds="0.5, 0.5, 1, 1" absolutelayout.layoutflags="all" fontfamily="{extension:fontfamily roboto_light}" fontsize="35" horizontaltextalignment="center" text="save , return" textcolor="white" verticaltextalignment="center" /> <control:custombutton absolutelayout.layoutbounds="0.5, 0.5, 1, 1" absolutelayout.layoutflags="all" backgroundcolor="transparent" bordercolor="transparent" clicked="onsaveclicked" /> </absolutelayout> <absolutelayout absolutelayout.layoutbounds="0, 0, 0.1, 0.1" absolutelayout.layoutflags="all" backgroundcolor="transparent" isvisible="{binding isreturnvisible}"> <control:customimage absolutelayout.layoutbounds="0.5, 0.5, 0.8, 0.8" absolutelayout.layoutflags="all" aspect="aspectfit" source="{extension:imagesource cross.png}" /> <control:custombutton absolutelayout.layoutbounds="0.5, 0.5, 1, 1" absolutelayout.layoutflags="all" backgroundcolor="transparent" bordercolor="transparent" clicked="onreturnclicked" /> </absolutelayout> </absolutelayout> </absolutelayout> </contentpage.content> </contentpage>
where extension:fontsize
coming :
[contentproperty("fontsize")] public class fontsizeextension : imarkupextension { public double fontsize { get; set; } public object providevalue(iserviceprovider serviceprovider) { return services.sizing.fontsizeadapter(fontsize); } }
sizing.cs
public class sizing { public static double fontsizeadapter(double fontsize) { switch (device.runtimeplatform) { case "android": return (fontsize / 2); case "ios": return fontsize; case "windows": case "winphone": return fontsize; default: return fontsize; } } }
however, when fontsize="{extention:fontsize {x:staticresource entryfontsize}}"
throws exception says value cannot null.. how can use both @ same time? mean x:staticresource , extension
thank !
edit - 08/18
1. ensure prefix correctly defined , used
make sure check namespace provided prefix correct markup extension, , there no spelling mistake while specifying prefix. should resolve 'value cannot null' error.
2. specify property name while using nested markup-extensions
the property-name of extension needs specified while using nested markup-extension. otherwise, tests show, treated string value , assigned default content property. should resolve error 'input string not in correct format'.
solution-1: specify property-name
<control:customentry absolutelayout.layoutbounds="0.5, 0.5, 1, 1" absolutelayout.flags="all" ... fontsize="{extension:fontsize fontsize={x:staticresource entryfontsize}}" .../>
solution-2: can simplify further adding staticresourcekey
property in markup extension:
[contentproperty("fontsize")] public class fontsizeextension : imarkupextension { public double fontsize { get; set; } public string staticresourcekey { get; set; } public object providevalue(iserviceprovider serviceprovider) { if (serviceprovider == null) throw new argumentnullexception(nameof(serviceprovider)); if (staticresourcekey != null) { var staticresourceextension = new staticresourceextension { key = staticresourcekey }; fontsize = (double)staticresourceextension.providevalue(serviceprovider); } return services.sizing.fontsizeadapter(fontsize); } }
and usage can be:
... fontsize="{extension:fontsize staticresourcekey=entryfontsize}}" ...
Comments
Post a Comment