wpf - Redundant converter calls when using Triggers to determine value -
i noticed xaml markup wasting resources doing conversions not supposed do. e.g. have following style acts switch:
<style targettype="{x:type image}"> <style.triggers> <datatrigger binding="{binding isdownloaded}" value="true"> <setter property="source" value="{binding data, converter={staticresource bytearraytobitmapimageconv}}"/> </datatrigger> <datatrigger binding="{binding isdownloaded}" value="false"> <setter property="source" value="{binding url, converter={staticresource urltobitmapimageconv}}"/> </datatrigger> </style.triggers> </style> obviously should either download image if has not been cached or turn raw data bitmapimage. problem both cases have taken place @ least once both converters called when datacontext changes, irrespectively of value isdownloaded has. either display converted image still download independendly in background or download image , try convert null (the data) bitmapimage.
setting binding mode onetime did not sadly.
i looking clean way avoid occurs multiple times in application. e.g. here:
<style targettype="contentcontrol"> <setter property="content" value="{staticresource contentnothingselected}"/> <style.triggers> <datatrigger binding="{binding source={x:static local:app.settings}, path=displaymode_current}" value="description"> <setter property="content" value="{staticresource descriptionviewer}"/> </datatrigger> <datatrigger binding="{binding source={x:static local:app.settings}, path=displaymode_current}" value="webpage"> <setter property="content" value="{staticresource webpageviewer}"/> </datatrigger> <datatrigger binding="{binding source={x:static local:app.settings}, path=displaymode_current}" value="media"> <setter property="content" value="{staticresource mediaviewer}"/> </datatrigger> </style.triggers> </style> even if display mode set media or description application navigate corresponding site in background, wasting resources , throwing occasional out of place javascript-error notifications.
i did of in code remodelled more declarative , keep way, appreciated.
you reconsider using triggers , instead use custom control , visual state manager. put logic of downloading , caching , converting images in custom control , set appropriate states. it's hard give example without more xaml i'm afriad.
Comments
Post a Comment